套接字不在while循环中发送数据

时间:2019-04-24 06:20:08

标签: java sockets networking

我正在尝试进行简单的网络通信,其中客户端将用户输入的字符串发送到服务器,然后该服务器显示在控制台上。 当我只发送一个字符串时,它可以正常工作,但是当我包装我的用户输入代码并在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");
    }

我希望服务器在写入数据时会写入它正在从套接字读取的任何数据。 但是它什么也没做。 我不确定自己是服务器还是客户端来此问题。

1 个答案:

答案 0 :(得分:3)

问题出在您的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 = "";
   }
}