How To Make A Chat Application Using Sockets In Java

Introduction

In this tutorial, we will see a Chat Application in Java, which is another module of a remote procedure call. We will deal with sockets and their parameters, to work out our requirements. For generations, remote procedure calls have been used to make message-passing systems in any environment. It can be a distributed system, standalone, or any client Server environment.

For details and a brief explanation of remote procedure calls, you can read this article to help understand, in quite a decent manner – Remote Procedure calls.

Prerequisite

  1. Installed Java
  2. NetBeans SDK

Once NetBeans is installed, you have to make a Java Application. Name it as a chat application. This will create a chat Application project inside your IDE. Also, there will be a main.java file created, which you can delete, as it is not required.

 Application project

Once you are done with this, right-click on the project -- > New -- > Select JFrame Form. You have to make two JFrame forms, one for the Client and one for the Server. Jframe is used to make a design of your Application. it has a simple configuration, which is similar to ASPX pages. Also, it has a decent toolbox with drag-and-drop functionality. Now, create two Jframes and name them Client.java and Server.java. Afterward, the design is given below.

Client.java

Client

You have to drag and drop these controls – TextArea, TextField, Button, and Label. TextArea is used to view the incoming messages and TextField is used to write the message.

Server.java

Server

Now that the coding part is there. Open client.java and Server.java. Enter the code part.

Client.java

package chatapplication;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

public class client extends javax.swing.JFrame {
    static Socket sckt;
    static DataInputStream dtinpt;
    static DataOutputStream dtotpt;

    public client() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            String msgout = "";
            msgout = jTextField1.getText().trim();
            dtotpt.writeUTF(msgout);
        } catch (Exception e) {
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new client().setVisible(true);
            }
        });

        try {
            sckt = new Socket("127.0.0.1", 1201);
            dtinpt = new DataInputStream(sckt.getInputStream());
            dtotpt = new DataOutputStream(sckt.getOutputStream());
            String msgin = "";
            while (!msgin.equals("Exit")) {
                msgin = dtinpt.readUTF();
                jTextArea1.setText(jTextArea1.getText().trim() + "\n Server:" + msgin);
            }
        } catch (Exception e) {
        }
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private static javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
}

Server. Java

package chatapplication;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server extends javax.swing.JFrame {
    static ServerSocket ssckt;
    static Socket sckt;
    static DataInputStream dtinpt;
    static DataOutputStream dtotpt;

    public Server() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {}

    private void btnsendActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            String msgout = "";
            msgout = txtbxfield.getText().trim();
            dtotpt.writeUTF(msgout);
        } catch (Exception e) {}
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Server().setVisible(true);
            }
        });

        String msgin = "";
        try {
            ssckt = new ServerSocket(1201);
            sckt = ssckt.accept();
            dtinpt = new DataInputStream(sckt.getInputStream());
            dtotpt = new DataOutputStream(sckt.getOutputStream());
            while (!msgin.equals("exit")) {
                msgin = dtinpt.readUTF();
                txtbxarea.setText(txtbxarea.getText().trim() + "\n Client:" + msgin);
            }
        } catch (Exception e) {}
    }

    private javax.swing.JButton btnsend;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private static javax.swing.JTextArea txtbxarea;
    private javax.swing.JTextField txtbxfield;
}

Output

Java

I hope you like it. Thank you for reading. Have a good day.