0

I have these two classes Client.java and Server.java, each containing a main method. The idea is very simple and the code in them is very similar. The objective I'm trying to achieve is opening a server that listens on a specific port using ServerSocket, then open a client and connect to the server using Socket so that I can finally send messages from client to server and reverse until I close the connection.

So far, I tried the easiest example, opened server on port 4444, then on the same PC I've opened a client to connect to the address 127.0.0.1 (localhost), on port 4444. Everything runs smooth, the connection is established successfully every time, excepting it won't send any message, either from server to client or client to server.

The final question is, what should I do to be able to exchange messages between server and client? What should I modify?

Client.java

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class Client { private static Socket client; private static BufferedReader in, console; private static PrintWriter out; private static String serverAddress; private static int port; public static void main(String[] args) { try { console = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Connect to server address (example 192.168.10.11): "); serverAddress = console.readLine(); System.out.print("Port: "); port = Integer.parseInt(console.readLine()); System.out.println("\nTrying to connect..."); client = new Socket(serverAddress, port); System.out.println("Connected to " + client.getRemoteSocketAddress()); in = new BufferedReader(new InputStreamReader(client.getInputStream())); out = new PrintWriter(client.getOutputStream()); out.write("Connected!!!!!!"); Thread input = new Thread() { public void run() { String tmp; try { while((tmp = in.readLine()) != null) { System.out.println("Server: " + tmp); } } catch (IOException e) { e.printStackTrace(); } } }; Thread output = new Thread() { public void run() { String tmp; try { while((tmp = console.readLine()) != null) { out.print(tmp); } } catch(IOException e) { e.printStackTrace(); } } }; input.start(); output.start(); while(true) { if(!input.isAlive() && !output.isAlive()) { client.close(); in.close(); console.close(); out.close(); break; } } } catch (IOException e) { e.printStackTrace(); } } } 

Server.java

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 { private static ServerSocket serverSocket; private static Socket server; private static BufferedReader in, console; private static PrintWriter out; private static int port; public static void main(String[] args) { try { console = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Open server on port (example 1234): "); port = Integer.parseInt(console.readLine()); System.out.println("Server open"); serverSocket = new ServerSocket(port); System.out.println("Waiting for a client to connect...\n"); server = serverSocket.accept(); System.out.println("Client " + server.getRemoteSocketAddress() + " connected!"); in = new BufferedReader(new InputStreamReader(server.getInputStream())); out = new PrintWriter(server.getOutputStream(), true); Thread input = new Thread() { public void run() { String tmp; try { while((tmp = in.readLine()) != null) { System.out.println("Client: " + tmp); } } catch (IOException e) { e.printStackTrace(); } } }; Thread output = new Thread() { public void run() { String tmp; try { while((tmp = console.readLine()) != null) { out.print(tmp); } } catch(IOException e) { e.printStackTrace(); } } }; input.start(); output.start(); while(true) { if(!input.isAlive() && !output.isAlive()) { serverSocket.close(); server.close(); in.close(); console.close(); out.close(); break; } } } catch (IOException e1) { e1.printStackTrace(); } } } 
2
  • Here is a tutorial that looks alot like your code. It does what you are asking for. careerbless.com/samplecodes/java/beginners/socket/… Commented Feb 6, 2018 at 0:29
  • Thanks for the reply. I already viewed many tutorials like this one. The problem is I am trying to use those 2 threads in both classes, just so input and output from server to client and reverse work simultaneously, just like any classic live chat. I managed to make it work without the threads, so most probably the problem is inside the run() methods. @Remixt Commented Feb 6, 2018 at 0:39

1 Answer 1

3
+50

You are reading lines but you aren't wring lines. Instead of out.write() and out.print() you should use out.println().

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

6 Comments

Also you have to flush the stream in this case out.flush() on each message that you want to send or PrintWriter out = new PrintWriter(client.getOutputStream(), true); on the client class as you have on the server class.
Thank you EJP for the answer, using println and flushing made it work finally. In order to mark your answer, can you please explain how using println instead of print made it work? @EJP
I've already explained. In my answer. You weren't writing lines. println() writes lines. See the Javadoc.
@HectorVenturaReyes No he doesn't The true parameter in the constructor you cited makes that automatic.
@AndreiMeși you have to options to send the data to the connected socket, first is to set to true the second parameter on the PrintWriter constructor or execute a flush after the println method. @AndreiMeși good luck.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.