Asynchronous server isn't responding after second send

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SvenV
    New Member
    • Oct 2008
    • 50

    Asynchronous server isn't responding after second send

    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:

    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");   
                }
    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?

    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);   
                }   
            }
    Can anyone please help me?
  • mldisibio
    Recognized Expert New Member
    • Sep 2008
    • 191

    #2
    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.

    Comment

    • SvenV
      New Member
      • Oct 2008
      • 50

      #3
      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.

      Comment

      Working...