By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,300 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,300 IT Pros & Developers. It's quick & easy.

Asynchronous server isn't responding after second send

P: 50
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:

Expand|Select|Wrap|Line Numbers
  1.             if(!File.Exists(@"\System\products.xml"))   
  2.             {   
  3.                 Console.WriteLine("Requesting producs file");   
  4.                 Send("REQPRODUCTS|"+wh_id+"|<EOF>");   
  5.                 sendDone.WaitOne();   
  6.                 Receive();   
  7.                 receiveDone.WaitOne();   
  8.                 Console.WriteLine("Products file received");   
  9.             }   
  10.             else  
  11.             {   
  12.                 Response = "...products.xml already exists...";   
  13.                 Console.WriteLine("Products file exists");   
  14.             }   
  15.             if(!File.Exists(@"\System\shippings.xml"))   
  16.             {   
  17.                 Console.WriteLine("Requesting shippingslist");   
  18.                 Send("GETSHIPPINGSLIST|"+wh_id+"<EOF>");   
  19.                 sendDone.WaitOne();   
  20.                 Receive();   
  21.                 receiveDone.WaitOne();   
  22.                 Console.WriteLine("Shippingslist received");   
  23.             }   
  24.             else  
  25.             {   
  26.                 Response = "...shippings.xml already exists...";   
  27.                 Console.WriteLine("Shippingslist exists");   
  28.             }  
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?

Expand|Select|Wrap|Line Numbers
  1.         private static void SendCallback(IAsyncResult ar)   
  2.         {   
  3.             try  
  4.             {   
  5.                 // Retrieve the socket from the state object.   
  6.                 Socket handler = (Socket)ar.AsyncState;   
  7.  
  8.                 // Complete sending the data to the remote device.   
  9.                 int bytesSent = handler.EndSend(ar);   
  10.                 Console.WriteLine("Sent {0} bytes to client.", bytesSent);   
  11.  
  12.                 handler.Shutdown(SocketShutdown.Both);   
  13.                 handler.Close();   
  14.  
  15.                 //I thought the following would solve the problem but it didn't   
  16.                 //Start accepting again   
  17.                 listenerSock.BeginAccept(   
  18.                         new AsyncCallback(AcceptCallback),   
  19.                         listenerSock);   
  20.  
  21.             }   
  22.             catch (Exception ex)   
  23.             {   
  24.                 Console.WriteLine("Error while sending: "+ ex.Message);   
  25.             }   
  26.         }   
Can anyone please help me?
Oct 15 '08 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 190
Please note well: I have zilch experience with asynchronous socket communication.

However, I do see that in the notes for Socket.Shutdown, the Shutdown.Both value completely disables sending/receiving on that socket. The remarks indicate: "Do not attempt to reuse the Socket after closing."

I am not sure what the process is to re-enable a disabled socket, but it sounds like your program is working fine, but after the first batch of commands, the socket is closed and will not be re-enabled until you restart the process.

Check out the notes for using Close without Shutdown. There is an option to block the close until the outgoing data is sent.

Again, I know next to nothing about Sockets, but probably the MSDN example assumed only one send/receive. Probably if you want multiple send/receives, you would want to shutdown the socket only after all communications are finished.
Oct 16 '08 #2

P: 50
Thank you for your response. Yes I also thought that was the problem so I disabled this piece of code without result. I'll do some more investigating on those properties and let you know.
Oct 17 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.