i've a ServerSocket that listen to connections on a particular address and port and for each connection(client) make a thread in order to implement a stateful protocol,now each thread(client) messages process in it's own thread,here is the problem:
1.How can i send data from one thread(client socket) to all(simply broadcasting a message.),i see some sort of BlockingQueues between two threads,here i've one to many threads for broadcasting and also have a one to one model for private messages.
2.I see a Synchronized block solution for share a data or resource between threads but here in my code i have no idea where should i implement this and why?
Entry Point to the server,Where the ServerSocket Is initialized and Listen:
public class ServerStarter {
//All Users that creates from threads adding here.
public static Hashtable<String,User> users = new Hashtable<String,User>();
//All Channels add here.
public static Hashtable<String,Channel> channels = new Hashtable<String,Channel>();
final BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
public static void main(String args[]){
Config config = new Config();
ServerSocket Server = null;
try {
//server configs,from left to right is: PORT,BackLog,Address
Server = new ServerSocket(config.port, config.backlog,config.ServerIP);
} catch (IOException e) {
System.err.println(e);
}
while (true) {
Socket sock = null;
BufferedReader inFromClient = null;
try {
sock = Server.accept();
} catch (IOException e) {
if (Server != null && !Server.isClosed()) {
try {
Server.close();
} catch (IOException e1)
{
e1.printStackTrace(System.err);
}
}
System.err.println(e);
}
try {
inFromClient = new BufferedReader(new InputStreamReader(sock.getInputStream()));
} catch (IOException e) {
System.err.println(e);
}
//each clients run on it's own thread!
new SocketThread(sock,inFromClient).start();
}
}
}
Where The Client Socket Threads Create is Here:
public class SocketThread extends Thread {
Socket csocket;
BufferedReader inFromClient;
public SocketThread(Socket csocket, BufferedReader inFromClient) {
this.csocket = csocket;
this.inFromClient = inFromClient;
}
public void run() {
try {
String fromclient = inFromClient.readLine();
//some primary informations sent to server for further processing.
RequestHandler Reqhandler = new RequestHandler(this.getId(), fromclient);
System.out.println("=======================================");
while (true) {
fromclient = inFromClient.readLine();
IRCParser parser = new IRCParser(fromclient);
//if the primary info's are OK and nothing causes to kill the thread the clients go to the infinite loop for processing messages.
Reqhandler.Commandhandler(parser.getCommand(), parser.getParameters());
}
} catch (IOException e) {
//kill current thread!
currentThread().interrupt();
return;
}
}
}
if the classes and other data is not sufficient,tell me to add more code and commenting,tnx