prasadmokashi
Programmer
Hi,
I am new to socket programming and threads in java. I am trying to do the following.
When the client connects, socket accepts the connection and then hands it over to thread & goes back to listening requests....
created thread keeps on waiting...
Now at any point of time, I have number of such threads waiting ( depending upon no of connections ).
Now when I receive some text from some other program, I would like to pass that text to all the threads waiting and wake them up.
I have the following code to handle the connection.
Could anybody please tell me, on receiving the message how would I be able to call send function for each thread in the threadgroup ??? OR is there any other simple way of handling this ?
protected class HandleConnection extends Thread {
private String message;
// the output stream
PrintWriter out;
public HandleConnection(ThreadGroup thg,Socket theConnection) {
super(thg,theConnection.getInetAddress().toString());
try {
PrintWriter out = new PrintWriter(theConnection.getOutputStream(), true);
} catch (IOException ioe) {
System.err.println("Couldn't get io stream on new socket connection");
}
}
public void send(String msg){
this.message = msg;
this.notify();
}
public void run() {
try {
while(true) {
synchronized (this) {
wait();
//send message
out.println(this.message);
}
}
} catch (Exception e) {}
}
}
I am new to socket programming and threads in java. I am trying to do the following.
When the client connects, socket accepts the connection and then hands it over to thread & goes back to listening requests....
created thread keeps on waiting...
Now at any point of time, I have number of such threads waiting ( depending upon no of connections ).
Now when I receive some text from some other program, I would like to pass that text to all the threads waiting and wake them up.
I have the following code to handle the connection.
Could anybody please tell me, on receiving the message how would I be able to call send function for each thread in the threadgroup ??? OR is there any other simple way of handling this ?
protected class HandleConnection extends Thread {
private String message;
// the output stream
PrintWriter out;
public HandleConnection(ThreadGroup thg,Socket theConnection) {
super(thg,theConnection.getInetAddress().toString());
try {
PrintWriter out = new PrintWriter(theConnection.getOutputStream(), true);
} catch (IOException ioe) {
System.err.println("Couldn't get io stream on new socket connection");
}
}
public void send(String msg){
this.message = msg;
this.notify();
}
public void run() {
try {
while(true) {
synchronized (this) {
wait();
//send message
out.println(this.message);
}
}
} catch (Exception e) {}
}
}