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

please write comments so i can understand the code and do it by Java, please Des

ID: 3720258 • Letter: P

Question

please write comments so i can understand the code and do it by Java, please

Design and implement a Java program that enables two users to chat. Using GUIs implement one user as the server and the other as the client. The server has two text areas: one for entering text and the other (noneditable) for displaying text received from the client. The client has two text areas: one for receiving text from the server and the other for entering text. When the user presses the enter key, the current line is sent to the server or client.

Explanation / Answer

I hope you know about basic layout coding i.e. how to set layout, frame. Here we are discussing only networking part.

1. server process -: It is a running program which always takes request from the client and provides the scope.

2. client process -: It is a running program which is responsible to give request to the server and get responses.

3. IP Address -: It is a unique ID assign for each system over a network.

4. port no -: It is a short int, which is used to identify a process uniquely.

* get ip address -: InetAddress.getByName("192.168.43.227")

* java.net provides 2 class serverSocket (used to established connection from server end) accept port number example ServerSocket(int port)

class socket(established connection from client side with the help of IP address & port number)

simple example

//server.java

import java.net*

public class server

{

public static void main(String[] args) throws execption

{

ServerSocket ss = new ServerSocket(5000);

System.out.println("Waiting for client");

Socket s1=ss.accept();

System.out.println("connected");

}

}

//client.java

import java.net*

public class client

{

public static void main(String[] args) throws execption

{

Socket s1=s1=new Socket(InetAddress.getByName("192.168.43.227"), 1000);

}

}

// I hope you know about file read and file write

* java.io* provide 2 different class to read and write operation in socket memory

1. BufferedReader (read from the socket)

2. printWriter (write-in socket)

here is the final code

A. class server

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.InetAddress;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.Date;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class ChatServer extends JFrame implements Runnable,ActionListener

{

JTextField tf1;

JTextArea ta1;

JScrollPane p1;

JButton b1;

ServerSocket ss;

Socket s1;

PrintWriter pw;

BufferedReader br;

public ChatServer()

{

setLayout(null);

getContentPane().setBackground(Color.blue);

setTitle("Chat Server");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setResizable(false);

tf1=new JTextField();

ta1=new JTextArea();

p1=new JScrollPane(ta1);

b1=new JButton("SEND");

add(tf1);

add(ta1);

add(b1);

add(p1);

tf1.setBounds(30, 30,150,25);

b1.setBounds(190, 30,80,25);

ta1.setBounds(30, 65,240,220);

try

{

ss=new ServerSocket(1000);

s1=ss.accept();

pw=new PrintWriter(s1.getOutputStream(),true);

br=new BufferedReader(new InputStreamReader(s1.getInputStream()));

}

catch (Exception e)

{

e.printStackTrace();

}

b1.addActionListener(this);

tf1.addActionListener(this);

new Thread(this).start();

setSize(350, 350);

setVisible(true);

}

public static void main(String args[])

{

new ChatServer();

}

public void actionPerformed(ActionEvent e)

{

String text=tf1.getText();

pw.println(text);

ta1.append(text+" -Me "+new Date()+" ");

tf1.setText("");

}

public void run()

{

while(true)

{

try

{

String text=br.readLine();

ta1.append(text+" -Sender"+new Date()+" ");

}

catch (Exception e1)

{

e1.printStackTrace();

}

}

}

}

B. class client

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.InetAddress;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.Date;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class ChatClient extends JFrame implements Runnable,ActionListener

{

JTextField tf1;

JTextArea ta1;

JScrollPane p1;

JButton b1;

ServerSocket ss;

Socket s1;

PrintWriter pw;

BufferedReader br;

public ChatClient()

{

setLayout(null);

getContentPane().setBackground(Color.GREEN);

setTitle("Chat Client");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setResizable(false);

tf1=new JTextField();

ta1=new JTextArea();

p1=new JScrollPane(ta1);

b1=new JButton("SEND");

add(tf1);

add(ta1);

add(b1);

add(p1);

tf1.setBounds(30, 30,150,25);

b1.setBounds(190, 30,80,25);

ta1.setBounds(30, 65,240,220);

try

{

s1=new Socket(InetAddress.getByName("192.168.43.227"), 1000);

pw=new PrintWriter(s1.getOutputStream(),true);

br=new BufferedReader(new InputStreamReader(s1.getInputStream()));

}

catch (Exception e)

{

e.printStackTrace();

}

b1.addActionListener(this);

tf1.addActionListener(this);

new Thread(this).start();

setSize(350, 350);

setVisible(true);

}

public static void main(String args[])

{

new ChatClient();

}

public void actionPerformed(ActionEvent e)

{

String text=tf1.getText();

pw.println(text);

ta1.append(text+" -Me"+new Date()+" ");

tf1.setText("");

}

public void run()

{

while(true)

{

try

{

String text=br.readLine();

ta1.append(text+" -Sender"+new Date()+" ");

}

catch (Exception e1)

{

// e1.printStackTrace();

}

}

}

}

I give the theory in the beginning, I hope it will clear your all doubts.