Java TCP sends the first message, then infinite wait

My Issue, based on the code below:

  • Run TCPServer.java
  • then run TCPClient.java

I expect the client to print

Server Said (1): HEY DUDE 1

Server Said (2): HEY DUDE 2

... but he just stays on HEY DUDE 1. What am I doing, this does not produce the results I want?

TCPServer.java

import java.io.*; import java.net.*; class TCPServer { public static void main (String args[]) throws Exception{ new TCPServer(); } TCPServer() throws Exception{ //create welcoming socket at port 6789 ServerSocket welcomeSocket = new ServerSocket(6789); while (true) { //block on welcoming socket for contact by a client Socket connectionSocket = welcomeSocket.accept(); // create thread for client Connection c = new Connection(connectionSocket); } } class Connection extends Thread{ Socket connectionSocket; Connection(Socket _connectionSocket){ connectionSocket = _connectionSocket; this.start(); } public void run(){ try{ //create input stream attached to socket BufferedReader inFromClient = new BufferedReader(new InputStreamReader (connectionSocket.getInputStream())); //create output stream attached to socket PrintWriter outToClient = new PrintWriter(new OutputStreamWriter(connectionSocket.getOutputStream())); //read in line from the socket String clientSentence = inFromClient.readLine(); System.out.println("Client sent: "+clientSentence); //process String capitalizedSentence = clientSentence.toUpperCase() + '\n'; //write out line to socket outToClient.print(capitalizedSentence); outToClient.flush(); }catch(Exception e){} } } } 

TCPClient.java

 import java.io.*; import java.net.*; class TCPClient { //String name=""; String host = "localhost"; int port = 6789; Socket socket = null; public static void main(String args[]) throws Exception{ TCPClient client = new TCPClient(); client.SendToServer("Hey dude 1"); System.out.println("Server Said(1): "+client.RecieveFromServer()); client.SendToServer("Hey dude 2"); System.out.println("Server Said(2): "+client.RecieveFromServer()); client.close(); } TCPClient(String _host, int _port) throws Exception{ host = _host; port = _port; socket = new Socket(host, port); } TCPClient() throws Exception{ socket = new Socket(host, port); } void SendToServer(String msg) throws Exception{ //create output stream attached to socket PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); //send msg to server outToServer.print(msg + '\n'); outToServer.flush(); } String RecieveFromServer() throws Exception{ //create input stream attached to socket BufferedReader inFromServer = new BufferedReader(new InputStreamReader (socket.getInputStream())); //read line from server String res = inFromServer.readLine(); // if connection closes on server end, this throws java.net.SocketException return res; } void close() throws IOException{ socket.close(); } } 
+4
source share
1 answer

Your server thread ends as soon as you process the first message. You need to put the server code in a loop like this:

 String clientSentence; while ((clientSentence = inFromClient.readLine()) != null) { System.out.println("Client sent: "+clientSentence); //process String capitalizedSentence = clientSentence.toUpperCase() + '\n'; //write out line to socket outToClient.print(capitalizedSentence); outToClient.flush(); } 
+6
source

Source: https://habr.com/ru/post/1481636/


All Articles