+92 332 4229 857 99ProjectIdeas@Gmail.com

Chat Server (Java)



The following code shows you how to make a Chat Server which can run on LAN, its a simple GUI based.

Code


import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

public class ChatServer extends JFrame
                        implements ActionListener {
      
       ServerSocket ss;
       Socket s;
       PrintWriter pw1;
       JTextField txtMsg;
       JButton btnSnd;
       List list;
       Container con;
       String recMsg,sendMsg;
      
       public ChatServer() {

         try 
             {

              createComponents();
              ss = new ServerSocket(333);
              s = ss.accept();
              getMessage();
              
           } catch ( Exception ex ) {

              }
       }
      

       //Function To Read Message By The Sender
       public void getMessage(){
             
       try
           {
      
              InputStream is= s.getInputStream();
              InputStreamReader isr = new InputStreamReader(is);
              BufferedReader br = new BufferedReader(isr);
              recMsg = br.readLine();
              recMsg = "Client Says : " + recMsg;
              list.addItem(recMsg);
             
              } catch (Exception ex) {
                    
              }
       }
       
       //Function To Send Message To Connected User
       public void sendMessage(String msg1){
      
       try
           {
             
              OutputStream os = s.getOutputStream();
              pw1 = new PrintWriter(os,true);
              pw1.println("Admin Says : " + msg1);
              list.addItem("Admin Says : " + msg1);
              txtMsg.setText("");
             
       } catch ( Exception ex ) {
                          
       }
     }
      
       //GUI Of Server
       public void createComponents(){
             
              btnSnd = new JButton("Send");
              list = new List();
              txtMsg = new JTextField();
              con = getContentPane();
              con.setLayout(new BorderLayout());
              con.add(list,"Center");
              con.add(btnSnd,"East");
              con.add(txtMsg,"South");
              setTitle("Server");
              setResizable(false);
              setVisible(true);
              setSize(400,400);
             
              btnSnd.addActionListener(this);
             
              //Checking if user pressed "Enter Key"
              txtMsg.addKeyListener(new KeyAdapter() {
                     public void keyReleased(KeyEvent ke){
                       try {
                      
                           if ( ke.getKeyCode() == KeyEvent.VK_ENTER ) {
                                        
                                  sendMessage(txtMsg.getText());
                                 
                           }
                                 
                           } catch (Exception ex) {
                                   ex.printStackTrace();
                           }
                     }
       });

              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
      
       //If "send button" is Pressed
       public void actionPerformed(ActionEvent ae){
               
              try
                 {
                      if ( ae.getSource().equals(btnSnd) ) {
                            
                            sendMsg = txtMsg.getText();
                            OutputStream os = s.getOutputStream();
                            pw1 = new PrintWriter(os,true);
                            pw1.println(txtMsg.getText());
                            list.addItem("Admin Says : " + sendMsg);
                            txtMsg.setText("");
                      }
                      
                 } catch (Exception ex ) {

                      }
               }
      
       public static void main(String[] args){
             
              ChatServer ch =  new ChatServer();
             
       }

}

0 comments: