Write the code in Java Language. please refer the below image for reference. Des
ID: 3846788 • Letter: W
Question
Write the code in Java Language. please refer the below image for reference.
Design and implement a simple chat system with
One server (multi-threaded) – multi clients
A client join or leave, notify all connected clients
A client send a message, broadcast the message to all clients
Client-side requirements (basic)
Ask the username of the user at the begin and notify the username to the server
Maintain a menu selection for the user:
Exit the system (leave the chat)
Get the list of users
User can type a chat message to send at any time
When data comes, display the data accordingly
Server-side requirements (basic)
Maintain the list of connected clients together with its username
Broadcast data to on connected clients, when
A new client joins (with the username)
A client sends chat message
A client exits the system (by the user)
A client loses its connection to the server
Terminal erjava x basic c ba .html x java Multi ThreadChatserver java 06/66/2017 16:26 seed@ubuntu: S telnet 192.168. 56.101 2222 entered tne Cnat room Trying 192.168.56. 101. Connected to 192.168.56.101. Escape character 1s Terminal Enter your name H1na [06/06/2017 16:26] seed@ubuntu S cd SIAD/Assignnent2 [06/66/2017 16:26] seed@ubuntu /SIAD/Assignnent2$ j avac MultiThreadchatserver. Hello Hina to our chat roon To leave enter /quit in a new line Note Multt uses or overrides a deprecated API ThreadChatserver h1. Note Recorptle with t: deprecatton for details H1na> ht Ce6/e6/2017 16:27 seed@ubuntu j ava MuuttThreadchatserver A new user Leela entered the chat room usage ava Muutt chatserver sportNumber Thread Leela> H1 Htna Now using port number 2222Explanation / Answer
// CHAT CLIENT
import java.net.*;
import java.io.*;
public class chatclient
{
public static void main(String args[])throws IOException
{
try
{
DatagramSocket s=new DatagramSocket();
InetAddress add= InetAddress.getLocalHost();
byte outbuffer[]=new byte[1024];
byte inbuffer[]=new byte[1024];
String data,x,str;
DataInputStream in =new DataInputStream(System.in);
System.out.println("Client window");
do
{
DatagramPacket indata=new DatagramPacket(inbuffer,inbuffer.length);
System.out.println("to server");
str=in.readLine();
outbuffer=str.getBytes();
DatagramPacket outdata=new DatagramPacket(outbuffer,outbuffer.length,add,2000);
s.send(outdata);
s.receive(indata);
data=new String(indata.getData()).trim();
System.out.println("From Server:"+data);
}
while(!((data.equals("stop")||str.equals("stop"))));
}
catch(IOException e)
{
System.out.println(e);
}
}
}
//CHAT SERVER
import java.net.*;
import java.io.*;
public class chatserver
{
public static void main(String as[])throws IOException
{
try
{
DatagramSocket s=new DatagramSocket(2000);
byte outbuffer[]=new byte[1024];
byte inbuffer[]=new byte[1024];
String data,x,str;
DataInputStream in =new DataInputStream(System.in);
System.out.println("Server window");
do
{
x=" ";
DatagramPacket indata=new DatagramPacket(inbuffer,inbuffer.length);
s.receive(indata);
InetAddress add=indata.getAddress();
int port=indata.getPort();
data=new String(indata.getData()).trim();
System.out.println("From Client:"+data);
System.out.println("To client ");
str=in.readLine();
outbuffer=str.getBytes();
DatagramPacket outdata=new DatagramPacket(outbuffer,outbuffer.length,add,port);
s.send(outdata);
}
while(!((data.equals("stop")||(str.equals("stop")))));
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.