gpt4 book ai didi

java - 我如何从java中的服务器TCP获得回复

转载 作者:可可西里 更新时间:2023-11-01 02:48:38 27 4
gpt4 key购买 nike

我的代码只是做一个简单的任务,从客户端的控制台发送文本到服务器并接收回复。但是我的代码不起作用。我一直向服务器发送文本,但没有回复发回。我做了几个例子,加上客户给的 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. 您的服务器期望读取一行,而您只写了没有换行符的文本:

读取服务器中的一行:inFromClient.readLine()

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


  1. 您不刷新客户的作者。在 outToServer.write(...);
  2. 行之后添加 outToServer.flush();

  1. 您只读取服务器中的 1 行并且不再在循环中读取。

编辑:为了方便起见,我将在此处发布更正后的代码:(我已经尝试过了,效果非常好)

客户:

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 之前是行不通的!

关于java - 我如何从java中的服务器TCP获得回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34022874/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com