繁体   English   中英

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

[英]Server doesn"t receive a request from the client

我正在开发密码验证应用程序。 为了测试,我想从键盘向服务器发送一个样本 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();
        }
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM