Client-Server Chat Program in Java Java by Rajesh Kumar Sahanee - February 7, 2019September 2, 20220 Post Views: 7,836 Hello Friends, I hope you are doing fine. Today I am going to share Client-Server Chat Program in Java. I have developed this program using socket programming when I was learning Java and just a day ago I found it in my backups. Now I am sharing it, so that anyone need it as a feature in their software/application, can use it. So, Here is the code.. Server.java Server.java Java import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; public class Server extends Frame implements ActionListener, Runnable { int port = 4444; ServerSocket serverSocket; Socket socket; Label statusL; List chatsL; Panel controlsP; TextField messageTF; Button sendB, exitB; BufferedReader bufferedReader; BufferedWriter bufferedWriter; public Server(String m) { super(m); //creating controls objects statusL = new Label("Status"); chatsL = new List(); messageTF = new TextField(20); controlsP = new Panel(); sendB = new Button("Send"); exitB = new Button("Exit"); //adding controls to Panel controlsP.setLayout(new FlowLayout()); controlsP.add(messageTF); controlsP.add(sendB); controlsP.add(exitB); //adding to controls and panel to Frame add(statusL, BorderLayout.NORTH); add(chatsL, BorderLayout.CENTER); add(controlsP, BorderLayout.SOUTH); //adding listeners on Send Button and Exit Button sendB.addActionListener(this); exitB.addActionListener(this); setSize(300, 300);//setting size of frame/window setLocation(0, 0);//setting location of frame/window on the screen setBackground(Color.YELLOW);//setting background for frame/window setVisible(true);//setting it to visible state listen(); } public void listen() { try { serverSocket = new ServerSocket(port); statusL.setText("Listening on ip:" + serverSocket.getInetAddress().getHostAddress() + " and port:" + port); socket = serverSocket.accept(); bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); bufferedWriter.write("Hello"); bufferedWriter.newLine(); bufferedWriter.flush(); Thread th; th = new Thread(this); th.start(); } catch (Exception e) { statusL.setText(e.getMessage()); } } public void actionPerformed(ActionEvent e) { if (e.getSource().equals(exitB)) { System.exit(0); } else { try { bufferedWriter.write(messageTF.getText()); bufferedWriter.newLine(); bufferedWriter.flush(); chatsL.add("You: " + messageTF.getText()); messageTF.setText(""); } catch (IOException ioe) { statusL.setText(ioe.getMessage()); } } } public void run() { try { socket.setSoTimeout(1000); } catch (Exception e) { } statusL.setText("Client Connected"); while (true) { try { String message = bufferedReader.readLine(); if(message == null) { serverSocket.close(); break; } chatsL.add("Client: " + message); } catch (IOException ioe) { } } listen(); } public static void main(String[] ar) { new Server("Server Program"); } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 import java.net.*;import java.io.*;import java.awt.*;import java.awt.event.*; public class Server extends Frame implements ActionListener, Runnable { int port = 4444; ServerSocket serverSocket; Socket socket; Label statusL; List chatsL; Panel controlsP; TextField messageTF; Button sendB, exitB; BufferedReader bufferedReader; BufferedWriter bufferedWriter; public Server(String m) { super(m); //creating controls objects statusL = new Label("Status"); chatsL = new List(); messageTF = new TextField(20); controlsP = new Panel(); sendB = new Button("Send"); exitB = new Button("Exit"); //adding controls to Panel controlsP.setLayout(new FlowLayout()); controlsP.add(messageTF); controlsP.add(sendB); controlsP.add(exitB); //adding to controls and panel to Frame add(statusL, BorderLayout.NORTH); add(chatsL, BorderLayout.CENTER); add(controlsP, BorderLayout.SOUTH); //adding listeners on Send Button and Exit Button sendB.addActionListener(this); exitB.addActionListener(this); setSize(300, 300);//setting size of frame/window setLocation(0, 0);//setting location of frame/window on the screen setBackground(Color.YELLOW);//setting background for frame/window setVisible(true);//setting it to visible state listen(); } public void listen() { try { serverSocket = new ServerSocket(port); statusL.setText("Listening on ip:" + serverSocket.getInetAddress().getHostAddress() + " and port:" + port); socket = serverSocket.accept(); bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); bufferedWriter.write("Hello"); bufferedWriter.newLine(); bufferedWriter.flush(); Thread th; th = new Thread(this); th.start(); } catch (Exception e) { statusL.setText(e.getMessage()); } } public void actionPerformed(ActionEvent e) { if (e.getSource().equals(exitB)) { System.exit(0); } else { try { bufferedWriter.write(messageTF.getText()); bufferedWriter.newLine(); bufferedWriter.flush(); chatsL.add("You: " + messageTF.getText()); messageTF.setText(""); } catch (IOException ioe) { statusL.setText(ioe.getMessage()); } } } public void run() { try { socket.setSoTimeout(1000); } catch (Exception e) { } statusL.setText("Client Connected"); while (true) { try { String message = bufferedReader.readLine(); if(message == null) { serverSocket.close(); break; } chatsL.add("Client: " + message); } catch (IOException ioe) { } } listen(); } public static void main(String[] ar) { new Server("Server Program"); }} Client.java Client.java Java import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; public class Client extends Frame implements ActionListener, Runnable { Socket socket; Panel topControlsP, bottomControlsP; List chatsL; TextField ipTF, portTF, messageTF; Button connectB, sendB, exitB; BufferedReader bufferedReader; BufferedWriter bufferedWriter; Thread th; public Client(String str) { super(str); //creating controls objects topControlsP = new Panel(); ipTF = new TextField(15); portTF = new TextField(5); connectB = new Button("Connect"); chatsL = new List(); bottomControlsP = new Panel(); messageTF = new TextField(20); sendB = new Button("Send"); exitB = new Button("Exit"); //adding controls to Panel topControlsP.add(ipTF); topControlsP.add(portTF); topControlsP.add(connectB); //adding controls to Panel bottomControlsP.add(messageTF); bottomControlsP.add(sendB); bottomControlsP.add(exitB); //adding to controls and panel to Frame add(topControlsP, BorderLayout.NORTH); add(chatsL, BorderLayout.CENTER); add(bottomControlsP, BorderLayout.SOUTH); //adding listeners on Connect, Send and Exit Button connectB.addActionListener(this); sendB.addActionListener(this); exitB.addActionListener(this); setSize(300, 300);//setting size of frame/window setLocation(300, 0);//setting location of frame/window on the screen setBackground(Color.MAGENTA);//setting background for frame/window setVisible(true);//setting it to visible state } public void actionPerformed(ActionEvent event) { if (event.getSource().equals(exitB)) { System.exit(0); } else if (event.getSource().equals(connectB)) { try { socket = new Socket(ipTF.getText(), Integer.parseInt(portTF.getText())); bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); ipTF.setText("Connected"); th = new Thread(this); th.start(); } catch (IOException ioe) { ipTF.setText(ioe.getMessage()); } } else { try { if (bufferedWriter != null) { bufferedWriter.write(messageTF.getText()); bufferedWriter.newLine(); bufferedWriter.flush(); chatsL.add("You: " + messageTF.getText()); messageTF.setText(""); } } catch (IOException ioe) { ipTF.setText(ioe.getMessage()); } } } public void run() { try { socket.setSoTimeout(1); } catch (Exception e) { ipTF.setText(e.getMessage()); } while (true) { try { String message = bufferedReader.readLine(); if(message == null) { break; } chatsL.add("Server: " + message); } catch (Exception e) { } } } public static void main(String[] ar) { new Client("Client Program"); } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 import java.net.*;import java.io.*;import java.awt.*;import java.awt.event.*; public class Client extends Frame implements ActionListener, Runnable { Socket socket; Panel topControlsP, bottomControlsP; List chatsL; TextField ipTF, portTF, messageTF; Button connectB, sendB, exitB; BufferedReader bufferedReader; BufferedWriter bufferedWriter; Thread th; public Client(String str) { super(str); //creating controls objects topControlsP = new Panel(); ipTF = new TextField(15); portTF = new TextField(5); connectB = new Button("Connect"); chatsL = new List(); bottomControlsP = new Panel(); messageTF = new TextField(20); sendB = new Button("Send"); exitB = new Button("Exit"); //adding controls to Panel topControlsP.add(ipTF); topControlsP.add(portTF); topControlsP.add(connectB); //adding controls to Panel bottomControlsP.add(messageTF); bottomControlsP.add(sendB); bottomControlsP.add(exitB); //adding to controls and panel to Frame add(topControlsP, BorderLayout.NORTH); add(chatsL, BorderLayout.CENTER); add(bottomControlsP, BorderLayout.SOUTH); //adding listeners on Connect, Send and Exit Button connectB.addActionListener(this); sendB.addActionListener(this); exitB.addActionListener(this); setSize(300, 300);//setting size of frame/window setLocation(300, 0);//setting location of frame/window on the screen setBackground(Color.MAGENTA);//setting background for frame/window setVisible(true);//setting it to visible state } public void actionPerformed(ActionEvent event) { if (event.getSource().equals(exitB)) { System.exit(0); } else if (event.getSource().equals(connectB)) { try { socket = new Socket(ipTF.getText(), Integer.parseInt(portTF.getText())); bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); ipTF.setText("Connected"); th = new Thread(this); th.start(); } catch (IOException ioe) { ipTF.setText(ioe.getMessage()); } } else { try { if (bufferedWriter != null) { bufferedWriter.write(messageTF.getText()); bufferedWriter.newLine(); bufferedWriter.flush(); chatsL.add("You: " + messageTF.getText()); messageTF.setText(""); } } catch (IOException ioe) { ipTF.setText(ioe.getMessage()); } } } public void run() { try { socket.setSoTimeout(1); } catch (Exception e) { ipTF.setText(e.getMessage()); } while (true) { try { String message = bufferedReader.readLine(); if(message == null) { break; } chatsL.add("Server: " + message); } catch (Exception e) { } } } public static void main(String[] ar) { new Client("Client Program"); }} Output Please don’t forget to share if you find it interesting