簡體   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