Hi,
I have a simple client server application which communicates through sockets. There is a content switch in between client and server that routes the request to a proper server machine. This content switch pings the server machines every 5 millisec to check the heartbeat. My server app is raising an System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> whenever the content switch is pinging and is trying to read the stream.
Inorder to avoid this I have used networkstream.dataavailable to check if there is request from the client or if it is just a ping from the contentswitch. But the networkstream.dataavailable is returning false even if there is data or a true request from true client. I can't use the IP to differentiate between contentswitch and actual client because the ip's are masked. How would I get rid of the I/O exception and read data when available.
My code snippet:
private void ProcessRequest(object param)
{
Socket inSocket = (Socket)param;
if (inSocket.Connected)
{
string clientIP = IPAddress.Parse(((IPEndPoint)inSocket.RemoteEndPoi nt).Address.ToString()).ToString();
NetworkStream networkStream = null;
System.IO.StreamWriter streamWriter = null;
System.IO.StreamReader streamReader = null;
try
{
networkStream = new NetworkStream(inSocket);
streamWriter = new System.IO.StreamWriter(networkStream);
streamReader = new System.IO.StreamReader(networkStream);
// Read data.
if (!networkStream.DataAvailable)
return; ---- This is always returning false. even if data is present. why is that so?
string line = streamReader.ReadLine();
//Parse data
..
//Report result back to the client
streamWriter.WriteLine((int)ret);
streamWriter.Flush();
}
catch (Exception ex)
{
Logger.write(ex.message.tostring());
}
finally
{
if (streamWriter != null) streamWriter.Close();
if (streamReader != null) streamReader.Close();
if (networkStream != null) networkStream.Close();
inSocket.Close();
}
}
Any help would be greatly appreciated. Thanks