Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.net.SocketException:从服务器套接字重置连接_Java_Sockets - Fatal编程技术网

java.net.SocketException:从服务器套接字重置连接

java.net.SocketException:从服务器套接字重置连接,java,sockets,Java,Sockets,客户端代码 public class NewClass { ServerSocket myServerSocket; boolean ServerOn = true; public NewClass() { try { myServerSocket = new ServerSocket(8888); } catch (IOException ioe) { System.out.println("Could not create server s

客户端代码

public class NewClass {

ServerSocket myServerSocket;
boolean ServerOn = true;

public NewClass() {
    try {
        myServerSocket = new ServerSocket(8888);
    } catch (IOException ioe) {
        System.out.println("Could not create server socket on port 8888. Quitting.");
        System.exit(-1);
    }



    while (ServerOn) {
        try {
            Socket clientSocket = myServerSocket.accept();
            ClientServiceThread cliThread = new ClientServiceThread(clientSocket);
            cliThread.start();
        } catch (IOException ioe) {
            System.out.println("Exception found on accept. Ignoring. Stack Trace :");
            ioe.printStackTrace();
        }
    }
    try {
        myServerSocket.close();
        System.out.println("Server Stopped");
    } catch (Exception ioe) {
        System.out.println("Error Found stopping server socket");
        System.exit(-1);
    }
}

public static void main(String[] args) {
    new NewClass();
}

class ClientServiceThread extends Thread {

    Socket myClientSocket;
    boolean m_bRunThread = true;

    public ClientServiceThread() {
        super();
    }

    ClientServiceThread(Socket s) {
        myClientSocket = s;
    }

    public void run() {
        BufferedReader in = null;
        PrintWriter out = null;
        System.out.println(
                "Accepted Client Address - " + myClientSocket.getInetAddress().getHostName());
        try {
            in = new BufferedReader(new InputStreamReader(myClientSocket.getInputStream()));
            out = new PrintWriter(new OutputStreamWriter(myClientSocket.getOutputStream()));

            while (m_bRunThread) {
                String clientCommand = in.readLine();
                if (clientCommand != null) {
                    System.out.println("Client Says :" + clientCommand);
                }

                if (!ServerOn) {
                    System.out.print("Server has already stopped");
                    out.println("Server has already stopped");
                    out.flush();
                    m_bRunThread = false;
                }
                if (clientCommand.equalsIgnoreCase("quit")) {
                    m_bRunThread = false;
                    System.out.print("Stopping client thread for client : ");
                } else if (clientCommand.equalsIgnoreCase("end")) {
                    m_bRunThread = false;
                    System.out.print("Stopping client thread for client : ");
                    ServerOn = false;
                } else {
                    out.println("Server Says : " + clientCommand);
                    out.flush();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
                myClientSocket.close();
                System.out.println("...Stopped");
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }

    }
}
上面代码的目的是创建服务器套接字和客户端套接字,以便在服务器和客户端之间传递数据。当客户端将数据发送到服务器时。服务器抓取消息并在屏幕上打印,但有以下例外情况。从字符串clientCommand=in.readLine()弹出的弹出窗口;出现在服务器代码上的行

public class Client {
public static void main(String[] args) throws IOException {

    Socket s = new Socket(InetAddress.getLocalHost(), 8888);


    PrintWriter out =new PrintWriter(s.getOutputStream(), true);
    out.print("KKKKKKKKKKKKK \r\n");
    out.flush();
    out.close();



 }

您的代码无效。您的服务器代码依赖于正确实现协议的客户机,而该客户机没有。坏习惯。需要防御性编码。如果
clientCommand==null
必须退出此读取循环并关闭套接字。您当前的代码将尝试写入关闭的连接,该连接正好产生此异常。。。稍后。

不确定您的期望是什么。在客户端关闭其套接字末端后,服务器试图继续读取,因此服务器收到了相应的异常。@JimGarrison仅此一项将导致
readLine()
返回null。还有更多:前面的文字。
 java.net.SocketException: Connection reset