Question

Problem descriptions: In this mini project, you are to develop a chat program for peer-to-peer communications....

Problem descriptions:

In this mini project, you are to develop a chat program for peer-to-peer communications. The techniques involved in this mini project include multithreading, client-server application using sockets, and graphical user interface (GUI).

Part A. In this part your implementation should include a client-server chatting functionality with one server and one client, where the client initiates the communication and the server responds. Show below is an example of a simple GUI for communications. You can fine-tune the GUI to include more features.


Part B. Modify your program developed in Part A to make it a peer-to-peer chat program so that either side can initiate the communication.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

This client server application is basic GUI based application..client or server both can initiate the chat.

How to run Client-Server

1.First compile both client and server programs.

2. Start server program then

3.Start client program

enjoy the chatting!

Chat Client:

import java.awt.*;
import java.awt.event.*;

import java.io.*;
import java.net.*;
/**
 App client which will connection to server.
 */
public class ChatClient extends Frame implements ActionListener,Runnable
{
 //Declarations
 Button b;
 TextField tf;
 TextArea ta;
 Socket s;
 PrintWriter pw;
 BufferedReader br;
 Thread th;
 
 public ChatClient()
 {
  Frame f=new Frame("Client Window");//Frame for Client
  f.setLayout(new FlowLayout());//set layout
  f.setBackground(Color.blue);//set background color of the Frame
  b=new Button("Send");//Send Button
  //b=setBackground(Color.white);

  b.addActionListener(this);//Add action listener to send button.
  f.addWindowListener(new W1());//add Window Listener to the Frame
  tf=new TextField(15);
  ta=new TextArea(12,20);
  ta.setBackground(Color.white);
  f.add(tf);//Add TextField to the frame
  f.add(b);//Add send Button to the frame
  f.add(ta);//Add TextArea to the frame
  try{
   s=new Socket(InetAddress.getLocalHost(),12000);//Socket for client
   //below line reads input from InputStreamReader
   br=new BufferedReader(new InputStreamReader(s.getInputStream()));
   //below line writes output to OutPutStream
   pw=new PrintWriter(s.getOutputStream(),true);
  }catch(Exception e)  
  {
  }
  th=new Thread(this);//start a new thread
  th.setDaemon(true);//set the thread as demon
  th.start();
  setFont(new Font("Arial",Font.BOLD,20));
  f.setSize(300,300);//set the size
  f.setVisible(true);
  f.setLocation(100,300);//set the location
  f.validate();
 }
 //method required to close the Frame on clicking "X" icon.
 private class W1 extends WindowAdapter
 {
  public void windowClosing(WindowEvent we)
  {
   System.exit(0);
  }
 }
 //This method will called after clicking on Send button.
 public void actionPerformed(ActionEvent ae)
 {
  pw.println(tf.getText());//write the value of textfield into PrintWriter
  tf.setText("");//clean the textfield
 }
 //Thread running as a process in background
 public void run()
 {
  while(true)
  {
   try{
    ta.append(br.readLine()+"\n");//Append to TextArea
   }catch(Exception e) {}
  }
 }
 //Main method
 public static void main(String args[])
 {
  //Instantiate AppClient class
   ChatClient client = new ChatClient();
 }
}

:

Chat Server:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

// chat server will handle request.
public class ChatServer extends Frame implements ActionListener,Runnable
{
 //Declarations
 Button b1;
 TextField tf;
 TextArea ta;
 ServerSocket ss;
 Socket s;
 PrintWriter pw;
 BufferedReader br;
 Thread th;
 
 public ChatServer()
 {
  Frame f=new Frame("Server Window");//Frame for Server
  f.setLayout(new FlowLayout());//set layout
  f.setBackground(Color.blue);//set background color of the Frame
  b1=new Button("Send");//Send Button
  //b1.setBackground(Color.red);
  b1.addActionListener(this);//Add action listener to send button.
  tf=new TextField(15);
  ta=new TextArea(12,20);
  ta.setBackground(Color.white);
  f.addWindowListener(new W1());//add Window Listener to the Frame
  f.add(tf);//Add TextField to the frame
  f.add(b1);//Add send Button to the frame
  f.add(ta);//Add TextArea to the framered
  try{
   ss=new ServerSocket(12000);//Socket for server
   s=ss.accept();//accepts request from client
   System.out.println(s);
   //below line reads input from InputStreamReader
   br=new BufferedReader(new InputStreamReader(s.getInputStream()));
   //below line writes output to OutPutStream
   pw=new PrintWriter(s.getOutputStream(),true);
  }catch(Exception e)
  {
  }
  th=new Thread(this);//start a new thread
  th.setDaemon(true);//set the thread as demon
  th.start();
  setFont(new Font("Arial",Font.BOLD,20));
  f.setSize(300,300);//set the size
  f.setLocation(300,300);//set the location
  f.setVisible(true);
  f.validate();
 }
 //method required to close the Frame on clicking "X" icon.
 private class W1 extends WindowAdapter
 {
  public void windowClosing(WindowEvent we) 
  {
   System.exit(0);
  }
 }
 //This method will called after clicking on Send button.
 public void actionPerformed(ActionEvent ae)
 {
  pw.println(tf.getText());//write the value of textfield into PrintWriter
  tf.setText("");//clean the textfield
 }
 //Thread running as a process in background
 public void run()
 {
  while(true)
  {
   try{
    String s=br.readLine();//reads the input from textfield
    ta.append(s+"\n");//Append to TextArea
   }catch(Exception e)
   {
   }
  } 
 }
 //Main method
 public static void main(String args[]) 
 {
  //Instantiate AppServer class
  ChatServer server = new ChatServer();
 }
} 

Output:

Client Window - - Send Micros (c) 2€ 1°C:\Use Hi Im doing well.How are yo This is amazing chat app cs. Command Prompt - java

Thanks

Add a comment
Know the answer?
Add Answer to:
Problem descriptions: In this mini project, you are to develop a chat program for peer-to-peer communications....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this project,use java to write a chat program that enables real-time communication between 3 persons....

    In this project,use java to write a chat program that enables real-time communication between 3 persons. Specifically the program should permit chat sessions between 2 well as 3 persons,. In case of 3 persons, messages generated by one should reach the other two (Its a conferencing facility and not an individual chat facility). The chat program needs to allow a user to type messages, which should be conveyed to the other end(s) and displayed at the other end(s). The user...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT