I am trying to connect to a piece of hardware via TCP/IP. I had done this very smoothly in VB and am trying to achieve the same result in C#.
The code looks like this:
internal Socket connectSocket(IPEndPoint vIPEndPoint)
{
Socket lclSocket = new Socket(vIPEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
lclSocket.Connect(vIPEndPoint);
}
catch (Exception ex)
{
}
if (lclSocket.Connected)
{
string str = receiveResponse(lclSocket);
if (str.Contains(PFCommonVariables.m_strCommandDelimiter))
{
_sockets.Add(vIPEndPoint, lclSocket);
}
else
{
lclSocket.Disconnect(true);
}
}
lclSocket.ReceiveTimeout = RECEIVE_TIMEOUT;
lclSocket.SendTimeout = SEND_TIMEOUT;
return lclSocket;
}
As I said, this code is exactly the same as the code in VB, and there I can keep a succesful connection. THe problem here in C# is that right after the 'socket.Connect' call that I make to the IPEndPoint, the Connected property is shown to be True. I verify successful sending and receiving of data thru Wireshark also. Then, I put a breakpoint at the if(lclSocket.Connected) statement. When I first come here, the Connected property is still True. However, if I wait here for 1-2 seconds without doing anything, the Connected property becomes false.
So, the connection is lost automatically. Then, if I keep the debugger on and wait on a line, in Wireshark I see numerous Keep-Alive TCP messages being sent regularly back and forth between the PC and the Hardware. I am not sue what these are but they may help diagnose the problem...