Results 1 to 3 of 3
  1. #1
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    288
    Rep Power
    3

    Default identify the user from server program

    Hi!

    I need your guidance..,

    i am doing an multi user chat application, i don't know how to identify the particular pair of user (both end chatting user) among users. and want to send message separately..,

    which steps i have to fallow, HasMap(); or what is the procedure to achieve my target.

    This is my server side program,

    Java Code:
    import java.util.*;
    import java.net.*;
    import java.io.*;
    public class ChatServer {
    
        Vector<String> users = new Vector<String>();
        Vector<HandleClient> clients = new Vector<HandleClient>();
    
        public void process() throws Exception {
            ServerSocket server = new ServerSocket(1234);
            System.out.println("Server Started...");
            while (true) {
                Socket client = server.accept();
                //add incoming client to connected clients vector.
                HandleClient c = new HandleClient(client);
                clients.add(c);
                //System.out.println(Nick_nmae+"  "+c);
            }  // end of while
        }
    
        public static void main(String[] args) throws Exception {
            new ChatServer().process();
        } // end of main
    
        public void broadcast(String user, String message) {
            // send message to all connected users
            for (HandleClient c : clients) {
                if(!message.equals("getusers"))
            {
                         c.sendMessage(user, message);   
            }
            }
        }
    
        /*
         * Inner class, responsible of handling incoming clients.
         * Each connected client will set as it's own thread.
         */
        class HandleClient extends Thread {
    
            String name = "";//client name/username
            BufferedReader input;//get input from client
            PrintWriter output;//send output to client
            boolean pair_status=false;
            String user_one,user_two;
    
            public HandleClient(Socket client) throws Exception {
                // get input and output streams
                input = new BufferedReader(new InputStreamReader(client.getInputStream()));
                output = new PrintWriter(client.getOutputStream(), true);
                // read name
                name = input.readLine();
                users.add(name); // add to users vector
                //broadcast(name, " Has connected!");
                start();
            }
    
            public void sendMessage(String uname, String msg) {
                output.println(uname + ": " + msg);
            }
    
            public void getOnlineUsers() {
                for (HandleClient c : clients) {
                    for (int i = 0; i < users.size(); i++) {
                        broadcast("", users.get(i));
                    }
                }
            }
    
            public String getUserName() {
                return name;
            }
    
            public void run() {
                String line;
                try {
                    while (true) {
                        line = input.readLine();
                        if (line.equals("end")) {
                            //notify all for user disconnection
                            broadcast(name, " Has disconnected!");
                            clients.remove(this);
                            users.remove(name);
                            break;
                        }
                         else if (line.startsWith("pair")) {
                            String new_str[] = line.split(",");
                        for (int i = 0; i < new_str.length; i++) {
                            //taMessages.append(new_str[i] + "\n");
                             user_one=new_str[1];
                             user_two=new_str[2];
                             pair_status=true;
                         }
                            broadcast("Connected with", user_two);
                         }
                        else if(line.equals("getusers")){
                            getOnlineUsers();
                        }
                        else
                        {
                        broadcast(name, line);                       
                        }
     
                       
                    } // end of while
                } // try
                catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            } // end of run()
        } // end of inner class
    }

    Guide me please..,

    Thank You..,
    - Raj

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: identify the user from server program

    Hi,

    I think you got the correct idea. You need a Map (e.g. a HashMap) to store key, value pairs. Key could be the username and value your HandleClient object. Then you can check if a username is inside the Hashmap and read the HandleClient (which you then could use to send a message).

    With kind regards,

    Konrad Neitzel

  3. #3
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    288
    Rep Power
    3

    Default Re: identify the user from server program

    Thanks for your response Mr.Kneitzel!

    I will try now itself..,

    Thank you for your Idea..,
    - Raj

Similar Threads

  1. Replies: 0
    Last Post: 10-05-2013, 06:51 AM
  2. user input program
    By myalani in forum New To Java
    Replies: 4
    Last Post: 10-28-2011, 05:07 AM
  3. Multi-user chat server and client
    By 435.mahesh in forum Java Software
    Replies: 6
    Last Post: 04-25-2009, 01:45 AM
  4. Replies: 1
    Last Post: 10-20-2008, 08:55 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 10:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •