Question

I'm trying to test the Client/Server tutorial example provided by Oracle, with a couple of minor adjustments.

Ideally, I would write "print stuff" into my BufferedReader, stdIn, in my client, and the server, upon receiving this string, would print out "Client and server connected!".

My code compiles, and it appears that the connection is successful. However, my server is printing nothing at this point.

The following is my Client code:

public class myClient {

public static void main(String[] args) throws IOException {

    String hostName = // my computer's local host name;
    int portNumber = 4444;

    try ( 
        Socket clientSocket = new Socket(hostName, portNumber);
        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    ) {
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

        // Tell server to print line "Client and server have connected!";
        String fromServer;
        String fromUser;

        while ((fromServer = in.readLine()) != null) {
            if (fromServer.equals("End"))
                break;

            fromUser = stdIn.readLine();

            if (fromUser != null)
                out.println(fromUser);
        }

    } catch (UnknownHostException e) {
        System.err.println("Don't know host!");
        System.exit(1);
    } catch (IOException e) {
        System.err.println("IO Exception caught!");
        System.exit(1);
    }

}

}

The following is my server code:

public class myServer {

public static void main(String[] args) throws IOException {

    int portNumber = 4444;

    try (
        ServerSocket serverSocket = new ServerSocket(portNumber);
        Socket clientSocket = serverSocket.accept();

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    ) {

        // if receieved string is "print stuff", print "Client and server have connected!";
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            if (inputLine.equals("End"))
                break;

            // try to echo input stream - no output!
            out.println(inputLine);

            if (inputLine.equals("print stuff"))
                out.println("Client and server have connected!");
        }

    } catch(IOException e) {
        System.err.println("Exception caught while trying to listen to port!");
    }

}

}

Was it helpful?

Solution

The first bug is that your client waits for input from the server after connecting, but the server does not send anything. Thus your user input loop is never executed.

The second bug is that, as long as you do not enter "print stuff", the server does not respond anything to the client. So again the client waits for input from the server, instad of waiting for input from the user.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top