Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Local Area Network Chat Room) In this assignment, you will need to modify the file simpleChatRoom.java to create a JAVA chat room on local area

(Local Area Network Chat Room) In this assignment, you will need to modify the file simpleChatRoom.java to create a JAVA chat room on local area network. The functionality provided is a simiplifed version of standard chat room. Anyone on the local area network can join the chat room at anytime, and start to send messages to others. There is no restriction on who can join the chat room, but you should be able to specify a name that will be sent with your messages, and you should be able to exit the chat room with a "QUIT" command.

- Finish up the sample code

 import java.io.*; import java.net.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Scanner; public class simpleChatRoom implements Runnable { public static int Port = 1777; /* Create the GUI Chat Room and listen to message * on local area network. * Please do not modify. */ // Variables declaration - do not modify private static javax.swing.JTextArea content; // End of variables declaration private static void chatRoomGUI() { //Create and set up the window JFrame frame = new JFrame("Chat Room"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); content = new JTextArea(350,600); content.setLineWrap(true); // content.append("Start chatroom....... "); JScrollPane scroll = new JScrollPane(content); scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); frame.getContentPane().add(scroll,BorderLayout.CENTER); frame.setPreferredSize(new Dimension(400, 600)); frame.setAlwaysOnTop(true); frame.pack(); frame.setVisible(true); } @Override public void run() { //Listen to UDP packets. try { DatagramSocket server = new DatagramSocket(Port); while (true) { // receive a packet byte[] buf = new byte[256]; DatagramPacket packet = new DatagramPacket(buf, buf.length); server.receive(packet); // display response String received = new String(packet.getData(), 0, packet.getLength()); content.append(received + " "); } } catch (IOException e) { System.err.print(e.getMessage()); } } public static void main(String[] args) throws IOException { { //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { chatRoomGUI(); } }); //Start a thread to listen to the packet (new Thread(new simpleChatRoom())).start(); /* Please start your client-side codes here. * Please send all your packets to port 1777 */ /* * End of your code */ } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions