My project requires the client to always be connected to the server via tcp. I would like to poll the connection to see if has been broken, if so I need to make a new connection.
From my understanding it appears in order to test the connection you must first send a byte of data, if you receive an error in return it means the connection is broken.
However, using this method below it is always showing as connected:
public static void PollConnection()
{
Debug.Log("in poll connection");
// This is how you can determine whether a socket is still connected.
try
{
byte[] tmp = new byte[1];
client.Client.Send(tmp, 0, 0);
isConnected = true;
Debug.Log("Appears connected to endpoint at this time");
}
catch (SocketException e)
{
isConnected = false;
Debug.Log("poll connection exception: " + e.Message);
}
}
It’s not clear what you use that TCP connection for. Just sending some random bytes doesn’t seem to make much sense. In order to send a heartbeat / ping you usually want the other side to react to that so you can verify that the other side is still alive. Of course for TCP it should be enough to just send onedirectional since you automatically get an ACT back.
Though your real issue seems to be in the way you call the Send method. You only provided a snippet of code so a lot of things are not clear. I guess “client” is a TCPClient? That means client.Client is a Socket. Though I can’t find any overload of the Send method that takes your parameters. Though I’ll assume that you use one of the overloads that takes a byte array, and offset and a length / size parameter. If that’s the case, here’s already your issue. You specify to send 0 bytes. If you send 0 bytes, nothing will be transmitted and the result will always be a success, at least in the mono implementation of the Socket class.
What should I put on the receiving end? I read somewhere about sending a null value to see if the connection still exists, is this possible? Or should I send a string to match on the server, if the string matches send a reply?
I don’t know enough about TCP to say whether you can or can’t send a null value. But what you send I’d say should depend on how you’re designing your networking. For my UDP based networking system, I built a messages system on top of it, and defined a keep alive message type. When a packet comes in, one of the first things that is checked is the message type, and if it is a keep alive message I just don’t pass it up to the higher levels of the messaging system. Every time any message comes in, a timer which tracks how long since receiving the last message is reset, including if it is a keep alive message.
I send keep alives after 3 seconds of not sending anything (every time I send any packet I reset a different timer which tracks how long since I last sent a message), both from the client to the server and from the server to all clients. If 10 seconds have passed without receiving anything I consider the connection disconnected. I don’t know if those are appropriate values, or a good way of doing this, for TCP.