1
votes

my program have one server socket and multiple client socket, so, what i want do is When the server close(shutdown),change one of the client sockets to server socket. is there a way to do this?

1
I don't think this is possible, and it seems like a rather perverse thing to do. If you still want to have a server socket, then why did you close the one you already had? - jasonharper
@jasonharper my program concept is socket chatting program. so the first program will be the server, the second or later will client. but i have issue that if server closed, also client connection close. All I want to do is have the client connected even if the server is closed. - 박지석

1 Answers

0
votes

what i want do is When the server close(shutdown),change one of the client sockets to server socket. is there a way to do this?

No -- once a TCP socket is connected, it can only be used for that one TCP connection, and once the TCP connection has been broken (e.g. by the server exiting), then all you can do with the socket is close it.

However, that doesn't mean you can't have a high-availability chat system like the one you are envisioning. What you can do is have your clients "plan ahead" by gathering the IP addresses of all the other clients in advance (either through the server, or if all the clients are on the same LAN, perhaps through broadcast or multicast UDP packets). That way, if/when the server goes away, the clients all have another IP address handy that they can automatically try to connect to (using a new TCP socket).

Note that you'll need to make sure that the program running at that IP address is accepting incoming TCP connections (server-style); you also might want to specify some kind of rule so that all of the clients will reconnect to the same IP address (e.g. sort the IP addresses numerically and have all the clients try to connect to the smallest IP address in the list, or something like that).