Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Chatting application in java (PUblic And private)

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Chatting application in java (PUblic And private)

    Problem :exception occurs but program run
    3 class SERVER CLIENT and MESSAGE BEING PASSED
    ///////////////////////message////////////1st make packge chat and import u can use message class in cLIENT AND server as welll

    package chat;

    import java.util.*;
    import java.net.*;
    import java.io.*;

    public class Message implements Serializable

    {
    public String uname="name0";
    public String data="data";
    public String destination="destinatiom";
    // public String sp[]={"ram"};
    public String sp[];
    public Hashtable<String,Socket> tl=new Hashtable<String,Socket>();
    public Message()
    {}
    public Message(String dat)
    {
    data=dat;
    }
    public Message(String dest,String sms)
    {
    destination=dest;
    data=sms;
    }


    }
    /////////////////////////////////////////////////////server//////////////////
    import chat.Message;
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    // Java extension packages
    import javax.swing.*;

    public class Server extends JFrame implements Serializable
    {
    private JTextField enterField;
    public JTextArea displayArea;
    private ObjectOutputStream output;
    ObjectInputStream input;
    private ServerSocket server;
    private Socket connection;
    private int count = 1;
    static Hashtable<String,Socket> h1=new Hashtable<String,Socket>();
    // set up GUI///////////////////////////////////////////////////////////////////////////////////
    public Server()
    {
    super( "Server" );

    Container container = getContentPane();

    // create enterField and register listener
    enterField = new JTextField();
    enterField.setEnabled( true );
    ////////////////////////////////////////////////



    ////////////////////////////////////////////////////
    container.add( enterField, BorderLayout.NORTH );

    // create displayArea
    displayArea = new JTextArea();
    container.add( new JScrollPane( displayArea ),
    BorderLayout.CENTER );

    setSize( 300, 150 );
    setVisible( true );

    }



    public void runServer()
    {
    try {


    server = new ServerSocket( 5000, 100 );

    while ( true )
    {

    displayArea.setText( "Waiting for connection\n" );

    connection = server.accept();

    try{
    ObjectInputStream in=new ObjectInputStream(connection.getInputStream());
    Message m =(Message)in.readObject();
    String s= m.uname;
    h1.put(s,connection);
    sendList(h1);

    Thread t = new Thread(new ClientHandler(connection) );
    t.start();
    }catch(Exception e)
    {}


    }
    }
    catch ( EOFException eofException )
    {
    System.out.println( "Client terminated connection" );
    }

    catch ( IOException ioException )
    {
    ioException.printStackTrace();
    }
    }
    /////////////////////////////////////////////////////////////////////

    synchronized void sendData(Message m)throws IOException
    {displayArea.append( "in send data");
    String s=m.destination;
    Set<String> set =h1.keySet();
    Iterator<String> i= set.iterator();
    while(i.hasNext())
    {System.out.println(i.next());
    String s1=i.next();
    if(s1.equals(s))
    {Socket ss=(Socket)h1.get(s1);
    output=new ObjectOutputStream(ss.getOutputStream());
    output.writeObject(m);
    }
    }
    }

    ///////////////////////////////////////////////////
    void sendList( Hashtable h1)
    {

    String so[]=new String[100];

    int ii=0;
    displayArea.append( "in list");
    Message ms=new Message();

    /////////////////
    Set<String> set =h1.keySet();
    Iterator<String> i= set.iterator();
    while(i.hasNext())
    {System.out.println(i.next());
    String s1=i.next();
    so[ii++]=s1;
    }

    ms.sp=so;



    ////////////////;
    try{

    while(i.hasNext())
    {System.out.println(i.next());
    String s1=i.next();

    Socket ss=(Socket)h1.get(s1);
    output=new ObjectOutputStream(ss.getOutputStream());
    output.writeObject(ms);

    }

    output.flush();
    }catch(IOException e)
    {
    System.out.println(e);
    }

    }

    //////////////////////////////////////////////////////////////////


    ////////////////////////////////////////////////////////////////////
    // execute application
    public static void main( String args[] )
    {
    Server application = new Server();

    application.setDefaultCloseOperation(
    JFrame.EXIT_ON_CLOSE );

    while(true)
    {application.runServer();}
    }
    }
    ///////////////////////////////////
    /////////////////////thread ////////////////////////
    class ClientHandler implements Runnable
    {
    ObjectOutputStream out;
    ObjectInputStream input;
    Socket sock;
    Server sr=new Server();
    public ClientHandler(Socket clientSocket)
    {
    sock=clientSocket;
    try
    {out = new ObjectOutputStream( clientSocket.getOutputStream() );
    Message m=new Message();
    m.data="yes sir";
    out.writeObject(m);
    sr.displayArea.append( "Connection " + " received from: "+sock.getInetAddress() );
    }catch(Exception e)
    {}




    } // end of constructor of ClientHandler class...
    /////////////////////thread workiing/////////////////////////////////

    public void run()
    {
    sr.displayArea.append( "in run");
    try
    { Message n= new Message();
    Message m1=(Message)input.readObject();


    sr.h1.put(m1.uname,sock) ;
    //boolean t=!(m1.destination.equals("destination"));
    //boolean t1=!(m1.destination.equals("jatin"));
    //f(t)
    sr.sendData(m1);
    }catch(Exception ex)
    {
    System.out.println("run : "+ex.toString() );
    ex.printStackTrace();

    }

    }

    }

    ////////////////////////////////////////////////////////////////client
    import chat.Message;
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;

    // Java extension packages
    import javax.swing.*;

    public class Client extends JFrame implements Serializable {
    private JTextField enterField;
    private JTextArea displayArea;
    private ObjectOutputStream output;
    private ObjectInputStream input;

    private String chatServer;
    private Socket client;
    String name = JOptionPane.showInputDialog(null, "Enter some text : ", "enter name");

    // initialize chatServer and set up GUI
    public Client( String host )
    {
    super( "Client" );

    // set server to which this client connects
    chatServer = host;

    Container container = getContentPane();

    // create enterField and register listener
    enterField = new JTextField();
    enterField.setEnabled( false );

    enterField.addActionListener(

    new ActionListener() {

    // send message to server
    public void actionPerformed( ActionEvent event )
    {
    sendData( enterField.getText() );

    }

    } // end anonymous inner class

    ); // end call to addActionListener

    container.add( enterField, BorderLayout.NORTH );

    // create displayArea
    displayArea = new JTextArea();
    container.add( new JScrollPane( displayArea ),
    BorderLayout.CENTER );

    setSize( 300, 150 );
    setVisible( true );
    }

    // connect to server and process messages from server
    public void runClient()
    {
    // connect to server, get streams, process connection
    try {

    // Step 1: Create a Socket to make connection
    connectToServer();


    // Step 3: Process connection
    processConnection();

    // Step 4: Close connection
    closeConnection();

    }
    // server closed connection
    catch ( EOFException eofException ) {
    System.out.println( "Server terminated connection" );
    }

    // process problems communicating with server
    catch ( IOException ioException ) {
    ioException.printStackTrace();
    }
    }

    // get streams to send and receive data


    // connect to server
    private void connectToServer() throws IOException
    {
    try { displayArea.setText( "Attempting connection\n" );

    // create Socket to make connection to server
    client = new Socket(
    InetAddress.getByName( chatServer ), 5000 );

    // display connection information
    displayArea.append( "Connected to: " +
    client.getInetAddress().getHostName() );
    ObjectOutputStream os=new ObjectOutputStream(client.getOutputStream());
    Message m=new Message();
    m.uname=name;
    m.destination="jatin";
    m.data="ho gayay connect";
    os.writeObject(m);
    }catch(Exception e)
    {System.out.println("hiiiiii");
    }
    }

    // process connection with server
    private void processConnection() throws IOException
    {
    // enable enterField so client user can send messages
    enterField.setEnabled( true );

    // process messages sent from server

    Message message=new Message();
    // read message and display it
    try {
    displayArea.append( "in process"+message.data );
    String n[]=new String[100];
    Message mn= ( Message ) input.readObject();
    System.out.println(mn.uname+mn.data);
    if (mn!=null)displayArea.append( "\n" + mn.uname+mn.data );
    else System.out.println("aur khal ha");
    if(mn.sp!=null)
    {
    n=mn.sp;

    }
    for(String z:n)
    System.out.println(z);
    displayArea.setCaretPosition(
    displayArea.getText().length() );
    }

    // catch problems reading from server





    catch ( Exception e )
    { System.out.println(e);
    e.printStackTrace();
    displayArea.append( "problem in server" );
    }





    } // end method process connection

    // close streams and socket
    private void closeConnection() throws IOException
    {
    displayArea.append( "\nClosing connection" );
    output.close();
    input.close();
    client.close();
    }

    // send message to server
    private void sendData( String me )
    {
    // send object to server do
    while(!me.equals("\n"))
    {

    Message message=new Message();
    message.data=me;
    message.uname=name;
    message.destination="jatin";
    System.out.println(me+name);
    try {
    output.writeObject( message );
    output.flush();
    displayArea.append( "me" + message.data );
    }catch ( IOException ioException ) {
    displayArea.append( "\nError writing object" );
    }
    }
    }

    // execute application
    public static void main( String args[] )
    {
    ;
    Client application;

    if ( args.length == 0 )
    application = new Client( "127.0.0.1" );
    else
    application = new Client( args[ 0 ] );

    application.setDefaultCloseOperation(
    JFrame.EXIT_ON_CLOSE );

    application.runClient();
    }

    }

    [/B][/B][/B][/B][/B]


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Chatting application in java (PUblic And private)

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    Please post the full text of the error messages.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. problem chatting with smack api with openfire server
    By calicratis19 in forum Java SE APIs
    Replies: 2
    Last Post: November 14th, 2013, 12:24 PM
  2. Convert string to private and public key
    By Ganezan in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 5th, 2010, 08:37 AM
  3. Java chatting program implementing socket logic
    By sivakrishna.g in forum Java Networking
    Replies: 2
    Last Post: October 20th, 2009, 12:47 AM
  4. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM
  5. [SOLVED] Difference between public and private variable and their uses
    By napenthia in forum Java Theory & Questions
    Replies: 1
    Last Post: April 22nd, 2009, 11:36 AM