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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
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
Comments