Results 1 to 4 of 4
- 08-24-2011, 07:29 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
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(); } }
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(); } }
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(); } } }
- 08-24-2011, 02:54 PM #2client1 want to send message only to client 3
or do you want the server when it receives the message from client1 to only send it to client3?
- 08-25-2011, 07:56 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
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,..
- 08-25-2011, 03:37 PM #4
Similar Threads
-
Send AMF message from client
By nguyenkimhuyit in forum NetworkingReplies: 0Last Post: 04-22-2011, 10:27 AM -
send message from a client to another client
By esko in forum NetworkingReplies: 2Last Post: 02-04-2010, 08:21 PM -
how to send a file from server to client and client saves the file?
By KoolCancer in forum New To JavaReplies: 3Last Post: 07-29-2009, 05:52 AM -
send/read int in a client/server app
By dim_ath in forum New To JavaReplies: 2Last Post: 01-03-2008, 02:03 PM -
how to send .jar files client to server
By gobinathm in forum NetworkingReplies: 1Last Post: 12-25-2007, 05:05 AM
Bookmarks