-1

I'm working on a basic Client-Server connection. This code works perfectly yet the client can only send 1 message and receive its modification before closing the connection. how can I make it to send and receive multiple messages?

I thought of using a while loop yet I didn't know how to implement it correctly. I need to be able to send more than 1 message in order to have a consistent connection

The code below is a client sending a string to the server and the server turns it to uppercase.

//Server:
public class TCPServer {

    public static void main(String argv[]) throws Exception 
    {
       String clientSentence;
       String capitalizedSentence;

       ServerSocket welcomeSocket = new ServerSocket(6789);
       while(true)
                    {
           Socket connectionSocket = welcomeSocket.accept(); 

           BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 
           DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); 

           clientSentence = inFromClient.readLine();

           capitalizedSentence = clientSentence.toUpperCase() + '\n';
           outToClient.writeBytes(capitalizedSentence);

           if(clientSentence.toUpperCase().trim().contentEquals("QUIT")) {
               connectionSocket.close();
               }
           }
       }
    }

//Client:
public class TCPClient {

    public static void main(String argv[]) throws Exception
    { 
        String sentence;
        String modifiedSentence;
        Socket clientSocket = new Socket("LocalHost", 6789);

        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        System.out.print("Enter characters to be capitalized: ");
        sentence = inFromUser.readLine(); 
        outToServer.writeBytes(sentence + '\n'); 
        modifiedSentence = inFromServer.readLine(); 

        System.out.println("FROM SERVER: " + modifiedSentence);
        }
    }

the output for this code is:

Enter characters to be capitalized: hi
FROM SERVER: HI
R.Bassil
  • 27
  • 1
  • 3

1 Answers1

0

Your Server can gets only one message from each client, because in your while-loop, in each iteration you call to welcomeSocket.accept(). This means that your server code stops until it gets new client connection.

Consider to use multi-threading if you want your server will support multiple clients. For example, take a look: on this post

Ofir
  • 590
  • 9
  • 19
  • I'm not trying to connect multiple users to the same server, I need to send multiple messages between the same client and server, without closing the connection. – R.Bassil Apr 27 '19 at 15:52
  • @R.Bassil, but the problem is the same as I mentioned. Each iteration your server is waiting to accept a new client. If you want to accept just one client, do this before the while-loop. – Ofir Apr 27 '19 at 16:04