Simple Java Echo Server Problem

I am trying to write my first socket server, so I decided to start with something very simple, just to understand the flow :) I am writing a simple echo java server, but the thing is (for some reason ?!) I do not get the server response in the client, although the request is received on the server.

package poc.client; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class Client { public static void main(String[] args) { try { final Socket socket = new Socket((String) null, 50000); final BufferedReader reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); final PrintWriter writer = new PrintWriter( socket.getOutputStream(), true); writer.println("ala bala\r\n"); writer.flush(); writer.close(); System.out.println(reader.readLine()); System.out.flush(); } catch (Exception ex) { Logger.getAnonymouseLogger().throwing(TAG, "main", ex); } } } 

And the server side

 package poc.server; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Server { /** * debugging purposes only */ @SuppressWarnings("unused") private static final String TAG = Server.class.getSimpleName(); public static void main(String[] args) { try { final ServerSocket socket = new ServerSocket(50000); while (true) { final Socket clientSocket = socket.accept(); final BufferedReader reader = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); final PrintWriter writer = new PrintWriter(clientSocket .getOutputStream(), true); writer.println(reader.readLine()); writer.flush(); writer.close(); } } catch (IOException e) { Logger.getAnonymouseLogger().throwins(TAG, "main",ex); } } } 

I have read all the tutorial lessons for the base Oracle / etc socket, but I just can’t understand what happened - I am successfully writing to the server socket, but I seem to be unable to get the answer.

+4
source share
2 answers

Do not cover the device before reading from the slot. Code Work

 final Socket socket = new Socket((String) null, 50000); final BufferedReader reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); final PrintWriter writer = new PrintWriter( socket.getOutputStream(), true); writer.println("ala bala\r\n"); System.out.println(reader.readLine()); //writer.flush(); writer.close(); 

According to the javadoc method of close () method:

Closes the thread and frees up any system resources associated with this. Closing a previously closed stream is not affected.

It seems that if you close the stream, the main socket also closes. You can verify this by printing a stack trace in the current code. It gives the error java.net.SocketException: socket closed .

+2
source

Try this instead:

 Socket socket = new Socket(InetAddress.getLocalHost(), 50000); 

Also note that there is no need for writer.flush(); , since you are making it an auto-reset thread when you pass true to the second parameter of the PrintWriter constructor.

0
source

All Articles