I need help with this Java assignment. Thanks Create a chat server-client pair w
ID: 3838845 • Letter: I
Question
I need help with this Java assignment. Thanks
Create a chat server-client pair with a GUI that will allow users to send private plain-text messages, and private encrypted messages to a user of their choice as well as group messages to all connected clients. Create a group chat server that clients will connect to. The default behavior when a message is sent from a client is that it goes through the server and the server sends the message to ALL connected recipients. The client should have the option of sending a private message, by specifying who they want to send a message to. The client should be able to send an encrypted private message using a OneTimePad, by specifying who they want to send a message to and that they intend to encrypt it. (Messages get encrypted on the originating client's end and decrypted on the receiving client's end)Explanation / Answer
program for group chat (plain text):
1.
IndividualClientHandler.java
import java.io.*;
import java.net.*;
import java.util.*;
public class IndividualClientHandler extends Thread
{
BufferedReader server_inbox;
Vector otherclients;
public IndividualClientHandler(BufferedReader b,Vector v)
{
server_inbox=b;
otherclients=v;
}
public void run()
{
try
{
fire();
}
catch(Exception e)
{
if((e.getMessage()) . equals ("connection reset"))
{
System.out.println("one client dis-connected from the conference");
}
}
}
public void fire() throws Exception
{
while(true)
{
String s=server_inbox.readLine();
System.out.println("message received:"+s);
for(int i=0;i<otherclients.size();i++)
{
Socket client =(Socket)otherclients.elementAt(i);
if(client!=null)
{
PrintWriter client_outbox =new PrintWriter(client.getOutputStream());
client_outbox.println(s);
client_outbox.flush();
}
}
}
}
}
2.
PostingServer.java
import java.io.*;
import java.net.*;
import java.util.*;
public class PostingServer
{
public static void main(String args[])
{
try
{
Vector clients=new Vector();
ServerSocket Server=new ServerSocket(5678);
System.out.println("server started waiting for clients requests");
while(true)
{
Socket requesting_client=server.accept();
clients.addElement(requesting_client);
IndividualClientHandler ich=new IndividualClientHandler(new BufferedReader
(new InputStream Reader(requesting_client.getInputstream())),clients);
ich.start();
System.out.println("new client connected:"+requesting_client.toString());
}
}
catch(Exception e)
{
if(e.getMessage()).equals("connection reset"))
{
System.out.println("one client disconnected from the conference...");
}
}
}}
3.
ChatClient.java
import java.net.*;
public class ChatClient
{
public static void main(String args[])
{
try
{
Socket s=new Socket("172.16.0.69",5678);
ChatFrame cf=new ChatFrame(args[0],s);
cf.fire();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
4.
ChatFrame.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ChatFrame extends JFrame implements ActionListener
{
BufferedReader br;
PrintWriter pw;
JTextField tf;
JTextArea ta;
JScrollPane sp;
JButton b;
String title;
public ChatFrame(String name, Socket s) throws Exception {
JPanel p=new JPanel();
ta=new JTextArea();
ta.setEditable(false);
sp=new JScrollPane(ta);
tf=new JTextField(20);
b=new JButton("send");
b.addActionListener(this);
title=name;
p.setLayout(new GridLayout(2,1));
p.add(sp);
JPanel p1=new JPanel();
p1.add(tf);
p1.add(b);
p.add(p1);
add(p);
setTitle(name);
setSize(400,200);
setVisible(true);
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
pw=new PrintWriter(s.getOutputStream());
}
public void fire() throws Exception
{
while(true)
{
String s=br.readLine();
ta.append(" "+s);
}
}
public void actionPerformed(ActionEvent ae)
{
pw.println(title+":"+tf.getText());
tf.setText("");
pw.flush();
}
}