gpt4 book ai didi

java - 服务器没有收到客户端的请求

转载 作者:行者123 更新时间:2023-12-01 22:52:12 25 4
gpt4 key购买 nike

我正在开发密码身份验证应用程序。为了进行测试,我想将一个示例 ID 从键盘发送到服务器,服务器将确认它已收到。但是,当我发送请求时,服务器没有响应。哪里错了?

public class Client {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
try {
Socket skt = new Socket("localhost", 2011);
BufferedReader br = new BufferedReader(
new InputStreamReader(skt.getInputStream()));
System.out.print("Received: " + br.readLine());
PrintWriter pw =
new PrintWriter(skt.getOutputStream(), true);
int key_in=scanner.nextInt();
System.out.println("Sent: " +key_in);
pw.print(key_in);
pw.close();
br.close();
}
catch(Exception e) {
System.out.print("Error");
}
}
}

public class Server {
public static void main(String args[]) {
String input = "Enter ID";
ArrayList<String> passwords = new ArrayList<String>();
passwords.add("password1");
passwords.add("password2");
passwords.add("password3");
try {
ServerSocket srvr = new ServerSocket(2011);
System.out.println("Server running...");
Socket skt = srvr.accept();
System.out.println("Client connected");
PrintWriter pw =
new PrintWriter(skt.getOutputStream(), true);
System.out.println("Sent: " + input);
BufferedReader br = new BufferedReader(
new InputStreamReader(skt.getInputStream()));
System.out.println("Received:" + br.readLine());
pw.print(input);
pw.close();
br.close();
skt.close();
srvr.close();
}
catch(Exception e) {
System.out.println("Error");
}
}
}

最佳答案

如果您不介意的话,我对您的代码做了一些更改。主要变化是更好地使用 DataInputStream/DataOutputStream。我不知道 PrintWriter 的问题出在哪里,但是其中一个问题就在那里,而且我不确定在哪里。如果你愿意 - 你可以自由地找到它。第二个问题是你打印出你向 Socket 写了一些东西,但实际上你没有:

System.out.println("Sent: " + input); // input actually wasn't sent 
System.out.println("Received:" + br.readLine()); // read to answer even though input wasn't sent yet
pw.print(input); // actual sending of the input

您尝试在实际向客户端发送输入之前读取值。

public class Server {
public static void main(String[] args) {
String input = "Enter ID";
ArrayList<String> passwords = new ArrayList<>();
passwords.add("password1");
passwords.add("password2");
passwords.add("password3");
try {
ServerSocket srvr = new ServerSocket(2011);
System.out.println("Server running...");
Socket skt = srvr.accept();
System.out.println("Client connected");

DataOutputStream pw = new DataOutputStream(skt.getOutputStream());
DataInputStream br = new DataInputStream(skt.getInputStream());

System.out.println("Sent: " + input);
pw.writeUTF(input);
pw.flush();

System.out.println("Received:" + br.readUTF());

pw.close();
br.close();
skt.close();
srvr.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}


public class Client {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
Socket skt = new Socket("localhost", 2011);
DataInputStream br = new DataInputStream(skt.getInputStream());
DataOutputStream pw = new DataOutputStream(skt.getOutputStream());
System.out.print("Received: " + br.readUTF());
int key_in=scanner.nextInt();
System.out.println("Sent: " +key_in);
pw.writeInt(key_in);
pw.close();
br.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}

希望这会对您有所帮助,并且您将使用我的更改来查找代码中的错误。

关于java - 服务器没有收到客户端的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58454025/

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