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

Write a chat Program using the Client and Server programs that I have attached h

ID: 641651 • Letter: W

Question

Write a chat Program using the Client and Server programs that I have attached here. We went through these programs in our CS990 class and I explained how they work.

All you have to do is build a GUI over these two programs. I should be able to run these two programs on a same computer in two different DOS windows.

First run the Server program in a dos window, which starts listening to a connection and opens a GUI window.

Then run Client program on a separate DOS window. this should connect to the server program and open another GUI window.

I should be able to chat between the two windows. Whatever I type in Server GUI window should appear in the client, and whatever I type in the client GUI window should appear in the server GUI window.

client file


import java.io.*;
import java.net.*;
import java.lang.*;

public class Server
{
   public static void main(String[] args) throws IOException
   {
final int port = 444;
System.out.println("Server waiting for connection on port " + port);
ServerSocket ss = new ServerSocket(port);
Socket clientSocket = ss.accept();
System.out.println("Recieved connection from " + clientSocket.getInetAddress() + " on port "
+ clientSocket.getPort());
// create two threads to send and recieve from client
RecieveFromClientThread recieve = new RecieveFromClientThread(clientSocket);
   Thread thread = new Thread(recieve);
thread.start();
SendToClientThread send = new SendToClientThread(clientSocket);
Thread thread2 = new Thread(send);
thread2.start();
   }
}

class RecieveFromClientThread implements Runnable
{
   Socket clientSocket = null;
   BufferedReader brBufferedReader = null;

   public RecieveFromClientThread(Socket clientSocket)
   {
this.clientSocket = clientSocket;
   }// end constructor

   public void run()
   {
try
{
   brBufferedReader = new BufferedReader(new InputStreamReader(
   this.clientSocket.getInputStream()));
   String messageString;
   while (true)
   {
while ((messageString = brBufferedReader.readLine()) != null)
{// assign
// message
// from
// client
// to
// messageString
   if (messageString.equals("EXIT"))
   {
break;// break to close socket if EXIT
   }
   System.out.println("From Client: " + messageString);// print
// the
// message
// from
// client
   System.out.println("Please enter something to send back to client..");
}
this.clientSocket.close();
System.exit(0);
   }
} catch (Exception ex)
{
   System.out.println(ex.getMessage());
}
   }
}// end class RecieveFromClientThread

class SendToClientThread implements Runnable
{
   PrintWriter pwPrintWriter;
   Socket clientSock = null;

   public SendToClientThread(Socket clientSock)
   {
this.clientSock = clientSock;
   }

   public void run()
   {
try
{
   pwPrintWriter = new PrintWriter(new OutputStreamWriter(
   this.clientSock.getOutputStream()));// get outputstream
   while (true) {
String msgToClientString = null;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));// get userinput
msgToClientString = input.readLine();// get message to send to
// client
pwPrintWriter.println(msgToClientString);// send message to
   // client with
   // PrintWriter
pwPrintWriter.flush();// flush the PrintWriter
System.out.println("Please enter something to send back to client..");
   }// end while
} catch (Exception ex)
{
   System.out.println(ex.getMessage());
}
   }// end run
}// end class SendToClientThread

server file


import java.io.*;
import java.net.*;
import java.lang.*;

public class Server
{
   public static void main(String[] args) throws IOException
   {
final int port = 444;
System.out.println("Server waiting for connection on port " + port);
ServerSocket ss = new ServerSocket(port);
Socket clientSocket = ss.accept();
System.out.println("Recieved connection from " + clientSocket.getInetAddress() + " on port "
+ clientSocket.getPort());
// create two threads to send and recieve from client
RecieveFromClientThread recieve = new RecieveFromClientThread(clientSocket);
   Thread thread = new Thread(recieve);
thread.start();
SendToClientThread send = new SendToClientThread(clientSocket);
Thread thread2 = new Thread(send);
thread2.start();
   }
}

class RecieveFromClientThread implements Runnable
{
   Socket clientSocket = null;
   BufferedReader brBufferedReader = null;

   public RecieveFromClientThread(Socket clientSocket)
   {
this.clientSocket = clientSocket;
   }// end constructor

   public void run()
   {
try
{
   brBufferedReader = new BufferedReader(new InputStreamReader(
   this.clientSocket.getInputStream()));
   String messageString;
   while (true)
   {
while ((messageString = brBufferedReader.readLine()) != null)
{// assign
// message
// from
// client
// to
// messageString
   if (messageString.equals("EXIT"))
   {
break;// break to close socket if EXIT
   }
   System.out.println("From Client: " + messageString);// print
// the
// message
// from
// client
   System.out.println("Please enter something to send back to client..");
}
this.clientSocket.close();
System.exit(0);
   }
} catch (Exception ex)
{
   System.out.println(ex.getMessage());
}
   }
}// end class RecieveFromClientThread

class SendToClientThread implements Runnable
{
   PrintWriter pwPrintWriter;
   Socket clientSock = null;

   public SendToClientThread(Socket clientSock)
   {
this.clientSock = clientSock;
   }

   public void run()
   {
try
{
   pwPrintWriter = new PrintWriter(new OutputStreamWriter(
   this.clientSock.getOutputStream()));// get outputstream
   while (true) {
String msgToClientString = null;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));// get userinput
msgToClientString = input.readLine();// get message to send to
// client
pwPrintWriter.println(msgToClientString);// send message to
   // client with
   // PrintWriter
pwPrintWriter.flush();// flush the PrintWriter
System.out.println("Please enter something to send back to client..");
   }// end while
} catch (Exception ex)
{
   System.out.println(ex.getMessage());
}
   }// end run
}// end class SendToClientThread

Explanation / Answer

I have a Client Server Chat program that will solve your purpose.

Program 2:

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