Server Crashes when the client is not reachable

Feb 24 2009 8:21 AM
Dear All,

I have a network application with UDP implementation.

In the server it has a specific IP and port number configured.

It will listen for the messages from the client to server and acknowledges the same.

Same way some times the server will initiate the communication and will try to send the message from the server to client.

For the messages from client to server and ACK it is working fine, even though the client or the server is restarted.

For the messages form the server to client, it tries and if the client is not reachable, it throws the error and then in the catch also I have made the server to continue to listen by calling BeginReceiveFrom, but it still throws the error.

The code used is:
        private void OnReceive(IAsyncResult ar)
        {
            IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint epSender = (EndPoint)ipeSender;

            try
            {
                serverSocket.EndReceiveFrom(ar, ref epSender);

                //Transform the array of bytes received from the user into an
                //intelligent form of object Data
                Data msgReceived = new Data(byteData);

                ipeSender = epSender as IPEndPoint;

                // Processing the message which is received

                byte[] message = new byte[<<ACK size>>];
                // Frames the ACK

                serverSocket.BeginSendTo(message, 0, message.Length, SocketFlags.None, epSender,
                    new AsyncCallback(OnSend), epSender); // epSender holds the sender Endpoint.

                byteData = new byte[40];
                serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epServerSender,
                    new AsyncCallback(OnReceive), epServerSender); // epServerSender holds the server IP and port.
            }
            catch (Exception ex)
            {
                byteData = new byte[40];
                serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length,
                    SocketFlags.None, ref epServerSender, new AsyncCallback(OnReceive), epServerSender);
                    // error is thrown in the above statement
                }
            }
        }

Can any one help me to get rid of this issue.

Thanks a lot in advance.