I have a service that receives events by TCP
, when an exception occurs in the method ClientConnected(IAsyncResult asyncResult)
the application is closed, and when controlling the exception with a try catch
the application is not closed but it does not allow me to of that ip
keep coming events.
What I want is that when an exception occurs, I will restart the socket
, because when I close and open the service, everything continues to function normally.
internal void ClientConnected(IAsyncResult asyncResult)
{
try
{
// Increment our ConcurrentConnections counter
Interlocked.Increment(ref _currentConnections);
// So we can buffer and store information, create a new information class
SocketConnectionInfo connection = new SocketConnectionInfo();
connection.Buffer = new byte[SocketConnectionInfo.BufferSize];
// We want to end the async event as soon as possible
Socket asyncListener = (Socket)asyncResult.AsyncState;
Socket asyncClient = asyncListener.EndAccept(asyncResult);
// Set the SocketConnectionInformations socket to the current client
connection.Socket = asyncClient;
// Tell anyone that's listening that we have a new client connected
if (OnClientConnected != null)
{
OnClientConnected(this, null);
}
// TODO :: Add throttleling (using SEMAPHORE's)
// Begin recieving the data from the client
if (this.Type == ServerType.TCP)
{
asyncClient.BeginReceive(connection.Buffer, 0, connection.Buffer.Length, SocketFlags.None, new AsyncCallback(DataReceived), connection);
}
else if (this.Type == ServerType.UDP)
{
asyncClient.BeginReceiveFrom(connection.Buffer, 0, connection.Buffer.Length, SocketFlags.None, ref ipeSender, new AsyncCallback(DataReceived), connection);
}
// Now we have begun recieving data from this client,
// we can now accept a new client
listener.BeginAccept(new AsyncCallback(ClientConnected), listener);
}
catch (Exception e)
{
Console.WriteLine("HighPerformanceServer: Exception metodo ClientConnected ->" + e.Message);
Stop();
}
}