java.net.SocketException:套接字在 Socket.accept() 和 Socket.getInputStream() 之间关闭

java.net.SocketException: Socket is closed between Socket.accept() and Socket.getInputStream()

我正在尝试创建一个 client/server 连接,但我对 java 还是很陌生。所以我得到的错误告诉我套接字已关闭。经过一些工作,我设法在下面编写了给定的代码。我确实相信我将套接字传递给连接 class 的方式有问题,如果我不得不猜测的话,这会导致套接字对象可能被关闭?

我试过添加等待以防万一服务器线程没有被执行,但这似乎没有影响任何东西。也许我应该在自己的命令提示符下使用自己的启动器启动服务器,但我认为这应该可以很好地测试客户端和服务器。

我似乎无法找出为什么在发送消息之前我的套接字已关闭。任何见解将不胜感激!

谢谢!

错误

java.net.SocketException: Socket is closed
    at java.net.Socket.getInputSTream(Unknown Source)
    at Connection.run(Connection.java:17)

Server.java

//main calling snippet. 
import java.lang.Thread;

public class Server {
    public static void main(String[] args) {
        if(args.length != 1) {
            System.err.println("Usage: java Server <port number>");
            System.exit(1);
        }

        int port = Integer.parseInt(args[0]);
        Thread server = new KServer(port);
        server.start();
        //added waits just to make sure the thread was executed? 
        //thinking this might be my problem
        long t = System.currentTimeMillis() + 5000;
        while (System.currentTimeMillis() < t) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        KClient client = new KClient("127.0.0.1",port);
        while (!(client.openConn())) {
            System.out.println("Failed to connect.  Retrying...");
        }
        client.send("Hello World");
        client.closeConn();

    }
}

KServer.java

//the actual server class that manages listening and threading the sockets
import java.net.*;
import java.io.*;

public class KServer extends Thread {

    private int port;
    private ServerSocket sSock;

    public KServer(int thisPort) {
        port = thisPort;
        try {
           sSock = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();            
        }
    }

    public void run() {
        while(true) {
            try (Socket cSock = sSock.accept();) {
                Thread con = new Connection(cSock);
                con.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

Connection.java

//Manages sending and receiving messages

import java.net.Socket;
import java.io.*;

public class Connection extends Thread {

    Socket socket;

    public Connection(Socket s) {
        socket = s;
    }

    public void run() {     
        String msg;
        BufferedReader in;
        try {
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while((msg = in.readLine()) != null) {
                System.out.println(msg);
            }   
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

KClient.java

//manages the clients connection life to the server
import java.net.*;
import java.io.*;

public class KClient {

    private Socket sock;
    private String dest;
    private int port;

    private OutputStreamWriter out;

    public KClient(String dst,int prt) {
        dest = dst;
        port = prt; 
    }

    public boolean openConn() {

        try {
            sock = new Socket(dest,port);
            out = new OutputStreamWriter(sock.getOutputStream(),"ISO-8859-1");          
        } catch (Exception e) {
            e.printStackTrace();
            return false;

        }
        return true;
    }

    public void send(String msg) {
        try {
            out.write(msg);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void closeConn() {

        try {
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}

不要使用 try-with-resources 接受套接字。它将关闭已接受的套接字,该套接字需要保持打开状态以便处理线程可以使用它。处理线程负责关闭它。