How To Make A Chat Application Using Sockets In Java

In this tutorial we will see a Chat Application in Java, which is another module of remote procedure call, we will deal with sockets and its parameter to work out with our requirement. From generations, remote procedure call has been used to make message passing system in any environment, can be a distributed system, standalone or any client Server environment.

For detail and brief explanation on remote procedure call, you can read this article to understand in quite 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 – Chatapplication. This will create chat Application project inside your IDE. Also, there will be 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 form, one for Client and one for Server. Jframe is used to make a design of your Application; it is having simple configuration; which is similar to ASPX pages. Also, have decent toolbox with drag and drop functionality. Now, create two Jframe and name it as Client.java and Server.java. Afterwards, Design is given below.

Client.java

Java

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

Server.java

Java

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

Client.java 

  1. package chatapplication;  
  2. import java.io.DataInputStream;  
  3. import java.io.DataOutputStream;  
  4. import java.net.Socket;  
  5.   
  6. /** 
  7.  * 
  8.  * @author Nilu 
  9.  */  
  10. public class client extends javax.swing.JFrame  
  11. {  
  12.   
  13.     static Socket sckt;  
  14.     static DataInputStream dtinpt;  
  15.     static DataOutputStream dtotpt;  
  16.     /** Creates new form client */  
  17.     public client() {  
  18.         initComponents();  
  19.     }  
  20.      
  21.     @SuppressWarnings("unchecked")  
  22.    
  23.     // <editor-fold defaultstate="collapsed" desc="Generated Code">  
  24.     private void initComponents() {  
  25.   
  26.          
  27.     }// </editor-fold>  
  28.   
  29.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)  
  30. {  
  31.     // TODO add your handling code here:  
  32.   
  33.   
  34.     try  
  35.     {  
  36.         String msgout = "";  
  37.         msgout = jTextField1.getText().trim();  
  38.         dtotpt.writeUTF(msgout);  
  39.     }  
  40.     catch (Exception e)  
  41.     {  
  42.     }  
  43. }  
  44.   
  45. public static void main(String args[])  
  46. {  
  47.     java.awt.EventQueue.invokeLater(new Runnable()  
  48.     {  
  49.             public void run()  
  50. {  
  51.     new client().setVisible(true);  
  52. }  
  53.         });  
  54.   
  55.         try  
  56.         {  
  57.             sckt = new Socket("127.0.0.1",1201);  
  58.             dtinpt = new DataInputStream(sckt.getInputStream());  
  59.             dtotpt = new DataOutputStream(sckt.getOutputStream());  
  60.             String msgin = "";  
  61.             while(!msgin.equals("Exit"))  
  62.             {  
  63.                 msgin=dtinpt.readUTF();  
  64.                 jTextArea1.setText(jTextArea1.getText().trim()+"\n Server:"+msgin);  
  65.             }  
  66.         }  
  67.         catch(Exception e)  
  68.         {  
  69.   
  70.         }  
  71.     }  
  72.       
  73.     // Variables declaration - do not modify  
  74. private javax.swing.JButton jButton1;  
  75. private javax.swing.JLabel jLabel1;  
  76. private javax.swing.JLabel jLabel2;  
  77. private javax.swing.JScrollPane jScrollPane1;  
  78. private static javax.swing.JTextArea jTextArea1;  
  79. private javax.swing.JTextField jTextField1;  
  80.     // End of variables declaration  
  81.   
  82. }   

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.   
  7.   
  8. public class Server extends javax.swing.JFrame  
  9. {  
  10.   
  11.     static ServerSocket ssckt;  
  12.     static Socket sckt;  
  13.     static DataInputStream dtinpt;  
  14.     static DataOutputStream dtotpt;  
  15.   
  16.   
  17.     /** Creates new form Server */  
  18.     public Server() {  
  19.         initComponents();  
  20.     }  
  21.   
  22.     @SuppressWarnings("unchecked")  
  23.     // <editor-fold defaultstate="collapsed" desc="Generated Code">  
  24.     private void initComponents() { }  
  25.   
  26. private void btnsendActionPerformed(java.awt.event.ActionEvent evt)  
  27. {  
  28.   
  29.     try  
  30.     {  
  31.         String msgout = "";  
  32.         msgout = txtbxfield.getText().trim();  
  33.         dtotpt.writeUTF(msgout);  
  34.   
  35.     }  
  36.     catch (Exception e)  
  37.     {  
  38.     }  
  39.   
  40.      
  41. }  
  42.   
  43. public static void main(String args[])  
  44. {  
  45.     java.awt.EventQueue.invokeLater(new Runnable()  
  46.     {  
  47.             public void run()  
  48. {  
  49.     new Server().setVisible(true);  
  50. }  
  51.         });  
  52.   
  53.         String msgin = "";  
  54.         try  
  55.         {  
  56.             ssckt = new ServerSocket(1201);  
  57.             sckt = ssckt.accept();  
  58.             dtinpt = new DataInputStream(sckt.getInputStream());  
  59.             dtotpt = new DataOutputStream(sckt.getOutputStream());  
  60.             while(!msgin.equals("exit"))  
  61.             {  
  62.                 msgin =dtinpt.readUTF();  
  63.                 txtbxarea.setText(txtbxarea.getText().trim()+"\n Client:"+msgin);  
  64.             }  
  65.         }  
  66.         catch(Exception e)  
  67.         {  
  68.         }  
  69.     }  
  70.   
  71.     // Variables declaration - do not modify  
  72. private javax.swing.JButton btnsend;  
  73. private javax.swing.JLabel jLabel1;  
  74. private javax.swing.JLabel jLabel2;  
  75. private javax.swing.JScrollPane jScrollPane1;  
  76. private static javax.swing.JTextArea txtbxarea;  
  77. private javax.swing.JTextField txtbxfield;  
  78.     // End of variables declaration  
  79.   
  80. }   

Output

Java

Hope, you like it. Thank you for for reading. Have a good day.

Up Next
    Ebook Download
    View all
    Learn
    View all