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
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
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
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..