有 Java 编程相关的问题?

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

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

我正在开发一个密码验证应用程序。为了进行测试,我想从键盘向服务器发送一个示例ID,服务器将向该服务器确认收到了该示例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");
    }
 }
}

共 (1) 个答案

  1. # 1 楼答案

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

    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();
            }
        }
    }
    

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