Using Java create the following: 1) The chat server will accept connections from
ID: 3818943 • Letter: U
Question
Using Java create the following:
1) The chat server will accept connections from clients on port 5190 and relay anything sent to the server to all clients (including the one who originally sent the message), with the sender's name prepended to the message. For example if "Tom" sends "Hello world" the server will relay that message to all connected clients as "Tom: Hello world" The server should not produce any output on the screen. Clients, upon connecting, have to announce their username, which should NOT be forwarded to the other clients. The server should not introduce itself, to the client!
2) Design the chat client for the above server. The client will have a top box which is displays all the messages from the clients, and a bottom input which will allow text to be written to the server. Also include a "SEND" button. Upon starting the client, you should ask which server to connect to and what username to use. When the client connects to the server it sends the username as the first message. After the first message is sent, anything received from the server is displayed in the top box and anything written in the bottom box (after the send button is pushed) is sent to the server.
Use ONLY JButton, JFrame, JPanel, Sockets, Server Sockets, and Threads if needed.
Explanation / Answer
Ans.
PROGRAMS::
1. //server.java//
import java.io.*;
import java.util.*; // util
import java.net.*; // net
// server class
public class Server {
// arraylist
static ArrayList<Socket> clr =new ArrayList<>();
public static void main(String[] args) {
Clients cl = new Clients(clr); // clients
// try block
try{
ServerSocket ss = new ServerSocket(5190);
System.out.println(ss.getLocalSocketAddress());
while(true){
Socket sock = ss.accept();
System.out.println(sock.getInetAddress().getHostAddress()+" connected");
clr.add(sock);
new ProcessClient(sock,cl).start();
}
}
// catch block
catch(IOException e){
System.out.println("Caught IOException!");
}
}
}
class Clients{
static ArrayList<Socket> clients;
Clients(ArrayList<Socket> curr){
clients = curr;
}
public void write(String name,String message) throws IOException{
PrintStream ps;
for(Socket x:clients){
ps = new PrintStream(x.getOutputStream());
ps.print(name+": "+message+" ");
}
}
}
class ProcessClient extends Thread{
Clients cl;
Socket sock;
String name;
ProcessClient(Socket newSock, Clients newCl){
sock=newSock;
name = "";
cl = newCl;
}
public void run(){
try{
Scanner sin = new Scanner(sock.getInputStream());
PrintStream sout = new PrintStream(sock.getOutputStream());
while (!sock.isClosed()){
String line = sin.nextLine();
if(name.equals("")){
// System.out.println(sock.getInetAddress()+"has selected name: "+line);
name = line;
}
else{
cl.write(name,line);
// System.out.println(name+": "+line);
}
if (line.equalsIgnoreCase("EXIT"))
sock.close();
}
}
catch(IOException e){}
System.out.println(sock.getInetAddress().getHostAddress()+" disconnected");
}
}
2.///client.java///
import java.util.*; // util
import java.io.*; // io
import java.net.*; // net
import java.awt.*; // awt
import java.awt.event.*; // event
import javax.swing.*; // swing
// client class
public class Client {
static JFrame jf; // JFrame jf
// Jpanel jp1,2,3
static JPanel jp1;
static JPanel jp2;
static JPanel jp3;
// jp_button
static JButton jb;
static JTextField jt1; // JTextfield jt1
static JLabel jl1; // JLabel
static JTextField jt2; // testfield 2
static JLabel jl2; //
static JTextField jt3; // JTextfield jt3
static JTextArea jt4; JTextfield jt4
static JLabel jl3; // JLabel
static JLabel jl;
static JScrollPane scroll ;
static String address = "";
static String name = null;
static String curr = "";
public static void main(String[] args) {
jf = new JFrame("ChatBox");
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jp1.setBackground(Color.WHITE);
jp2.setBackground(Color.WHITE);
jp3.setBackground(Color.WHITE);
jp3.setBorder(BorderFactory.createLineBorder (Color.black));
jl = new JLabel("");
jl.setSize(400, 400);
jl1 = new JLabel("IP Address");
jl2 = new JLabel("User Name");
jl3 = new JLabel("Enter the Message:");
jb = new JButton("SEND");
jt1 = new JTextField("");
jt4 = new JTextArea("CHAT BOX");
jt4.setColumns(50);
jt4.setAlignmentY(400);
jt4.setEditable(false);
jt1.addActionListener(new IP());
jt1.setColumns(10);
jt1.setToolTipText("Enter Server address: ");
jt2 = new JTextField("");
jt2.setToolTipText("Enter your username");
jt2.setColumns(10);
jt3 = new JTextField("");
jt3.setToolTipText("Enter your message");
jt3.setColumns(20);
jt3.setEditable(false);
jp1.add(jl1, BorderLayout.PAGE_START);
jp1.add(jt1, BorderLayout.PAGE_START);
jp1.add(jl2, BorderLayout.PAGE_START);
jp1.add(jt2, BorderLayout.PAGE_END);
jp3.add(jl, BorderLayout.PAGE_START);
jp3.add(jt4, BorderLayout.CENTER);
jp2.add(jl3, BorderLayout.LINE_START);
jp2.add(jt3, BorderLayout.CENTER);
jp2.add(jb, BorderLayout.EAST);
scroll = new JScrollPane (jt4);
jf.setSize(800,800);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(jp1, BorderLayout.PAGE_START);
jf.add(jp3, BorderLayout.CENTER);
jf.add(jp2, BorderLayout.SOUTH);
jf.add(scroll);
jf.setVisible(true);
while(address.equals("")){System.out.print("");}
System.out.println("Yes, it is working");
// try block
try{
Socket s = new Socket(address,5190);
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
jt2.addActionListener(new Name(out));
jb.addActionListener(new Message(out));
jt3.addActionListener(new Message(out));
new Read(s).start();
}
// catch block
catch(IOException e){
System.out.println("Connection to the goo_gle has failed. :(");
}
}
static class IP implements ActionListener{
@Override
public void actionPerformed(ActionEvent ae){
JTextField jt = (JTextField) ae.getSource();
address = jt.getText();
jt.setEditable(false);
}
}
static class Name implements ActionListener{
PrintWriter out;
Name(PrintWriter newOut){out = newOut;}
@Override
public void actionPerformed(ActionEvent ae){
JTextField jt = (JTextField) ae.getSource();
name = jt.getText();
out.println(name);
jt3.setEditable(true);
jt.setEditable(false);
}
}
static class Message implements ActionListener{
//Socket s;
PrintWriter out;
//Message(Socket newS){s = newS;}
Message(PrintWriter newOut){out = newOut;}
@Override
public void actionPerformed(ActionEvent ae){
String line = jt3.getText();
out.println(line);
jt3.setText("");
}
}
static class Read extends Thread{
Socket s;
Read(Socket newS){s = newS;}
synchronized public void run(){
Scanner sin;
curr = jt4.getText();
try {
sin = new Scanner(s.getInputStream());
while(!s.isClosed()){
while(sin.hasNext()){
curr =curr+" "+sin.nextLine();
jt4.setText(curr);
}
}
} catch (IOException ex) {
System.out.println("EXIT");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.