Pergunta

So in a nutshell, I have server set up using tcplistener / tcpclient, and it has a couple dozen client machines. Everything is working well, except that I do not yet have a system set up to detect whether or not a client has disconnected. I would like to use heartbeats (periodically sent packets) to show the client is still there. Unfortunately, what is happening is that the server has a waiting receive block called so that it can get actual data from the clients, but it is receiving the heartbeats instead. Is there any way I can set up another receive on the same port that is looking specifically for heartbeats? -Thanks.

Foi útil?

Solução

One common way is to define your packets to have a specific type, therefore the server packet receiving code can just check the type and deal with it appropriately. i.e.

if ( packet.type == PacketTypes.HeartBeat )
    //keep alive the client
else
    //it's data (or another packet)

When you add your code I can give a better example.

UPDATE:

As per our discussions in the comments, you can simply create a separate thread for dealing with your heartbeat packets. In this thread, your socket receive code will ignore anything but heartbeat packets by first checking the type.

You can also create another thread housing a socket receive on the same port to deal with your data. In this case, it would just ignore any heartbeat packets received.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top