简体   繁体   中英

Client-Server communication using sockets blocked at client-side

I'm writing a simple local client-server program that should do the following:

  1. Client sends his name to server ("client1")
  2. Server returns a String saying "hello client1"

The problem I'm having is that the "hello client1" message is never retrieved, and instead the program just keeps running without throwing any exceptions or errors. I'm guessing it has something to do with the readLine method that blocks the bufferedReader, but I haven't been able to find a proper solution to the problem yet. To run the program locally (see Main), I use a serverThread and a clientThread, maybe that could be part of the problem too.

NOTE: In the following code, I put a comment block on the lines that seem to cause the problem, the rest of the code runs fine:

Server:

public class Serverside implements Runnable{
    PrintStream out;
    int port;

    Serverside(int port){
        out = new PrintStream(System.out);
        this.port = port;
    }

    void start(){
        try{
            ServerSocket serverSocket = new ServerSocket(port);
            Socket connSocket = serverSocket.accept();

            BufferedReader clientInput = new BufferedReader(new InputStreamReader(connSocket.getInputStream()));
            DataOutputStream clientOutput = new DataOutputStream(connSocket.getOutputStream());

            String clientName = clientInput.readLine();
            String reply = "Hello " + clientName + "\n";

            //clientOutput.writeBytes(reply);

            clientOutput.close();
            clientInput.close();
            connSocket.close();
            serverSocket.close();
        }catch(Exception e){
            out.println("Server: An exception has been thrown: " + e.getMessage());
        }
    }

    @Override
    public void run() {
        out.println("Server running");
        start();
    }
}

Client:

public class ClientSide implements Runnable {

    String hostname;
    String clientName;
    int port;

    public ClientSide(String name, int port) {
        hostname = "localhost";
        this.port = port;
        clientName = name;
    }

    void start(){
        try{
            Socket connSocket = new Socket(hostname, port);

            BufferedReader serverInput = new BufferedReader(new InputStreamReader(connSocket.getInputStream()));
            DataOutputStream serverOutput = new DataOutputStream(connSocket.getOutputStream());

            serverOutput.writeBytes(clientName);

            //String reply = serverInput.readLine();
            //System.out.println("Server says: " + reply);

            serverOutput.close();
            serverInput.close();
            connSocket.close();
        }catch(Exception e){
            System.out.println("Client: An exception has been thrown: "  + e.getStackTrace().toString());
        }

    }

    @Override
    public void run() {
        System.out.println("Client-side running");
        start();
    }
}

Main:

public class Main {

    static final int PORT = 20000;

    void start() {
        Serverside server = new Serverside(PORT);
        ClientSide client = new ClientSide("Elias",PORT);

        Thread serverThread = new Thread(server);
        Thread clientThread = new Thread(client);

        clientThread.start();
        serverThread.start();
    }

    public static void main(String[] args) {
        new Main().start();
    }

}

You're reading lines but you aren't sending lines. You need to add a line terminator to the client name.

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