Coders Packet

Server-Client model based Chatting Application using Java Swing , AWT , Java Networking

By Akash N

This is a stand-alone desktop-based Chatting Application implemented using Java Swing, AWT, and Java Networking.

Server-Client model-based Chatting Application using Java Swing, AWT, Java Networking


Introduction :

This is a stand-alone desktop-based Chatting Application implemented using Java Swing, AWT, and Java Networking. It is a Server-Client model-based application and is a good example of using java.io, java.net package to create a chat application.

This project aims to express how we can implement a simple chat application between a server and a client. The application is desktop-based and is implemented using Swing and AWT.

Its functionalities include :

a. Sending text messages between Server and Client.

b. Clean interface with proper alignment of text.

c. Speech bubble feature.

d. Scrollbar.

 

Let's dive directly into the coding part.

Java Classes :


1. user_1 (Server class) :

The server module of the application waits for the client to connect to it. Then if the connection is granted a client interacts communicates and connects to the server, it can mutually communicate with the server. The duty of the server is to let clients exchange messages.

The server can be termed as the source of the project as it provides services to other applications or users in the same network. After starting server waits for the client’s request to connect. A server listens to a specific port for the client’s connection request, as soon as the request is sent by the client, a connection between server and client will be established, through which they can communicate. A local port number will be assigned to the client and a socket will be bound to it. In the same way, the server will also be assigned another local port number, as it also has to connect requests from other clients. A socket will be bound to the local port of the server as well, through which both can communicate by reading and writing in it. As this is a single server-based application, there exists only one server.

package chatting.application;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.io.*;
import javax.swing.border.*;

public class user_1 implements ActionListener{
  static JFrame f = new JFrame();
  JPanel p1 = new JPanel();
  JTextField t1= new JTextField();
  JButton b1 = new JButton("Send");
  static JPanel ta1 = new JPanel();
  
  Timer timer;
  
  static Box box = Box.createVerticalBox();
  
  static ServerSocket server_socket;
  static Socket socket;
  static DataInputStream d_in;
  static DataOutputStream d_out;
  
  Boolean typing = false;
  
  user_1(){
    
    
    
    p1.setLayout(null);
    p1.setBackground(Color.decode("#2a162e"));
    p1.setBounds(0,0,400,65);
    f.add(p1);
    
    ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("chatting/application/icons/3.png"));
    Image i2 = i1.getImage().getScaledInstance(25, 25, Image.SCALE_DEFAULT);
    ImageIcon i3 = new ImageIcon(i2);
    JLabel l1 = new JLabel(i3);
    l1.setBounds(5,21,25,25);
    p1.add(l1);
    
    l1.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        System.exit(0);
      }
    });

     
    
    ImageIcon i4 = new ImageIcon(ClassLoader.getSystemResource("chatting/application/icons/img1.png"));
    Image i5 = i4.getImage().getScaledInstance(45, 45, Image.SCALE_DEFAULT);
    ImageIcon i6 = new ImageIcon(i5);
    JLabel l2 = new JLabel(i6);
    l2.setBounds(40,10,45,45);
    p1.add(l2);
    
    

    ImageIcon i7 = new ImageIcon(ClassLoader.getSystemResource("chatting/application/icons/video.png"));
    Image i8 = i7.getImage().getScaledInstance(-1, 25, Image.SCALE_DEFAULT);
    ImageIcon i9 = new ImageIcon(i8);
    JLabel l5 = new JLabel(i9);
    l5.setBounds(270,21,25,25);
    p1.add(l5);
    
    ImageIcon i10 = new ImageIcon(ClassLoader.getSystemResource("chatting/application/icons/phone.png"));
    Image i11 = i10.getImage().getScaledInstance(-1, 25, Image.SCALE_DEFAULT);
    ImageIcon i12 = new ImageIcon(i11);
    JLabel l6 = new JLabel(i12);
    l6.setBounds(315,21,25,25);
    p1.add(l6);
    
    
    ImageIcon i13 = new ImageIcon(ClassLoader.getSystemResource("chatting/application/icons/3icon.png"));
    Image i14 = i13.getImage().getScaledInstance(-1, 20, Image.SCALE_DEFAULT);
    ImageIcon i15 = new ImageIcon(i14);
    JLabel l7 = new JLabel(i15);
    l7.setBounds(360,21,15,25);
    p1.add(l7);
    
    
    JLabel l3 = new JLabel("USER-1");
    l3.setFont(new Font("SAN_SERIF", Font.BOLD, 14));
    l3.setForeground(Color.WHITE);
    l3.setBounds(100, 10, 100 , 25);
    p1.add(l3);
    
    JLabel l4 = new JLabel("Active Now");
    l4.setFont(new Font("SAN_SERIF", Font.PLAIN, 12));
    l4.setForeground(Color.WHITE);
    l4.setBounds(100, 35, 100 , 18);
    p1.add(l4);
    
    timer = new Timer(1,new ActionListener() {
      
      public void actionPerformed(ActionEvent ae)
      {
        if(!typing)
        {
          l4.setText("Active Now");
          
        }
      }
      
    });
    
    timer.setInitialDelay(1500);
    
    
    
    t1.setBounds(10,570,300,35);
    t1.setFont(new Font("SAN_SERIF", Font.PLAIN,16));
    f.add(t1);
    
    t1.addKeyListener(new KeyAdapter(){
      public void keyPressed(KeyEvent ke){
        
        l4.setText("typing...");
        
        timer.stop();
        
        typing = true;
      }
      
      public void keyReleased(KeyEvent ke){
        typing = false;
        
        if(!timer.isRunning())
        {
          timer.restart();
        }
      }
      
    });
    
    b1.setBounds(315,570,75,35);
    b1.setBackground(Color.decode("#2a162e"));
    b1.setForeground(Color.WHITE);
    b1.setFont(new Font("SAN_SERIF", Font.BOLD,14));
    b1.addActionListener(this);
    f.add(b1);
    
    //ta1.setBounds(5,70,390,495);
    ta1.setFont(new Font("SAN_SERIF",Font.PLAIN,16));
    
    
    //f.add(ta1);
    
    JScrollPane scroll = new JScrollPane(ta1);
    
    scroll.setBounds(5,70,390,495);
    scroll.setBorder(new EmptyBorder(3,3,3,3));
    scroll.getVerticalScrollBar().setPreferredSize(new Dimension(13, 0));
    
    f.add(scroll);
    
    f.setLayout(null);
    
    f.setSize(400,620);
    f.setLocation(300,100);
    f.setUndecorated(true);
    f.setVisible(true);
  }
  
  public void actionPerformed(ActionEvent ae) {
    
    
    
    try {
      
    String temp =  t1.getText();
    
    
    JPanel p2 = textarea_panel1(temp);
    
    ta1.setLayout(new BorderLayout());
    
    JPanel right = new JPanel(new BorderLayout());
    
    right.add(p2, BorderLayout.LINE_END);
    box.add(right);
    box.add(Box.createRigidArea(new Dimension(0,10)));
    ta1.add(box,BorderLayout.PAGE_START);
    f.validate();
    d_out.writeUTF(temp);
    t1.setText("");
    
    }catch(Exception err) {
      t1.setText("");
    }
  }
  
  public static JPanel textarea_panel1(String temp)
  {
    JPanel p3 = new JPanel();
    p3.setLayout(new BoxLayout(p3,BoxLayout.Y_AXIS));
    
    JLabel l2 = new JLabel("

"+temp+"

");
    l2.setFont(new Font("SAN_SERIF",Font.PLAIN,16));
    l2.setBackground(Color.decode("#68369b"));
    l2.setForeground(Color.WHITE);
    l2.setOpaque(true);
    
    l2.setBorder(new EmptyBorder(5,10,5,10));
    
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat time = new SimpleDateFormat("HH:MM");
    
    JLabel l3 = new JLabel();
    
    l3.setText(time.format(cal.getTime()));
    //l3.setBackground(new Color(200,200,200));
    //l3.setOpaque(true);
    p3.add(l2);
    p3.add(l3);
    return p3;
  }
  
  public static JPanel textarea_panel2(String temp)
  {
    JPanel p3 = new JPanel();
    p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS));
    JLabel l2 = new JLabel("

"+temp+"

");
    l2.setFont(new Font("SAN_SERIF",Font.PLAIN,16));
    l2.setBackground(new Color(200,200,200));
    l2.setOpaque(true);
    l2.setBorder(new EmptyBorder(5,10,5,10));
    
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat time = new SimpleDateFormat("HH:MM");
    
    JLabel l3 = new JLabel();
    
    l3.setText(time.format(cal.getTime()));
    
    p3.add(l2);
    p3.add(l3);
    return p3;
  }
  
  
  public static void main(String args[])
  {
    new user_1();
    
    String input = "";
    
    try {
    server_socket = new ServerSocket(6000);
    
    while(true)
    {
    socket = server_socket.accept();
    d_in = new DataInputStream(socket.getInputStream());
    d_out = new DataOutputStream(socket.getOutputStream());
    
    
    
    while(true)
    {
    input = d_in.readUTF();		
    JPanel temp = textarea_panel2(input);
    
    JPanel left = new JPanel(new BorderLayout());
    left.add(temp,BorderLayout.LINE_START);
    box.add(left);
    box.add(Box.createRigidArea(new Dimension(0,10)));
    ta1.add(box,BorderLayout.PAGE_START);
    f.validate();
    }
    }
    }
    catch(Exception e) {
      
    }
  }

}

 

 

 

2. user_2 (Client class) :

User_2 is a client module, it sends the connection request to the server(User_1 here) to connect. Text messages can be sent and received after this.

The client is a user in the same network as of server. As the client will be knowing the hostname of the pc on which the server is active and the port number on which the server is listening for requests, the client sends a connection requests to the server and hence a connection will be established. Communication is done with the help of sockets bound to the local ports of the client and server. There exists only one server, but there can be multiple clients connecting to the same server.

 

package chatting.application;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import javax.swing.border.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class user_2 implements ActionListener{
  static JFrame f = new JFrame();
  JPanel p1 = new JPanel();
  JTextField t1= new JTextField();
  JButton b1 = new JButton("Send");
  static JPanel ta1 = new JPanel();
  static Box box = Box.createVerticalBox();
  
  static Socket socket;
  static DataInputStream d_in;
  
  static DataOutputStream d_out;
  Timer timer ;
  Boolean typing = false;
  
  user_2(){
    
    
    
    p1.setLayout(null);
    p1.setBackground(Color.decode("#2a162e"));
    p1.setBounds(0,0,400,65);
    f.add(p1);
    
    ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("chatting/application/icons/3.png"));
    Image i2 = i1.getImage().getScaledInstance(25, 25, Image.SCALE_DEFAULT);
    ImageIcon i3 = new ImageIcon(i2);
    JLabel l1 = new JLabel(i3);
    l1.setBounds(5,21,25,25);
    p1.add(l1);
    
    l1.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        System.exit(0);
      }
    });

     
    
    ImageIcon i4 = new ImageIcon(ClassLoader.getSystemResource("chatting/application/icons/img1.png"));
    Image i5 = i4.getImage().getScaledInstance(45, 45, Image.SCALE_DEFAULT);
    ImageIcon i6 = new ImageIcon(i5);
    JLabel l2 = new JLabel(i6);
    l2.setBounds(40,10,45,45);
    p1.add(l2);
    
    

    ImageIcon i7 = new ImageIcon(ClassLoader.getSystemResource("chatting/application/icons/video.png"));
    Image i8 = i7.getImage().getScaledInstance(-1, 25, Image.SCALE_DEFAULT);
    ImageIcon i9 = new ImageIcon(i8);
    JLabel l5 = new JLabel(i9);
    l5.setBounds(270,21,25,25);
    p1.add(l5);
    
    ImageIcon i10 = new ImageIcon(ClassLoader.getSystemResource("chatting/application/icons/phone.png"));
    Image i11 = i10.getImage().getScaledInstance(-1, 25, Image.SCALE_DEFAULT);
    ImageIcon i12 = new ImageIcon(i11);
    JLabel l6 = new JLabel(i12);
    l6.setBounds(315,21,25,25);
    p1.add(l6);
    
    
    ImageIcon i13 = new ImageIcon(ClassLoader.getSystemResource("chatting/application/icons/3icon.png"));
    Image i14 = i13.getImage().getScaledInstance(-1, 20, Image.SCALE_DEFAULT);
    ImageIcon i15 = new ImageIcon(i14);
    JLabel l7 = new JLabel(i15);
    l7.setBounds(360,21,15,25);
    p1.add(l7);
    
    
    JLabel l3 = new JLabel("USER-2");
    l3.setFont(new Font("SAN_SERIF", Font.BOLD, 14));
    l3.setForeground(Color.WHITE);
    l3.setBounds(100, 10, 100 , 25);
    p1.add(l3);
    
    JLabel l4 = new JLabel("Active Now");
    l4.setFont(new Font("SAN_SERIF", Font.PLAIN, 12));
    l4.setForeground(Color.WHITE);
    l4.setBounds(100, 35, 100 , 18);
    p1.add(l4);
    
    
    timer = new Timer(1,new ActionListener() {
      
      public void actionPerformed(ActionEvent ae) {
        if(!typing)
        {
          l4.setText("Action Now");
        }
      }
      
    });
    
    timer.setInitialDelay(1500);
    
    t1.setBounds(10,570,300,35);
    t1.setFont(new Font("SAN_SERIF", Font.PLAIN,16));
    f.add(t1);
    
    t1.addKeyListener(new KeyAdapter() {
      
      public void keyPressed(KeyEvent ke)
      {
        l4.setText("typing...");
        timer.stop();
        typing=true;
      }
      
      public void keyReleased(KeyEvent ke)
      {
        typing = false;
        
        if(!timer.isRunning())
        timer.restart();
      }
      
    });
    
    
    b1.setBounds(315,570,75,35);
    b1.setBackground(Color.decode("#2a162e"));
    b1.setForeground(Color.WHITE);
    b1.setFont(new Font("SAN_SERIF", Font.BOLD,14));
    b1.addActionListener(this);
    f.add(b1);
    
    //ta1.setBounds(5,70,390,495);
    ta1.setFont(new Font("SAN_SERIF",Font.PLAIN,16));
        
    
    //f.add(ta1);
        
    JScrollPane scroll = new JScrollPane(ta1);
        
    scroll.setBounds(5,70,390,495);
    scroll.setBorder(new EmptyBorder(3,3,3,3));
    scroll.getVerticalScrollBar().setPreferredSize(new Dimension(13, 0));
        
    f.add(scroll);
        
    
    
    f.setLayout(null);
    
    f.setSize(400,620);
    f.setLocation(850,100);
    f.setUndecorated(true);
    f.setVisible(true);
  }
  
    public void actionPerformed(ActionEvent ae) {
    
    
    
    try {
      
    String temp =  t1.getText();
    
    
    JPanel p2 = textarea_panel1(temp);
    JPanel right = new JPanel(new BorderLayout());
    right.add(p2,BorderLayout.LINE_END);
    
    box.add(right);
    box.add(Box.createRigidArea(new Dimension(0,10)));
    ta1.setLayout(new BorderLayout());
    
    ta1.add(box,BorderLayout.PAGE_START);
    f.validate();
    d_out.writeUTF(temp);
    t1.setText("");
    
    }catch(Exception err) {
      t1.setText("");
    }
  }
  
    public static JPanel textarea_panel1(String temp)
  {
    JPanel p3 = new JPanel();
    p3.setLayout(new BoxLayout(p3,BoxLayout.Y_AXIS));
    
    JLabel l2 = new JLabel("

"+temp+"

");
    l2.setFont(new Font("SAN_SERIF",Font.PLAIN,16));
    l2.setBackground(Color.decode("#68369b"));
    l2.setForeground(Color.WHITE);
    l2.setOpaque(true);
    
    l2.setBorder(new EmptyBorder(5,10,5,10));
    
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat time = new SimpleDateFormat("HH:MM");
    
    JLabel l3 = new JLabel();
    
    l3.setText(time.format(cal.getTime()));
    //l3.setBackground(new Color(200,200,200));
    //l3.setOpaque(true);
    p3.add(l2);
    p3.add(l3);
    return p3;
  }
    
  public static JPanel textarea_panel2(String temp)
  {
    JPanel p3 = new JPanel();
    p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS));
    JLabel l2 = new JLabel("

"+temp+"

");
    l2.setFont(new Font("SAN_SERIF",Font.PLAIN,16));
    l2.setBackground(new Color(200,200,200));
    l2.setOpaque(true);
    l2.setBorder(new EmptyBorder(5,10,5,10));
    
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat time = new SimpleDateFormat("HH:MM");
    
    JLabel l3 = new JLabel();
    
    l3.setText(time.format(cal.getTime()));
    
    p3.add(l2);
    p3.add(l3);
    return p3;
  }
  
  public static void main(String args[])
  {
    new user_2();
    
    String input = "";
    try {
      
    while(true)
    {
    socket = new Socket("127.0.0.1",6000);
    d_in = new DataInputStream(socket.getInputStream());
    d_out = new DataOutputStream(socket.getOutputStream());
    
    
    
    while(true)
    {
    input = d_in.readUTF();
    
    JPanel temp = textarea_panel2(input);
    JPanel left = new JPanel(new BorderLayout());
    left.add(temp,BorderLayout.LINE_START);
    box.add(left);
    box.add(Box.createRigidArea(new Dimension(0,10)));
    ta1.add(box,BorderLayout.PAGE_START);
    f.validate();
    }
    }
    }
    catch(Exception e)
    {
      System.out.println(e);
    }
    
  }

}

 

 

 

Below are the operations involved in the working of the application

 

  1. There will be two applications namely server and client.

 

  1. First, the server application is run as it is required to accept the connection request from the clients.

 

  1. The server will be assigned with the port to receive requests from the clients.

 

  1. Then the client application is run and a local port number bound by a socket will be assigned along with sending connection requests to the server.

 

  1. A connection will be established between server and client.

 

  1. The text message can be typed in the text field below and sent by pressing the send button next to it.

 

  1. The message will be encoded using writeUTF(message) and decoded by the other party using readUTF(message).

 

Output :

output

 

 

CONCLUSION

This project is a basic chatting application with all the necessary functionalities like sending text messages between server and client, speech bubble feature, scrollbar, etc, built using Java Swing, Java AWT, Java Networking. This is a desktop-based application with unique features and is easily customizable. Java.net package has been used which provides a wide range of classes for developing networking applications. The most commonly used networking protocols for messaging over a network are TCP and UDP. TCP is a connection-oriented protocol that enables applications to exchange messages over the network. Alternative to TCP is UDP as it is a low latency protocol with less transmission time.

 

FUTURE WORK               

This project is just an attempt to learn GUI design using Java Swing, Java AWT, and Networking using Socket programming. Many functionalities mentioned below can be added to its functionality.

 

  1. File transfer.
  2. Backing up of messages.
  3. Group chat option.
  4. User registration.
  5. Voice chat.

 

 

 

 

 

Download project

Reviews Report

Submitted by Akash N (akashn162000)

Download packets of source code on Coders Packet