1

I'm writing a TCP client and server app. The Server gets a request and opens a Socket per client in a separate thread. the client might register for event and the server should send the events to the client once the occur. My question is, it is a good practice to save the socket and send the events on the same open socket of the request. i.e. what if no messages are sent will the socket close after the some time out? is there need for keepAlives? are there any other scenarios which will cause a disconnect while the client sides still are connected. what is the overhead of creating a new socket for each event - each message sent by the server?

4

1 回答 1

0

You can make alive connections and you don't need to use alive packets between server and client for TCP connections. If connection is dropped, both side will.

If I understand right, you can easily check if the connection is dropped in packet recieving function. Just import a feature that reconnects if disconnected.

But if the connection is dropped before server publishes new "event" and reconnects after that, client cannot recieve this packet.

If you pass over this problem, try this:

  1. Save your events in a list.
  2. Add a timestamp property to every event.
  3. Be sure that client can remember the timestamp value of the last event it received.
  4. Make the client to send a request that includes the timestamp value of the last event for missing events after reconnection.
  5. If server receives this request, make it to send events which timestamps are greater than LastTimeStamp argument in the request packet.
于 2013-03-10T10:37:30.807 回答