views:

130

answers:

2

I learned an example of usage of sockets. In this example a client sends a request to a server to open a socket and then the server (listening to a specific port) opens a socket and everything is fine, socket is "opened" from both sides (client and server).

But it is still not clear to me how flexible is this stuff. For example, is it possible for the client to close an opened (from both ends) socket and to reopen it again (under condition that the server keeps the socket opened).

Is it possible for the server to "know" that a socket was closed on the client side? Is it possible for the client to know that a socket was closed on the server side?

ADDED:

One more important thing to me. What happens if a application (no mater server or client) crashes, abnormally terminated, killed? Will it close all sockets opened on the side of the application?

ADDED 2:

What if an application on one side of the socket is switched off (killed, closed, terminated) and then it is switched on again (on the same IP address and the same port). Should we create a new socket between the two applications or we can use the old socket (created before the crash).

+3  A: 

A socket can be used for a lot of things for which the answers to these questions would change, but I'll assume you're talking about TCP.

For example, is it possible for the client to close an opened (from both ends) socket and to reopen it again (under condition that the server keeps the socket opened).

No, because TCP will perform a goodbye and you can't pick up the connection from there again. You'd have to do the three-way handshake again, and that starts a brand new connection.

Is it possible for the server to "know" that a socket was closed on the client side? Is it possible for the client to know that a socket was closed on the server side?

Yes. TCP can send out a goodbye packet or one side can time out and it's entirely possible to detect these scenarios in both cases.

Hao Lian
I do not understand the procedure. For example the client close the socket and server does not do it. The client tries to open a new socket (from the same port and IP address) but server says: sorry it is impossible since such a socket already exists! And then client says: well it exists for you, but i closed it and I cannot reopen it again!
Roman
Server should process sockets in different threads. It shouldn't be a problem to open (i.e. accept) sockets on the same port.
Roman
ah, what an ugly mess with nicknames.
Roman
@Roman, I mean that the client tries to re-open a socket from the same port (client port) and the same IP address (client IP). Then server will not allow to do it, as far as I know.
Roman
(a) Only if it has been specifically programmed to disallow it. Has it?(b) In any case it should also have been programmed to close sockets when it detects EOS or an exception on them, which it pretty infalliby will when the client closes his end.
EJP
+1  A: 

Is it possible for the server to "know" that a socket was closed on the client side?

When server tries to send some data to that client a correspondent exception will be thrown.

One more important thing to me. What happens if a application (no mater server or client) crashes, abnormally terminated, killed? Will it close all sockets opened on the side of the application?

Exceptions are created for handling these abnormal cases. If there is a black out and a client (or server) is turned off then other side will get an exception as soon as it try to interact with turned off side.

UPD:

What if an application on one side of the socket is switched off (killed, closed, terminated) and then it is switched on again (on the same IP address and the same port). Should we create a new socket between the two applications or we can use the old socket (created before the crash).

Create new socket.

Roman