Friday, 20 May 2022

Write a socket program in java for chatting application.(Use Swing)

 

DOWNLOAD   ServerChatForm     ClientChatForm

// Step 1 SAVE THIS FILE AS ServerChatForm.java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
* Created by : Amar Ghugare
* created on : 20-05-2022 17:32
* Purpose of class : to create a server of chat
*/
public class ServerChatForm extends JFrame implements ActionListener {

static ServerSocket server;
static Socket conn;
JPanel panel;
JTextField NewMsg;
JTextArea ChatHistory;
JButton Send;
DataInputStream dis;
DataOutputStream dos;
public ServerChatForm()throws UnknownHostException, IOException{
this.setSize(500, 500);
this.setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Server");

panel = new JPanel();
panel.setLayout(null);
this.add(panel);

NewMsg = new JTextField();
NewMsg.setBounds(20, 400, 340, 30);
panel.add(NewMsg);

ChatHistory = new JTextArea();
ChatHistory.setText("Waiting for Client");
ChatHistory.setBounds(20, 20, 450, 360);
panel.add(ChatHistory);

Send = new JButton("Send");
Send.setBounds(375, 400, 95, 30);
Send.addActionListener(this);
panel.add(Send);


server = new ServerSocket(2000, 1, InetAddress.getLocalHost());
conn = server.accept();
ChatHistory.setText(ChatHistory.getText() + '\n' + "Client Found");

while (true) {
try {
DataInputStream dis = new DataInputStream(conn.getInputStream());
String string = dis.readUTF();
ChatHistory.setText(ChatHistory.getText() + '\n'+ "Client:"+string);
} catch (Exception e1) {
ChatHistory.setText(ChatHistory.getText() + '\n'+ "Message sending fail:Network Error");
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if ((e.getSource() == Send) && (NewMsg.getText() != "")) {
ChatHistory.setText(ChatHistory.getText() + '\n' + "ME:"+ NewMsg.getText());
try {
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeUTF(NewMsg.getText());
} catch (Exception e1) {
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e2) {
e2.printStackTrace();
}
}
NewMsg.setText("");
}
}

public static void main(String[] args)throws UnknownHostException,IOException {
new ServerChatForm();
}
}


// STEP 2 SAVE THIS FILE AS ClientChatForm.java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
* Created by : Amar Ghugare
* created on : 20-05-2022 17:44
* Purpose of class : to make a client for chat
*/
public class ClientChatForm extends JFrame implements ActionListener {
static Socket conn;
JPanel panel;
JTextField NewMsg;
JTextArea ChatHistory;
JButton Send;

public ClientChatForm()throws UnknownHostException, IOException{
this.setSize(500, 500);
this.setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

panel = new JPanel();
panel.setLayout(null);
this.add(panel);

NewMsg = new JTextField();
NewMsg.setBounds(20, 400, 340, 30);
panel.add(NewMsg);

ChatHistory = new JTextArea();
ChatHistory.setBounds(20, 20, 450, 360);
panel.add(ChatHistory);

Send = new JButton("Send");
Send.setBounds(375, 400, 95, 30);
Send.addActionListener(this);
panel.add(Send);


conn = new Socket(InetAddress.getLocalHost(), 2000);

/** for remote pc use ip address of server with function* InetAddress.getByName("Provide ip here")* ChatHistory.setText("Connected to Server");*/
ChatHistory.setText("Connected to Server");
this.setTitle("Client");
while (true) {
try {
DataInputStream dis = new DataInputStream(conn.getInputStream());
String string = dis.readUTF();
ChatHistory.setText(ChatHistory.getText() + '\n'+ "Server:"+ string);
} catch (Exception e1) {
ChatHistory.setText(ChatHistory.getText() + '\n'+ "Message sending fail:Network Error");
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if ((e.getSource() == Send) && (NewMsg.getText() != "")) {
ChatHistory.setText(ChatHistory.getText() + '\n' + "Me:"+ NewMsg.getText());
try {
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeUTF(NewMsg.getText());
} catch (Exception e1) {
ChatHistory.setText(ChatHistory.getText() + '\n' + "Message sending fail:Network Error");
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException e2) {
e2.printStackTrace();
}
}
NewMsg.setText("");
}
}

public static void main(String[] args)throws UnknownHostException,IOException {
new ClientChatForm();
}
}


// STEP 3 COMPILE BOTH FILES 
// STEP 4 RUN SERVER CHAT FIRST THEN RUN CLIENT CHAT




No comments:

Post a Comment

Write a Java program to display given extension files from a specific directory on server machine.

 DOWNLOAD     SLIP14Q2 /** * STEPS TO RUN CODE * Step 01 compile the code * Step 02 run the code * Step 03 give a file directory locatio...