Is it possible to close and reopen a socket?

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

But it’s still not clear to me how flexible this material is. For example, is it possible for a client to close an open socket (at both ends) and open it again (provided that the server supports opening the socket).

Is it possible for the server to “know” that the socket has been closed on the client side? Is it possible for the client to know that the socket has been closed on the server side?

ADDED:

Another important thing for me. What happens if an application (without a server or client) crashes, is abnormally terminated, is killed? Is it closed for all sockets open on the side of the application?

ADDED 2:

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

+6
java networking sockets
source share
3 answers

Is it possible for the server to “know” that the socket has been closed on the client side?

When a server tries to send some data to this client, an appropriate exception will be thrown.

Another important thing for me. What happens if the application (no server or client) crashes, abnormally terminated, is killed? Does it close all sockets open on the side of the application?

Exceptions are thrown to handle these abnormal cases. If there is black, and the client (or server) is turned off, the other side will receive an exception as soon as it tries to interact with the turned off side.

UPD:

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

Create a new socket.

+2
source share

A socket can be used for many things for which the answers to these questions will change, but I assume you are talking about TCP.

For example, is it possible for a client to close an open socket (at both ends) and open it again (provided that the server supports opening the socket).

No, because TCP will say goodbye and you won’t be able to get a connection from it again. You will need to perform a three-way handshake again, and this will start a new connection.

Is it possible for the server to “know” that the socket has been closed on the client side? Is it possible for the client to know that the socket has been closed on the server side?

Yes TCP can send a farewell packet, or one side can time out, and it is possible to detect these scenarios in both cases.

+9
source share

Q1-> Is it possible for the server to “know” that the socket has been closed on the client side? Is it possible for the client to know that the socket has been closed on the server side?

A1 → The gap will be known if the client closes the socket and vice versa. In fact, FIN will be sent from the end, which initiates the closure of the connection.

Q2-> What should I do if the application on one side of the socket is disconnected (killed, closed, terminated), and then it is turned on again (on the same IP address and the same port). Should we create a new socket between the two applications, or can we use the old socket (created before the failure).

A2-> If the socket is created, the fd number is associated with ip-addr and the port, therefore the socket on the server side has server-ip, and on the client side and socket on the client side there is client-ip and some port. if the client crashes, all fd are freed and cannot be reused. If we use the same fd, the system will treat it as a regular fd file.

+2
source share

All Articles