Results 1 to 4 of 4
  1. #1
    banzuke is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default client can choose to send message to another client,

    good afternoon guys,.. i really appreciate if anyone can help. I want to make a multiclient server socket, where client can choose another client that he want to send message.. And every client have their own username that make it different to another client.
    example :
    client1 want to send message only to client 3,.. so client1 type "client3 : message to send"
    I hope anyone can teach me to make it possible,.. I'm newbie in java and i'm sorry if my english so bad.. here is code that i edit from google
    - CLIENT
    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class KnockKnockClient {
        public static void main(String[] args) throws IOException {
    
            Socket kkSocket = null;
            PrintWriter out = null;
            BufferedReader in = null;
    
            try {
                kkSocket = new Socket("localhost", 4444);
                out = new PrintWriter(kkSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host: taranis.");
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to: localhost.");
                System.exit(1);
            }
    
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            String fromServer;
            String fromUser;
            String username;
            
            System.out.println("Enter Username : ");
            username=stdIn.readLine();
            out.println(username);
            while ((fromServer = in.readLine()) != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Bye."))
                    break;
    		    
                fromUser = stdIn.readLine();
    	    if (fromUser != null) {
                    System.out.println("Client: " + fromUser);
                    out.println(fromUser);
    	    }
            }
    
            out.close();
            in.close();
            stdIn.close();
            kkSocket.close();
        }
    }
    - SERVER
    Java Code:
    import java.net.*;
    import java.io.*;
    import java.util.concurrent.Executor;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class KKMultiServer {
        public static void main(String[] args) throws IOException {
            ServerSocket serverSocket = null;
            boolean listening = true;
            ExecutorService exe=null;
            
    
            try {
                serverSocket = new ServerSocket(4444);
                exe = Executors.newCachedThreadPool();
            } catch (IOException e) {
                System.err.println("Could not listen on port: 4444.");
                System.exit(-1);
            }
    
            while (listening)
    	    exe.execute(
            new KKMultiServerThread(serverSocket.accept()));
            
    
            serverSocket.close();
        }
    }
    - Thread that handle new client
    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class KKMultiServerThread implements Runnable {
        private Socket socket = null;
    
        public KKMultiServerThread(Socket socket) {
    	
    	this.socket = socket;
        }
    
        public void run() {
    
    	try {
    	    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    	    BufferedReader in = new BufferedReader(
    				    new InputStreamReader(
    				    socket.getInputStream()));
    
    	    String inputLine, outputLine;
    	    
                inputLine=in.readLine();
    	    outputLine = "Welcome "+inputLine;
    	    out.println(outputLine);
    
    	    while ((inputLine = in.readLine()) != null) {
    		outputLine = inputLine;
    		out.println(outputLine);
    		if (outputLine.equals("Bye"))
    		    break;
    	    }
    	    out.close();
    	    in.close();
    	    socket.close();
    
    	} catch (IOException e) {
    	    e.printStackTrace();
    	}
        }
    }
    please help me how to make every thread can communicated so we can choose with thread to send message ... Thanks for reply..

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    client1 want to send message only to client 3
    Do you want the message to go directly from client1 to client3
    or do you want the server when it receives the message from client1 to only send it to client3?

  3. #3
    banzuke is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default

    i want to server send it directly to client3,..

    i already made it with this algoritm,.. when the client1 send message that contain localport of the destination client to server, then server send it to all client then in client side this message checked if the localport identical with him then the message show, if not then the message will not show up,...

    is that algorith good or bad??
    ty for replay,..

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    server send it to all client
    Could the server determine that only client3 is to get the message and then only send the message to client3 instead of sending it to all the clients?

Similar Threads

  1. Send AMF message from client
    By nguyenkimhuyit in forum Networking
    Replies: 0
    Last Post: 04-22-2011, 10:27 AM
  2. send message from a client to another client
    By esko in forum Networking
    Replies: 2
    Last Post: 02-04-2010, 08:21 PM
  3. Replies: 3
    Last Post: 07-29-2009, 05:52 AM
  4. send/read int in a client/server app
    By dim_ath in forum New To Java
    Replies: 2
    Last Post: 01-03-2008, 02:03 PM
  5. how to send .jar files client to server
    By gobinathm in forum Networking
    Replies: 1
    Last Post: 12-25-2007, 05:05 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
  •