I based my program on the asynchronous client/server example on msdn. There weren't too many modifications to the original code. I just created some extra code to response to different messages. F.e. when I send GETARTICLES<EOF > it gets the articles from the DB and when I send something else it does that specific task. That all works fine. The problem is when I connect to the server and I try to send 2 commands after another. F.e. First get the articles, when that is done get the shippings or something. This would look like the following:
Now it does the first if correctly and then it enters the second if..It goes through every method (send, receive)
But the server doesn't react to anything at all. What could be the problem here? When I close the connection to the server and start the process, connecting etc all over again it works.
So I though it was a prolem in the server. I looked at the send process in the server. There I saw that the handler (the client connection) is shutdown, right?
So I tried several things. I commented out these lines and tried to call the beginReceive on the handler object. But this was not so succesful. After that I tried the following approach. But that also doesn't work. Because this is only caught when a new connection is made, right?
Can anyone please help me?
Code:
if(!File.Exists(@"\System\products.xml"))
{
Console.WriteLine("Requesting producs file");
Send("REQPRODUCTS|"+wh_id+"|<EOF>");
sendDone.WaitOne();
Receive();
receiveDone.WaitOne();
Console.WriteLine("Products file received");
}
else
{
Response = "...products.xml already exists...";
Console.WriteLine("Products file exists");
}
if(!File.Exists(@"\System\shippings.xml"))
{
Console.WriteLine("Requesting shippingslist");
Send("GETSHIPPINGSLIST|"+wh_id+"<EOF>");
sendDone.WaitOne();
Receive();
receiveDone.WaitOne();
Console.WriteLine("Shippingslist received");
}
else
{
Response = "...shippings.xml already exists...";
Console.WriteLine("Shippingslist exists");
}
But the server doesn't react to anything at all. What could be the problem here? When I close the connection to the server and start the process, connecting etc all over again it works.
So I though it was a prolem in the server. I looked at the send process in the server. There I saw that the handler (the client connection) is shutdown, right?
So I tried several things. I commented out these lines and tried to call the beginReceive on the handler object. But this was not so succesful. After that I tried the following approach. But that also doesn't work. Because this is only caught when a new connection is made, right?
Code:
private static void SendCallback(IAsyncResult ar)
{
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("Sent {0} bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
//I thought the following would solve the problem but it didn't
//Start accepting again
listenerSock.BeginAccept(
new AsyncCallback(AcceptCallback),
listenerSock);
}
catch (Exception ex)
{
Console.WriteLine("Error while sending: "+ ex.Message);
}
}
Comment