How to correctly close a socket and then reopen it?

落花浮王杯 提交于 2019-12-05 05:06:45

After closing a socket, you cannot reuse it to share other data between Server and Client classes. From the Java Socket API, about close() method's description:

Any thread currently blocked in an I/O operation upon this socket will throw a SocketException.

Once a socket has been closed, it is not available for further networking use (i.e. can't be reconnected or rebound). A new socket needs to be created.

Closing this socket will also close the socket's InputStream and OutputStream.

If this socket has an associated channel then the channel is closed as well.

Then, it isn't possible to close a socket and reopen it. That's the reason, I think, of the exception being thrown.

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();
        }
    }
}

It works, beacuse you can declare tons of Sockets with the same ServerSocket, so for example this works too:

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 a client to connect when you declare a Socket! Code:

Client:

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");
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!