Note that there are some explanatory texts on larger screens.

plurals
  1. POPrintWriter in Java Socket program
    primarykey
    data
    text
    <p>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.</p> <p>Client:</p> <pre><code>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); } </code></pre> <p>Server:</p> <pre><code>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(); } </code></pre> <p>I've tested for user input from Client program and its getting it correctly, but the problem is if I move</p> <pre><code>print.println("You're connected to the Server at: " + dateTime); </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload