Searching...
Sunday 8 June 2014

FREE CHAT SERVER MAKE USING JAVA

6/08/2014 08:52:00 am

ChatServer.java

 

package chatting;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

 

public class ChatServer extends Frame {                           

    private static class WI extends WindowAdapter {

        public void windowClosing(WindowEvent e) {

            System.exit(0);

        }      

    }

    static TextArea ta;

    PrintWriter pw;

    BufferedReader br;

    static  Socket clientSocket = null;

    static  ServerSocket serverSocket = null;

    static  clientThread1 t[] = new clientThread1[10];

    public ChatServer(){

        Frame f = new Frame("ServerSide Chating");

        f.setLayout(new FlowLayout());

        f.setBackground(Color.orange);

        ta = new TextArea(23,36);

        ta.setBackground(Color.cyan);

        ta.setText("\nWelcome To Server..." + "\nList Of Online User..\n\n");

        f.addWindowListener(new WI());

        f.add(ta);       

        f.setSize(300, 400);

        f.setLocation(300, 300);

        f.setVisible(true);

        f.validate();

        try{

            serverSocket = new ServerSocket(12000);

            while(true){

                try {

                    clientSocket = serverSocket.accept();

                    for(int i=0; i<=9; i++){

                        if(t[i]==null){

                                (t[i] = new clientThread1(clientSocket,t)).start();

                                break;

                            }

                    }

                }catch (IOException e) {

                    System.out.println(e);}

            }

        }

        catch(Exception e){}                     

    }

    public static void setUName(String Name){

        ta.append("\nUser "+ Name  + " is Now OnLine." );

    }

    public static void setofline(String Name){

        ta.append("\nUser "+ Name  + " is Now OffLine." );

    }

    public static void main(String args[]){

        ChatServer a = new ChatServer();

    }

}

class clientThread1 extends Thread{   

    DataInputStream is = null;

    PrintStream os = null;

    Socket clientSocket = null;      

    clientThread1 t[];    

    public clientThread1(Socket clientSocket, clientThread1[] t){

            this.clientSocket=clientSocket;

        this.t=t;

    }   

    public void run(){

        String line;

        String name;

            try{

                is = new DataInputStream(clientSocket.getInputStream());

                os = new PrintStream(clientSocket.getOutputStream());

                os.println("Enter your name.");

                name = is.readLine();

            ChatServer.setUName(name);

                os.println("Hello "+name+",Welcome To Our Chat room.\n To Leave Enter /exit in a New Line.");

                for(int i=0; i<=9; i++)

                        if (t[i]!=null && t[i]!=this) 

                            t[i].os.println("***** A New User "+ name+" is Now Online.");

                while (true) {

                        line = is.readLine();

                if(line.startsWith("/exit")) break;

                        for(int i=0; i<=9; i++)

                            if (t[i]!=null)  t[i].os.println("<"+name+"> "+line);

                }

                for(int i=0; i<=9; i++)

    if (t[i]!=null && t[i]!=this) 

                {

                            t[i].os.println("***** The user "+name+" is Now OffLine.");                   

                }

            ChatServer.setofline(name);  

                for(int i=0; i<=9; i++)

                        if (t[i]==this) t[i]=null;    

                is.close();

                os.close();

                clientSocket.close();           

            }

            catch(IOException e){};

    }

}

 

ChatClient.java

 

package chatting;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

import javax.swing.JOptionPane;

 

public class ChatClient extends Frame implements ActionListener{   

    private static class WI extends WindowAdapter {      

        public void windowClosing(WindowEvent e) {

            System.exit(0);

        } 

    }

    Button b1;

    TextField tf;

    static TextArea ta;

    Socket s;

    PrintWriter pw;

    BufferedReader br;

    Thread th;

    static Socket clientSocket = null;

    static PrintStream os = null;

    static DataInputStream is = null;

    static BufferedReader inputLine = null;

    static Frame f;

    public ChatClient(){

        f= new Frame("ClientSide Chating");

        f.setLayout(new FlowLayout());

        f.setBackground(Color.orange);

        b1 = new Button("Send");

        b1.setBackground(Color.pink);

        b1.addActionListener(this);

        //b1.addKeyListener(this);

        tf = new TextField(29);

        //tf.addKeyListener(this);

        ta = new TextArea(20,36);

        ta.setBackground(Color.cyan);

        f.addWindowListener(new WI());

        f.add(tf);

        f.add(b1);

        f.add(ta);

        f.setSize(300, 400);

        f.setLocation(300, 300);

        f.setVisible(true);

        f.validate();

        int port_number=12000;

        String host="localhost";

        try {

            clientSocket = new Socket(host, port_number);

            inputLine = new BufferedReader(new InputStreamReader(System.in));

            os = new PrintStream(clientSocket.getOutputStream());

            is = new DataInputStream(clientSocket.getInputStream());

        } catch (UnknownHostException e) {

            System.err.println("Don't know about host "+host);

        } catch (IOException e) {

            System.err.println("Couldn't get I/O for the connection to the host "+host);

        }          

        if (clientSocket != null && os != null && is != null) {

            try {

                    new Thread(new ThreadChat1()).start();                         

            } catch (Exception e) {

                System.err.println("IOException:  " + e); } }      

    }

    public static void setChat(String str){

        ta.append("\n"+str);

    }

    public static void setLName(String n){

        f.setName(n);

    }

    public static void exittru(boolean flag){

        if(flag==true){

            ChatClient.WI wi=new ChatClient.WI();

            wi.windowClosing(null);

        }    }

    public void actionPerformed(ActionEvent e) {

        os.println(tf.getText());

        tf.setText("");

    }

    public static void main(String args[]){

        ChatClient a = new ChatClient();

    }}

class ThreadChat1 implements Runnable{

    String name;   

    public void run() {             

            String responseLine;  

            try{

                while ((responseLine = ChatClient.is.readLine()) != null) {

                        if (responseLine.indexOf("Enter your name.") != -1){

                    name = JOptionPane.showInputDialog(null,"Enter Name : ","Chatting",1);

                    ChatClient.os.println(name);

                    ChatClient.setLName(name);

                }

                else if (responseLine.indexOf("*** Bye") != -1) break;

                else

                    ChatClient.setChat(responseLine);

                }           

            ChatClient.os.close();

            ChatClient.is.close();

            ChatClient.clientSocket.close();

            ChatClient.exittru(true);

            } catch (Exception e) {

                System.err.println("IOException:  " + e);

    }


}}

0 comments:

Post a Comment