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 its parameter, to work out with our requirement.  For generations, remote procedure call has been used to make message passing system 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 call.


Prerequisite

  1. Installed Java
  2. NetBeans SDK
Once NetBeans is installed, you have to make a Java Application.  Name it as – 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. 
 
Java

Once you are done with this, right-click on the project -- > New -- > Select JFrame Form.  You have to make two Jframe forms, one for Client and one for 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 Jframe and name them Client.java and Server.java.  Afterward, design is given below.


Client.java

 
Java

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

 
Java

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


Client.java  
 
  1. package chatapplication;  
  2. import java.io.DataInputStream;  
  3. import java.io.DataOutputStream;  
  4. import java.net.Socket;  
  5. public class client extends javax.swing.JFrame {  
  6.  static Socket sckt;  
  7.  static DataInputStream dtinpt;  
  8.  static DataOutputStream dtotpt;  
  9.  public client() {  
  10.   initComponents();  
  11.  }  
  12.  @SuppressWarnings("unchecked")  
  13.  private void initComponents() {}  
  14.  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {  
  15.   try {  
  16.    String msgout = "";  
  17.    msgout = jTextField1.getText().trim();  
  18.    dtotpt.writeUTF(msgout);  
  19.   } catch (Exception e) {}  
  20.  }  
  21.  public static void main(String args[]) {  
  22.   java.awt.EventQueue.invokeLater(new Runnable() {  
  23.    public void run() {  
  24.     new client().setVisible(true);  
  25.    }  
  26.   });  
  27.   try {  
  28.    sckt = new Socket("127.0.0.1"1201);  
  29.    dtinpt = new DataInputStream(sckt.getInputStream());  
  30.    dtotpt = new DataOutputStream(sckt.getOutputStream());  
  31.    String msgin = "";  
  32.    while (!msgin.equals("Exit")) {  
  33.     msgin = dtinpt.readUTF();  
  34.     jTextArea1.setText(jTextArea1.getText().trim() + "\n Server:" + msgin);  
  35.    }  
  36.   } catch (Exception e) {}  
  37.  }  
  38.  private javax.swing.JButton jButton1;  
  39.  private javax.swing.JLabel jLabel1;  
  40.  private javax.swing.JLabel jLabel2;  
  41.  private javax.swing.JScrollPane jScrollPane1;  
  42.  private static javax.swing.JTextArea jTextArea1;  
  43.  private javax.swing.JTextField jTextField1;  
  44. }  
 

Server. Java 
  1. package chatapplication;  
  2. import java.io.DataInputStream;  
  3. import java.io.DataOutputStream;  
  4. import java.net.ServerSocket;  
  5. import java.net.Socket;  
  6. public class Server extends javax.swing.JFrame {  
  7.  static ServerSocket ssckt;  
  8.  static Socket sckt;  
  9.  static DataInputStream dtinpt;  
  10.  static DataOutputStream dtotpt;  
  11.  public Server() {  
  12.   initComponents();  
  13.  }  
  14.  @SuppressWarnings("unchecked")  
  15.  private void initComponents() {}  
  16.  private void btnsendActionPerformed(java.awt.event.ActionEvent evt) {  
  17.   try {  
  18.    String msgout = "";  
  19.    msgout = txtbxfield.getText().trim();  
  20.    dtotpt.writeUTF(msgout);  
  21.   } catch (Exception e) {}  
  22.  }  
  23.  public static void main(String args[]) {  
  24.   java.awt.EventQueue.invokeLater(new Runnable() {  
  25.    public void run() {  
  26.     new Server().setVisible(true);  
  27.    }  
  28.   });  
  29.   String msgin = "";  
  30.   try {  
  31.    ssckt = new ServerSocket(1201);  
  32.    sckt = ssckt.accept();  
  33.    dtinpt = new DataInputStream(sckt.getInputStream());  
  34.    dtotpt = new DataOutputStream(sckt.getOutputStream());  
  35.    while (!msgin.equals("exit")) {  
  36.     msgin = dtinpt.readUTF();  
  37.     txtbxarea.setText(txtbxarea.getText().trim() + "\n Client:" + msgin);  
  38.    }  
  39.   } catch (Exception e) {}  
  40.  }  
  41.  private javax.swing.JButton btnsend;  
  42.  private javax.swing.JLabel jLabel1;  
  43.  private javax.swing.JLabel jLabel2;  
  44.  private javax.swing.JScrollPane jScrollPane1;  
  45.  private static javax.swing.JTextArea txtbxarea;  
  46.  private javax.swing.JTextField txtbxfield;  
  47. }   

Output
 
Java

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