On Monday, 24 March 2014 at 18:06:56 UTC, nrgyzer wrote:
Alright, that world work. But what about sending 10 Bytes,
waiting some minutes (probably some hours) and sending 10 Bytes
again. Is it possible to do this on the same Socket?
Yes. Unless your socket is marked as non-blocking, the read
operation will block until more data is available (or the
connection gets closed).
My scenario is: I've one central server and multiple clients.
The clients are connecting to the server and send (or request)
some information. After that the clients are simply "sleeping"
for an unknown time. For instance client 1 for 1 seconds,
client 2 for 1 minute, client 3 for 1 hours and client 4 for 1
day. After that time they resend/request some more information
from the server... my questions regarding this are:
- When I client connects, the server creates a thread. How can
I determine that I can terminate the thread because the
connection is inactive (for instance because the client closed
the connection or some connecting issues)?
As mentioned previously, you know that the connection was closed
cleanly on the remote side when receive returns an empty buffer.
- How can the client check if the connection is alive? (If the
connection isn't alive, I need to reconnect)
You generally have to implement this yourself. Silence on the
wire can mean two things:
1. No data is being sent;
2. Something is preventing data from being sent (lost packets, a
peer was shut down or restarted, Internet routing changes,
squirrel chewed through a wire).
Generally network software implements "ping" packets that are
sent occasionally (once every few seconds or minutes). If no
"ping" packets are received throughout a certain interval, the
connection is considered dead.
TCP also has a "keepalive" option which does this for you, but it
is an optional TCP feature and thus may or may not work.