'Accepting/Sending messages to one Client Simultaneously

Till now I have a Server, Client1 and a Client2. The code works all fine and you can send messages between each Clients. The output looks something like this (but note that this is not the output which I want) :

OUTPUT1

C1: Hi

C2: Hello

C1: How are you?

C2: Great!

The output which I really want it to look like is:

OUTPUT2

C1: Hi

C1: are you alright?

C2: yes

C2: all fine

C2: all I need is to get to hospital asap

C1: I can drop you

C2: no thanks

So can anyone tell me how to send messages to one particular client multiple times rather than only one time

Client1

import java.net.*;

import java.io.*;

import java.util.*;
public class Client1V1
{
   public static void main(String[] args) throws Exception
   {
      new Thread( () -> runSocket() ).start();
      runClient1();
   }
   
   public static void runSocket()
   {
      try
      {
          boolean connected1,connected2;
          System.out.println("Server is started");
          
          ServerSocket ss1 = new ServerSocket(1111);
          System.out.println("Server is waiting for the client1");
          Socket s1 = ss1.accept();connected1=true;
          System.out.println("Client1 Connected");
          
          ServerSocket ss2 = new ServerSocket(1112);
          System.out.println("Server is waiting for the client2");
          Socket s2 = ss2.accept();connected2=true;
          System.out.println("Client2 Connected");
          
          OutputStreamWriter os1,os2;
          PrintWriter out1,out2;
          BufferedReader br1,br2;
          String fromC1="",fromC2="";
          
          os2 = new OutputStreamWriter(s1.getOutputStream());
          out2 = new PrintWriter(os2);
          out2.println("Type a message to C2");
          os2.flush();
          while(true && connected1==true && connected2==true)
          {
              br1 = new BufferedReader(new InputStreamReader(s1.getInputStream()));
              fromC1 = br1.readLine();
              //System.out.println("Client1: "+fromC1);
              
              os1 = new OutputStreamWriter(s2.getOutputStream());
              out1 = new PrintWriter(os1);
              out1.println(fromC1);
              os1.flush();
              
              br2 = new BufferedReader(new InputStreamReader(s2.getInputStream()));
              fromC2 = br2.readLine();
              //System.out.println("Client2: "+fromC2);
              
              os2 = new OutputStreamWriter(s1.getOutputStream());
              out2 = new PrintWriter(os2);
              out2.println(fromC2);
              os2.flush();
          }
      }catch(Exception e){}
   }
   
   public static void runClient1() throws Exception
   {
      try{
          String ip = "localhost";
          int port = 1111;
          Socket s1 = new Socket(ip, port);
          
          System.out.println("Now Connect to C2");
          
          BufferedReader br;
          String fromC2;
          
          Scanner sc = new Scanner(System.in);
          String toC2="";
          
          OutputStreamWriter os;
          PrintWriter out;
          
          br = new BufferedReader(new InputStreamReader(s1.getInputStream()));
          fromC2 = br.readLine();
          System.out.println(fromC2);
          while(!toC2.equals("exit"))
          {  
              toC2 =  sc.nextLine();
              
              os = new OutputStreamWriter(s1.getOutputStream());
              out = new PrintWriter(os);
              out.println(toC2);
              os.flush();
              
              br = new BufferedReader(new InputStreamReader(s1.getInputStream()));
              fromC2 = br.readLine();
              System.out.println("C2: "+fromC2);
          }
      }catch(Exception e){}
   }
}

Client2

import java.net.*;
import java.io.*;
import java.util.*;
public class Client2V1
{
   public static void main(String[] args) throws Exception
   {
      runClient2();
   }
   
   public static void runClient2() throws Exception
   {
     try{
          String ip = "localhost";
          int port = 1112;
          Socket s1 = new Socket(ip, port);
          
          System.out.println("Type a message to C1");
          
          BufferedReader br;
          String fromC1;
          
          Scanner sc = new Scanner(System.in);
          String toC1="";
          
          OutputStreamWriter os;
          PrintWriter out;
          while(!toC1.equals("exit"))
          {
              br = new BufferedReader(new InputStreamReader(s1.getInputStream()));
              fromC1 = br.readLine();
              System.out.println("C1: "+fromC1);
              
              toC1 =  sc.nextLine();
              
              os = new OutputStreamWriter(s1.getOutputStream());
              out = new PrintWriter(os);
              out.println(toC1);
              os.flush(); 
          }
      }catch(Exception e){}
   }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source