Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I heve these 2 codes Server and Cleint. How do i make the Server send a m

ID: 652887 • Letter: H

Question

Hello,

I heve these 2 codes Server and Cleint. How do i make the Server send a message to the client?

please help

Server

import java.net.*;
import java.io.*;
import java.util.*;

public class ServerList{
public static void main(String [] args){
new ServerList();
}//main end
public ServerList(){
  
try{
ServerSocket ss=new ServerSocket(16789);
  
Socket s = null;
while(true){
s = ss.accept();// wait client to connect
ThreadServer th = new ThreadServer(s);
th.start();
}//end while

}//end try

catch(Exception e){
System.out.println("Error in connection");

}//end catch

}//end constactur

class ThreadServer extends Thread{
  
Socket s = null;
BufferedReader br;
PrintWriter bw;
ArrayListtext=new ArrayList();
public ThreadServer(Socket _s)
{
this.s = _s;
  
}//end constactur
  
  
public void run(){
try{
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
bw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
String ClientMsg =br.readLine();


while(! ClientMsg.equalsIgnoreCase( "DONE" )){


System.out.println("Received : " + ClientMsg);
bw.println(ClientMsg);
bw.flush(); text.add(ClientMsg);
ClientMsg =br.readLine();
}//end while
  
System.out.println("Fiished with the client");


  

// // for lop to send the lest
// for(int i=0 ;i // { System.out.println(text.get(i));
// bw.println(text.get(i));
// bw.flush();
// }//end forloob
//






  


//close
s.close();
br.close();
bw.close();
  
  
  

  
}//end try

  

catch(Exception e){
System.out.println("Error.");
}//end catch
  
  
  
}//end run
  
  
  
  
}//end class ThreadServer

}//end ServerList class

****************************************************************************

Client


import java.net.*;
import java.io.*;
import java.util.*;

public class ClientList{
public static void main(String [] args){
try{
Socket s =new Socket("localhost",16789);
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter bw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
  

for(String text:args){
bw.println(text);
bw.flush();
String msg = br.readLine();
System.out.println("SENDING "+msg);
  
}//end for
  
bw.println("DONE");
bw.flush();
  
  
  
// for(String text:args){
//   
// String mssg=br.readLine();
// System.out.println(mssg);
// }
s.close();
  
  
}//end try
  

catch(Exception e){
System.out.println("Error....");

}//end catch

}//end main
  
  


}//class ClientList end

Explanation / Answer

After completing complie the program, and it should give output as follows.

java GreetingClient localhost

Connecting to localhost on port

Just connected to localhost

Server says Thank you for connecting

Goodbye!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote