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

Create a Group Chat application in JAVA. The chat uses TCP/IP sockets to communi

ID: 3664740 • Letter: C

Question

Create a Group Chat application in JAVA. The chat uses TCP/IP sockets to communicate, and can be embedded easily in a Web page. In this system, there is a network of regular PCs or laptops with wireless network cards forming a distributed computing system. The nodes in the networks communicate using messages.

They should also be able to share files with each other.

FUNCTIONALITY The application should also provide some kind of interesting and useful functionality.describe what functionality your application will provide.

Explanation / Answer

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.net.*;

import java.io.*;

class Receiver implements Runnable {

Thread activity = new Thread(this);

MulticastSocket so;

JTextArea txt;

Receiver(MulticastSocket sock, JTextArea txtAr)

{

so = sock; txt = txtAr; activity.start();

}

Running the application

public void run() {

byte[] data = new byte[1024];

while(true) try {

DatagramPacket packet = new DatagramPacket(data,data.length);

so.receive(packet);

String mess = new String(data,0,packet.getLength()); txt.append(mess+ " "); }

catch(IOException e) { break; } } }

The Constructor and GUI base

public class JChat extends JFrame implements ActionListener {

String name;

InetAddress iadr;

int port;

MulticastSocket so;

JTextArea txt = new JTextArea();

JScrollPane sp = new JScrollPane(txt);

JTextField write = new JTextField();

JButton quit = new JButton("Go Offline");

public JChat(String username, String groupAdr, int portNr) throws IOException {

name = username;

iadr = InetAddress.getByName(groupAdr);

port = portNr;

so = new MulticastSocket(port);

so.joinGroup(iadr);

new Receiver(so,txt);

[B]sendMess[/B]("Online");

setTitle("Chatting with "+ name);

txt.setEditable(true);

add(quit,BorderLayout.NORTH);

add(sp,BorderLayout.CENTER);

add(write,BorderLayout.SOUTH);

quit.addActionListener(this);

write.addActionListener(this);

setSize(400,250);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE); }

Styling the Chat Window

sendMess method public void sendMess(String s) {

byte[] data = (name + ": " + s).getBytes();

DatagramPacket packet = new DatagramPacket(data,data.length,iadr,port);

try { so.send(packet); }

catch(IOException ie) {

Toolkit.getDefaultToolkit().beep();

JOptionPane.showMessageDialog(null, "Data overflow !"); } }

The actionListener

public void actionPerformed(ActionEvent e) {

if(e.getSource()==write) {

sendMess(write.getText());

write.setText(""); }

else if(e.getSource()==quit) {

sendMess("Offline");

try {

so.leaveGroup(iadr);

}

catch(IOException ie) {

Toolkit.getDefaultToolkit().beep();

JOptionPane.showMessageDialog(null, "Data overflow, connection error !"); }

so.close();

dispose();

System.exit(0); } }

The main code

public static void main(String[] arg) throws IOException {

String in = JOptionPane.showInputDialog(null,"What's your name?");

if(arg.length>0) in = arg[0];

new JChat(in,"Add a D-Class IP",9876); } }