Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/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_Multithreading - Fatal编程技术网

我收到一个java.net.SocketException:连接重置错误

我收到一个java.net.SocketException:连接重置错误,java,multithreading,Java,Multithreading,当我关闭一个客户端时,我遇到了这个错误,我尝试了一些关闭连接的方法,但最终我不确定如何消除这个错误: import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.i

当我关闭一个客户端时,我遇到了这个错误,我尝试了一些关闭连接的方法,但最终我不确定如何消除这个错误:

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;

    public class Client {

        private JTextArea tArea;
        private BufferedReader in;
        private Socket sock;
        private PrintWriter out;

        public static void main(String[] args) {
            new Client().go();
        }

        public void go() {
            JFrame frame = new JFrame("Chat Client");
            final JTextField tField = new JTextField(25);
            tArea = new JTextArea(30, 20);
            JButton button = new JButton("send");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ev) {
                    sendMessage(tField.getText());
                    tField.setText("");
                }
            });
            frame.setSize(300, 500);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(tArea, BorderLayout.NORTH);
            frame.add(tField, BorderLayout.CENTER);
            frame.add(button, BorderLayout.EAST);
            frame.pack();
            setupNetwork();
            Thread t = new Thread(new IncomingReader());
            t.start();
        }

        public void setupNetwork() {
            try {
                sock = new Socket("localhost", 8999);
                in = new BufferedReader(
                        new InputStreamReader(sock.getInputStream()));
                out = new PrintWriter(sock.getOutputStream());
            } catch (IOException ex) {
                ex.printStackTrace();
                System.out.println("fail networking");


}
    }

    class IncomingReader implements Runnable {
        public void run() {
            //receive messages from server
            try {
                String message = null;
                while ((message = in.readLine()) != null) {
                    tArea.append(message + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("fail incoming reader");

            }
        }
    }

    public void sendMessage(String message) {
        try {
            out.println(message);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("fail send message");
        }
    }
}

我知道这是因为我正在关闭一个连接,但它一直抛出这个错误,这很烦人,即使它没有破坏我的服务器。

这是因为在客户端上完成套接字操作后,您应该正确地关闭它。我不是一个摇摆大师,但看起来是你需要的。只要在主窗口关闭时关闭连接即可

编辑:当您关闭TCP套接字时,需要做一点工作:。当你关闭程序时,这不会发生。操作系统将为您关闭连接并释放所有相关资源,但不会通知服务器客户端关闭连接

编辑2:演示 服务器:

客户:

public class Server {
    public static void main(String args[]) {
          try {
              System.out.println("waiting connetion");
              ServerSocket serverSocket = new ServerSocket(8999);
              while (true) {
                  Socket clientSocket = serverSocket.accept();
                  System.out.println("Connected");
                  Reader reader = new InputStreamReader(
                          clientSocket.getInputStream());
                  reader.read(); // wait for input
                  System.out.println("No exception");
              }
          } catch (IOException ex) {
              System.out.println("Exception");
              ex.printStackTrace();
          }
    }
}

造成这种情况的通常原因是您已写入另一端已关闭的连接。换句话说,应用程序协议错误


这里的具体问题是,当您从服务器中的
readLine()
获取
null
时,您应该关闭该套接字并从写入程序数组中删除相应的
Writer

要告诉java代码所有HTTP请求都应该通过代理路由,请使用以下代码段:

public class Client {
    public static void main(String args[]) throws Exception {
         Socket sock = new Socket("localhost", 8999);
         System.out.println("Press 1 to close gracefully, any other nuber otherwise");
         Scanner sc = new Scanner(System.in);
         if (sc.nextInt() ==1 ) {
             sock.close();
         } else {
             //do nothing
         }
    }
}

System.setProperty设置代理主机和端口。验证器应该是您的公司用户名和密码。这现在应该可以工作了。

在客户端中放置
socketname.close()
方法…问题是,客户端在服务器能够从中读取流之前就完成了…

当他关闭客户端连接时会发生这种情况。@EJP是的,当他突然关闭客户端连接时会发生这种情况。客户端关闭连接时,应正确通知服务器。如果只是操作系统提供的资源,这种情况就不会发生。查看当进程退出时,操作系统会优雅地、非突然地关闭TCP连接。在您的非规范性引用中,没有任何内容说“如果只是操作系统释放资源,这将不会发生”。@EJP您为我编写了代码来证明我的观点。查看编辑后的答案这不是解决您问题的方法,但我建议您应该使用ApacheMina来做这类事情。因为这些库自己处理低级维护,应用程序可以专注于编写逻辑。我一直在尝试,但我想不出在用户退出应用程序时如何关闭或删除连接forcefully@CatznDogz你找不到close()方法?在你的答案中,您说这是因为客户端正在尝试写入已关闭的连接。但是,是什么让另一方(即服务器端)首先关闭了连接?
public class Server {
    public static void main(String args[]) {
          try {
              System.out.println("waiting connetion");
              ServerSocket serverSocket = new ServerSocket(8999);
              while (true) {
                  Socket clientSocket = serverSocket.accept();
                  System.out.println("Connected");
                  Reader reader = new InputStreamReader(
                          clientSocket.getInputStream());
                  reader.read(); // wait for input
                  System.out.println("No exception");
              }
          } catch (IOException ex) {
              System.out.println("Exception");
              ex.printStackTrace();
          }
    }
}
public class Client {
    public static void main(String args[]) throws Exception {
         Socket sock = new Socket("localhost", 8999);
         System.out.println("Press 1 to close gracefully, any other nuber otherwise");
         Scanner sc = new Scanner(System.in);
         if (sc.nextInt() ==1 ) {
             sock.close();
         } else {
             //do nothing
         }
    }
}
System.setProperty("http.proxyHost", "proxyHost");
System.setProperty("http.proxyPort", "proxyPort");
Authenticator authenticator = new Authenticator() {
     public PasswordAuthentication getPasswordAuthentication() {
    return (new PasswordAuthentication("USERNAME","PASSWORD".toCharArray()));
    }
};
Authenticator.setDefault(authenticator);