Nick
Nick

Reputation: 597

PrintWriter in Java Socket program

I'm writing a program that has 2 way communication using Sockets. Client Program presents user with some options, when user selects that option it gets processed by Server Program and returns results back to Client Program. I am having some trouble with Server Program because it won't process the PrintWriter statement if I put it inside my conditional statement as you can see bellow.

Client:

Socket socket = null;

    try
    {
        System.out.println("Connecting to Server");

        socket = new Socket("192.168.0.104", 7003);
        socket.setSoTimeout(10000);
        System.out.println("Connected");

        DataOutputStream os = new DataOutputStream(socket.getOutputStream());

        InputStreamReader userInput = new InputStreamReader(System.in);
        BufferedReader userBuffer = new BufferedReader(userInput);

        InputStreamReader serverInput = new InputStreamReader(socket.getInputStream());
        BufferedReader serverBuffer = new BufferedReader(serverInput);
        PrintWriter print = new PrintWriter(socket.getOutputStream(), true);

        System.out.println("Option 1");
        System.out.println("Option 2");
        System.out.println("Option 3");
        String userOption = userBuffer.readLine();

        os.writeBytes(userOption);

        if (userOption.equals("1"))
        {
            String line = serverBuffer.readLine();              
            System.out.println(line);               
        }

        System.out.println("Closing Client Connection");
        serverBuffer.close();
        serverInput.close();
        print.close();
        socket.close();
        os.close();
        System.exit(0);

    }

Server:

ServerSocket serverSock = null;
    Socket standSock = null;

    try
    {
        serverSock = new ServerSocket(7003);
        standSock = serverSock.accept();

        InputStreamReader input = new InputStreamReader(standSock.getInputStream());
        BufferedReader read = new BufferedReader(input);
        PrintWriter print = new PrintWriter(standSock.getOutputStream(), false);

        String dateTime = (Calendar.getInstance()).getTime().toString();

    if (read.readLine().equals("1"))
    {
        System.out.println("Option 1");
        print.println("You're connected to the Server at: " + dateTime);
    }

        System.out.println("Closing Server Connection");
        read.close();
        input.close();
        print.close();
        standSock.close();

    }

I've tested for user input from Client program and its getting it correctly, but the problem is if I move

print.println("You're connected to the Server at: " + dateTime);

inside the condition statement the program just hangs and eventually times out, but if I move that statement outside the condition statement it works. Why can't it work if I put it inside my condition statement?

Upvotes: 0

Views: 6880

Answers (2)

Chad Campbell
Chad Campbell

Reputation: 321

The problem is that DataOutputStream only really sends the information after a newline or when you close the stream, however closing the stream will also close the socket.

I would recommend sticking with PrintWriter as it is much easier to manage, especially when in the constructor you can make the second argument true so it "auto-flushes" so whenever you do a println() on that PrintWriter object it will automatically send the information.

Upvotes: 1

timotep
timotep

Reputation: 43

The os.writeBytes(userOption); in Client never actually writes a return. You are calling readline() on the server so it waits until you type in a return. The readline() you are calling from System.in actually discards the return you type in so that is never transmitted. Adding + \n" to os.writeBytes(userOption); should do the trick.

Probably a better option would be to actually use the PrintWriter you are creating in the Client, which will automatically flush the StreamBuffer after calling print.println(userOption). And it requires no extra \n.

Upvotes: 2

Related Questions