Alex
Alex

Reputation: 11157

signal sent when stream is closed

I have a client-server app and i observed that (in a local environment i.e. LAN ) when i close the stream that connects the client to the server at one end, at the other end it is observable ( i.e. if in my client i close the stream that connects the client to the server, at the other end, at the server, it is noticeable and it is reported as an Exception in the try-catch block that surrounds my server-stream code ) .

How is it possible ? Does the client/server send a specific code that the stream is closed just before closing the stream ? Is it applicable also in wider networks ( like big WANs ) ?

The bigger question is that when i close a stream at one end, can i rely on the fact that at the other end it will be noticed and in consequence close that thread and not just wait endless to recieve data that will never come due to the fact that that stream is closed ?

The maximum distance between a client and a server is 700 km ( i don't know if it plays a role but i mentioned it just in case )

Thanks !

Upvotes: 0

Views: 170

Answers (1)

Martin v. Löwis
Martin v. Löwis

Reputation: 127587

In TCP, the connection shutdown procedure certainly involves sending additional datagrams to the other side, so each side will notice that the connection was orderly shut down.

Relying on this has one major pitfall: in case of an unorderly shutdown (e.g. physical disconnect of network cable), no notification is sent. There is a "keepalive" feature in TCP where the other end will regularly ping whether the connection is still alive, but that is not very practical as the frequency of pings is very low (order of hours). If "most" connections see an orderly shutdown (which includes client application crashes as long as the system keeps running), then relying on the shutdown notifications is reasonable. Server software typically puts a timeout into every connection just to be on the safe side.

Upvotes: 3

Related Questions