How to close a socket and then open it again?

I am developing a game for the class in which I participate, and it is about 99%. However, I realized that there is a problem: if the server and client are disconnected, and the client tries to reconnect (and the server is working fine and working fine), the client does not create a new connection.

Server and client are multithreaded. If I send the client a Kick message about what will happen, the client will close its socket. If I reconnect, I get a SocketException : if closure, whenever a connection is made, a new client is created, which is basically a class that creates a socket and connects to the server with the get / send stream.

What do you think I'm doing illogical? This is similar to trying to use the old Socket and Client that were created, but if I make a few calls to println , I see that it creates a new one, and they are in different memory locations.

Thanks!

+4
source share
2 answers

After you close the socket, you cannot reuse it to exchange other data between the Server and Client classes. From the Java Socket API in the description of the close() method:

Any thread that is currently blocked in the I / O operation on this socket will throw a SocketException.

Once the socket has been closed, it is unavailable for further use of the network (i.e. cannot be reconnected or rebound). A new connector must be created.

Closing this socket will also close the InputStream and OutputStream sockets.

If this socket has an associated channel, the channel closes as well.

Then closing the socket and reopening it is not possible. This is the reason, in my opinion, because this is an exception.

+7
source

It can be easy! I made this program (server side):

 import java.net.ServerSocket; import java.net.Socket; import java.io.DataInputStream; import java.io.DataOutputStream; public class Host{ public static void main(String[] args) throws Exception{ while(true){ ServerSocket ss = new ServerSocket(1300); Socket s = ss.accept(); DataInputStream din = new DataInputStream(s.getInputStream()); String msgin = din.readUTF(); System.out.println(msgin); ss.close(); s.close(); din.close(); } } } 

Client side:

 import java.net.Socket; import java.io.DataInputStream; import java.io.DataOutputStream; public class Client{ public static void main(String[] args) throws Exception{ while(true){ Socket s = new Socket("localhost", 1300); DataOutputStream dout = new DataOutputStream(s.getOutputStream()); dout.writeUTF("hello"); s.close(); dout.close(); } } } 

This works, you can declare many sockets with the same ServerSocket, so, for example, this also works:

 ServerSocket ss = new ServerSocket(1300); Socket a = ss.accept(); Socket b = ss.accept(); Socket c = ss.accept(); 

and you have 3 sockets ... Remember: java waits for the client to communicate when you declare a Socket! Code:

Customer:

 import java.net.Socket; public class client { public static void main(String[] args) throws Exception{ Socket a = new Socket("localhost", 1300); Thread.sleep(3000); Socket b = new Socket("localhost", 1300); Thread.sleep(3000); Socket c = new Socket("localhost", 1300); Thread.sleep(3000); a.close(); b.close(); c.close(); } } 

Server:

 import java.net.ServerSocket; import java.net.Socket; public class server { public static void main(String[] args) throws Exception{ ServerSocket ss = new ServerSocket(1300); System.err.println("Listening..."); Socket a = ss.accept(); System.err.println("1"); Socket b = ss.accept(); System.err.println("2"); Socket c = ss.accept(); System.err.println("3"); } } 
+2
source

Source: https://habr.com/ru/post/1385760/


All Articles