this is my first post here so please be gentle =)
I’m writing an asynchronuos server/client application for the management of a DB for my office, i tested it locally on my computer and it worked fine. When I uploaded the server tool on another machine I found something that is driving me crazy: the server/client handle only the first query, when I send another query both server and client crash.
After going through lines in debug mode I found the problem: after the beginconnect() (during the second query) the socket is connected but when the debugger reaches the beginsend() line the socket is disconnected (this happens only on client side, the socket on server side seems connected).
I can’t figure how it can happen, the socket is re-instantiated every time I send a new query and of course is shutted down and disposed after the query process (send/receive) is completed. My idea is that the disposal of the socket doesn’t happen immediately but after a while (but why always between the new beginconnect() and beginsend()?). Do you have any clue on this? Or I should reserve a room in a mental hospital?
* NEW PART (EDITED) *
well..here again! This time the socket on client side remains open for the entire session (socket closure occurs only on client tool closure) but i receive another kind of error: “System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)”
After some googling I discovered that this kind of error is generally triggered by the server on the other side of the connection so I tryed to “mine” server code lines to figure where this connection closure command is. I found that the problem may be in the following piece of code:
private static void SendCallback(IAsyncResult ar)
{
Console.WriteLine("send callback");
try
{
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("\nQuery answer sent to client");
handler.Shutdown(SocketShutdown.Both);
//handler.Disconnect(true);
//handler.Dispose();
state.content = new List<byte[]>();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey();
}
}
deleting the handler.shutdown line the client is no more able to detect the end of transmission (not a real problem, is the last part of the sent packet so it is possible to use that as trigger..at least I think). Any suggestion? I read something about the keepalive but I can’t understand how to set it and if it is really necessary in this situation. Thanks in advance for all your effort.
I would recommend not to open and close sockets like you do.
Keep a socket open if possible, because it needs to get freed by your program and the OS.