如何从java中获取服务器TCP的回复

时间:2015-12-01 14:32:38

标签: java tcp bufferedwriter

我的代码只是做一个简单的任务,从客户端的控制台向服务器发送文本并收到回复。但是我的代码不起作用。我一直向服务器发送文本,没有回复回复。我做了几个例子,加上客户提供的2号码。我这样做的方式相同,但我无法弄清问题是什么。

服务器:

public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(8);
            Socket client = server.accept();
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
            BufferedWriter outToClient = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
            String in = inFromClient.readLine(),out;
            while(in!=null){
                out = in+" from server";
                outToClient.write(out);
                outToClient.newLine();
                outToClient.flush();
            }
            inFromClient.close();
            outToClient.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

客户端:

public class Client {
    public static void main(String[] args) {
        try {
            Socket client = new Socket("localhost", 8);
            System.out.println("Connected to server");
            BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
            BufferedWriter outToServer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
            Scanner input = new Scanner(System.in);
            String strClient,strServer;
            while(true){
                System.out.print("Client: ");
                strClient = input.nextLine();
                outToServer.write(strClient);
                strServer = inFromServer.readLine();
                System.out.print("Server: ");
                System.out.println(strServer);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

1 个答案:

答案 0 :(得分:2)

您的代码存在一些问题:

  1. 您的服务器希望读取一行,而您只是在没有换行符号的情况下编写文本:
  2. 使用以下内容读取服务器中的行:inFromClient.readLine()

    在客户端编写没有换行符的文本:outToServer.write(strClient); 将其更改为outToServer.write(strClient + "\n");

    1. 你不要冲洗客户的作者。在行outToServer.flush();
    2. 之后添加outToServer.write(...);
      1. 您只在服务器中读取1行,并且不再在循环中读取。
      2. 编辑:为了方便起见,我会在此处发布更正后的代码:(我已尝试过它,它就像魅力一样)

        客户端:

        public class Client {
            public static void main(String[] args) {
                try (Socket client = new Socket("localhost", 8);
                    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    BufferedWriter outToServer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
                    Scanner input = new Scanner(System.in)) {
        
                    System.out.println("Connected to server");
                    String strClient,strServer;
        
                    while(true){
                        System.out.print("Client: ");
                        strClient = input.nextLine();
                        outToServer.write(strClient);
                        outToServer.newLine();
                        outToServer.flush();
                        strServer = inFromServer.readLine();
                        System.out.println("Server: " + strServer);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        

        服务器:

        public class Server {
            public static void main(String[] args) {
                try (ServerSocket server = new ServerSocket(8);
                    Socket client = server.accept();
                    BufferedReader inFromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    BufferedWriter outToClient = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()))) {
        
                    String in = inFromClient.readLine(), out;
                    while(in != null){
                        out = in + " from server";
                        outToClient.write(out);
                        outToClient.newLine();
                        outToClient.flush();
                        in = inFromClient.readLine();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        

        请注意,此解决方案使用Javas ARM(自动资源管理)来自动关闭流和套接字。所以这在java 1.7之前不起作用!