Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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 ServerSocket客户端(True)_Java_Loops_Client_Serversocket - Fatal编程技术网

Java ServerSocket客户端(True)

Java ServerSocket客户端(True),java,loops,client,serversocket,Java,Loops,Client,Serversocket,我有一个关于While(对)的问题。通常会删除计算机中的CPU。为什么在这种情况下,它不经常运行?如果需要,我如何让它运行 public void startConnection() { try { client = new Socket(ip, port); out = new PrintWriter(client.getOutputStream(), true); in = new BufferedReader(new Inp

我有一个关于While(对)的问题。通常会删除计算机中的CPU。为什么在这种情况下,它不经常运行?如果需要,我如何让它运行

public void startConnection() {
       try {
           client = new Socket(ip, port);
       out = new PrintWriter(client.getOutputStream(), true);
       in = new BufferedReader(new 
InputStreamReader(client.getInputStream()));
       out.println("Connection started from, " + client.getLocalAddress());
       out.flush();
       while (true) {
           String recieve = in.readLine();
           System.out.println(recieve);

           @SuppressWarnings("resource")
        Scanner scan = new Scanner(System.in);
           String send = scan.nextLine();
           if (send != null ) {
           out.println(send);
           out.flush();
           }
       }
            } catch (Exception e) {
               System.out.println(e);
                JOptionPane.showMessageDialog(null, e.toString(), 
"Connection-Error", JOptionPane.ERROR_MESSAGE);
                System.exit(0);
        }
    }

从套接字读取是一种阻塞操作(取决于套接字的配置和调用中的参数)。在这种情况下,操作系统将阻止您,直到套接字中有可读取的内容


如果需要,您可以将套接字配置为使读取操作超时。

它在InputStream上的
读取行上阻塞了,如果需要,我如何让它运行?首先更正缩进,然后编译然后运行