2

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

}

}

2
  • Try to print every line your server receives from the client to the console. This should make debugging a lot easier. Commented May 11, 2014 at 13:55
  • @ruediste Don't know why I didn't think of this - just tried it (and amended my code) and nothing is printing out Commented May 11, 2014 at 14:01

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Worked a charm! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.