有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javasocket不在while循环中发送数据

我正在尝试进行简单的网络通信,其中客户端向服务器发送用户输入字符串,然后服务器向控制台显示。 当我只发送一个字符串时,它可以正常工作,但只要我将用户输入代码打包并在while循环中发送代码,服务器就不会收到任何内容

服务器:

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(PORT);
            System.out.println("Server now hosted on port " + PORT);
            Socket s = serverSocket.accept();
            System.out.println("A client has connected !");

            BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
            BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());

            while(true){            
                //RECEIVE
                int data;
                String inString = "";
                while((data=bis.read()) != -1){
                    inString += (char)data;
                }
                System.out.println("SLAVE : " + inString);              
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Port déjà utilisé");
        }finally {
            try {
                serverSocket.close();
                System.out.println("Server closed");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Could not close port " + PORT);
            }
        }

客户:

Scanner sc = new Scanner(System.in);
        Socket s = null;

        try {
            s = new Socket("127.0.0.1", PORT);

            BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
            BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());

            System.out.println("Connexion established !");
            while(true){ // without this while loop, it works fine
                String send = "";
                System.out.print(">> ");
                send = sc.nextLine();
                bos.write(send.getBytes());
                bos.flush();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Could not connect");;
        }
        finally {
            try {
                s.close();
                System.out.println("Closing socket");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Could not close connection");;
            }
        }
        System.out.println("End of client");
    }

我希望服务器在提交时写入它从socket读取的任何数据。 但它什么也没做。 我不确定问题是来自服务器还是客户端


共 (1) 个答案

  1. # 1 楼答案

    问题在于while((data=bis.read()) != -1){代码

    它一直循环,直到收到EOS为止-1

    当您没有客户机循环时,您的流将关闭,允许发送-1,但当您有循环时则不允许。尝试使用服务器循环打印,如下所示

    while((data=bis.read()) != -1){
       inString += (char)data;
    
       if (((char)data) == '\n') {
           System.out.println("SLAVE : " + inString);   
           inString = "";
       }
    }