Friday, June 15, 2012

java client server chat



// server application**************************

import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class serverapp extends JApplet implements ActionListener,Runnable

{
    // Basic Components
    JPanel panel = new JPanel();
    List list = new List(10);
    JTextField textfield = new JTextField();
    JButton button = new JButton();
    Font ver = new Font("Verdana",1,12);
    Font ver2 = new Font("Verdana",1,14);
    
    // Advanced Components
    String feel[] = {"I Feel ...","Sad","Happy","Angry","Love","Cool","Sorry","Thankful"};
    JComboBox feelings = new JComboBox(feel);
    JButton sendfeel = new JButton();
    JButton exit = new JButton();
    JLabel abuse = new JLabel("PLEASE DO NOT ABUSE ...........");
    
    // All Strings
    String name = "Mr X";
    String name2;
    String texta;
    String feels;
    String send;
    String recive;
    
    // Connectivity
    ServerSocket server;
    BufferedReader fromServername;
    PrintStream toServername;
    BufferedReader fromServermsg;
    PrintStream toServermsg;
    BufferedReader fromServermsg2;
    PrintStream toServermsg2;
    Socket client;
    
    // Threads
    Thread t1 = new Thread(this);
    Messages msg;
    Thread t2 = new Thread(msg);
    
    public void init()
    {
        getContentPane().add(panel);
        panel.setLayout(null);
        panel.setBackground(new Color(242,167,0));
        
        try
        {
        server = new ServerSocket(1001);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        
        name = JOptionPane.showInputDialog("Enter You Name");
        if ((name==null) || name.equals(""))
        {
            name = "Mr. X";
            System.out.println(name);
        }
        
        panel.add(list);
        list.setBounds(5,5,450,250);
        list.setFont(ver);
        list.add("Chat Applet By Neville Mehta - neville.m@usa.net");
        list.add(" ");
        
        panel.add(textfield);
        textfield.setBounds(5,265,450,25);
        textfield.setFont(ver);
        textfield.setText("Type The Text Here");
        
        panel.add(button);
        button.setBounds(5,300,450,25);
        button.setText(name + " Says");
        button.setFont(ver);
        button.addActionListener(this);
        
        panel.add(feelings);
        feelings.setBounds(5,335,120,25);
        feelings.setFont(ver);
        panel.add(sendfeel);
        sendfeel.setBounds(135,335,150,25);
        sendfeel.setFont(ver);
        sendfeel.setText("Send Feelings");
        sendfeel.addActionListener(this);
        
        panel.add(exit);
        exit.setBounds(385,335,70,25);
        exit.setFont(ver);
        exit.setText("Exit");
        exit.addActionListener(this);
        
        //panel.add(abuse);
        //abuse.setBounds(5,370,450,25);
        //abuse.setFont(ver2);
        //abuse.setForeground(Color.blue);
        
        t1.start();
    }
    
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == button)
        {
            texta = textfield.getText();
             
            
            
            list.add(name + " >> " + texta);
            
            
            try
            {
                toServername.println(texta);
                System.out.println("Sent");
            }
            catch(Exception yu)
            {System.out.println(yu);}
            
        }
        
        if (e.getSource() == exit)
        {
            System.exit(0);
        }
        
        if (e.getSource() == sendfeel)
        {
            if (feelings.getSelectedItem().equals("I Feel ..."))
            {
                return;
            }
            
            feels = String.valueOf(feelings.getSelectedItem());
            list.add(name + " >> " + name + " feels " + feels);
            
            try
            {
                toServername.println(name + " feels " + feels);
                System.out.println("Sent");
            }
            catch(Exception yu)
            {System.out.println(yu);}
        }
        
    }
    
    public void run()
    {
        
        while (true)
        {
            try
            {
            client = server.accept();
            Name fromname = new Name(client);
            msg = new Messages(client);
            msg.run();
            
            }
            catch(Exception er)
            {
                System.out.println(er);
            }
        }
        
    }
    
    public class Name extends Thread
    {
        
        public Name(Socket client)
        {
            try
            {
            fromServername = new BufferedReader(new InputStreamReader(client.getInputStream()));
            toServername = new PrintStream(client.getOutputStream());
            
            // Recive Name
            {
                name2 = fromServername.readLine();
                list.add(name2 + " Enters Chat");
                list.add(" ");
            }
        
            // Send Name
            {
                toServername.println(name);
            }
            
             
            
            }
            catch(Exception e3)
            {
                System.out.println(e3);
            }
        }
        
        public void run()
        {
            
        }
    }
    
    public class Messages extends Thread
    {
        
        public Messages(Socket client)
        {
            try
            {
                fromServermsg = new BufferedReader(new InputStreamReader(client.getInputStream()));
                toServermsg = new PrintStream(client.getOutputStream());
            }
            catch (Exception e45)
            {
                System.out.println(e45);
            }
        }
        
        public void run()
        {
            while (true)
            {
                try
                {
                
                // Recive Messages
                {
                    recive = fromServermsg.readLine();
                    list.add(name2 + " >> " + recive);
                    System.out.println("Reciving Msgs");
                }
                
                // Send Messages
                
                }
                catch(Exception e2)
                {
                    System.out.println(e2);
                }
            }
        }
    
    }
    
    
}
//<applet code="serverapp.class" height=500 width=500></applet>




// client application **************************

import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class Clientapp extends JApplet implements ActionListener,Runnable
{
    // Basic Components
    JPanel panel = new JPanel();
    List list = new List(10);
    JTextField textfield = new JTextField();
    JButton button = new JButton();
    Font ver = new Font("Verdana",1,12);
    Font ver2 = new Font("Verdana",1,14);
    
    // Advanced Components
    String feel[] = {"I Feel ...","Sad","Happy","Angry","Love","Cool","Sorry","Thankful"};
    JComboBox feelings = new JComboBox(feel);
    JButton sendfeel = new JButton();
    JButton exit = new JButton();
    JLabel abuse = new JLabel("PLEASE DO NOT ABUSE ...........");
        
    
    // All Strings
    String name = "Mr Y";
    String name2;
    String texta;
    String feels;
    String send;
    String recive;
    
    // Connectivity
    Socket client;
    BufferedReader fromServername;
    PrintStream toServername;
    BufferedReader fromServermsg;
    PrintStream toServermsg;
    
    // Threads
    Thread t1 = new Thread(this);
    Messages msg = new Messages();
    Thread t2 = new Thread(msg);
    
    public void init()
    {
        getContentPane().add(panel);
        panel.setLayout(null);
        panel.setBackground(new Color(242,167,0));
        
        try
        {
        client = new Socket("127.0.0.1",1001);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        
        name = JOptionPane.showInputDialog("Enter You Name");
        if ((name==null) || name.equals(""))
        {
            name = "Mr. Y";
            System.out.println(name);
        }
        
        panel.add(list);
        list.setBounds(5,5,450,250);
        list.setFont(ver);
        list.add("Chat Applet By Neville Mehta - neville.m@usa.net");
        list.add(" ");
         
        panel.add(textfield);
        textfield.setBounds(5,265,450,25);
        textfield.setFont(ver);
        textfield.setText("Type The Text Here");
        
        panel.add(button);
        button.setBounds(5,300,450,25);
        button.setText(name + " Says");
        button.setFont(ver);
        button.addActionListener(this);
        
        panel.add(feelings);
        feelings.setBounds(5,335,120,25);
        feelings.setFont(ver);
        panel.add(sendfeel);
        sendfeel.setBounds(135,335,150,25);
        sendfeel.setFont(ver);
        sendfeel.setText("Send Feelings");
        sendfeel.addActionListener(this);
        
        panel.add(exit);
        exit.setBounds(385,335,70,25);
        exit.setFont(ver);
        exit.setText("Exit");
        exit.addActionListener(this);
        
        //panel.add(abuse);
        //abuse.setBounds(5,370,450,25);
        //abuse.setFont(ver2);
        //abuse.setForeground(Color.blue);
        
        
        
        t1.start();
    }
    
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == button)
        { 
            texta = textfield.getText();
            
            
            /*{
                texta = "I am an engineer";
            }*/
            
            
            list.add(name + " >> " + texta);
            
            try
            {
                fromServermsg = new BufferedReader(new InputStreamReader(client.getInputStream()));
                toServermsg = new PrintStream(client.getOutputStream());
                 
                toServermsg.println(texta);
                System.out.println("sent");
            }
            catch(Exception vb)
            {System.out.println(vb);}
            
        }
        
        if (e.getSource() == exit)
        {
            System.exit(0);
        }
        
        if (e.getSource() == sendfeel)
        {
            if (feelings.getSelectedItem().equals("I Feel ..."))
            {
                return;
            }
            
            feels = String.valueOf(feelings.getSelectedItem());
            list.add(name + " >> " + name + " feels " + feels);
            
            try
            {
                fromServermsg = new BufferedReader(new InputStreamReader(client.getInputStream()));
                toServermsg = new PrintStream(client.getOutputStream());
                
                toServermsg.println(name + " feels " + feels);
                System.out.println("sent feel");
            }
            catch(Exception vb)
            {System.out.println(vb);}
        }
        
    }
    
    public void run()
    {
        try
        {
        fromServername = new BufferedReader(new InputStreamReader(client.getInputStream()));
        toServername = new PrintStream(client.getOutputStream());
        
        // Send Name
        {
            toServername.println(name);
        }
        
        // Recive Name
        {
            name2 = fromServername.readLine();
            list.add(name2 + " Enters Chat");
            list.add(" ");
        }
        
        }
        catch(Exception e3)
        {
            System.out.println(e3);
        }
        
        t2.start();
        
    }
    
    public class Messages extends Thread
    {
        public void run()
        {
            while (true)
            {
                try
                {
                fromServermsg = new BufferedReader(new InputStreamReader(client.getInputStream()));
                toServermsg = new PrintStream(client.getOutputStream());
                 
                // Recive Messages
                { 
                    recive = fromServermsg.readLine();
                    list.add(name2 + " >> " + recive);
                }
                
                }
                catch(Exception e2)
                {
                    System.out.println(e2);
                }
            }
        }
    }
    
    
}
//<applet code="clientapp.class" height=500 width=500></applet>


No comments:

Post a Comment