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 Answers
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).