简体   繁体   中英

Server doesn"t receive a request from the client

I'm working on a password authentication application. For testing, I wanted to send a sample ID from the keyboard to the server to which the server would confirm that it received. However, when I send a request, the server does not respond. Where is the mistake?

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

I've made few changes in your code, if you don't mind. Main change is that better to use DataInputStream/DataOutputStream . I don't know where the problem with PrintWriter , but one of the problems is there, and I don't sure where. If you want - you are free to find it out. The second problem is that you print that you wrote something to Socket, but in fact you don't:

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

You're trying to read a value before actual sending to the client 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();
        }
    }
}

Hope this will help you and you'll use my changes to find errors in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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