Accessing a Thread by name?
Hello everyone,I have a question about thread programming. I have several threads running and the are all named. I'm wondering if there is a way from my main code to access a specific thread by name?for example, my main program wants to call a method in "ThreadB"?
I'm programming a chat type application and I need to be able to add a client to a host thread ...... here's an example
packetType = recvData.getInt();
if(packetType == JOIN_HOST)
{
// This is the name of the thread the client wants to
// connect to.
String hostName = recvData.getString();
// Here is where I need to access a specific thread based on the host
// the client wants to connect to.Any Ideas?
}
I know this question seems brief but if anyone can help I would be very thankful....
Thanks,
Mike
[1069 byte] By [
Tigerfanga] at [2007-10-3 3:58:43]

david:not sure if I am,let me explain a little more.I am writing a chat program and I have a couple different classes. I have a separate host program that a host user logs in with. The server will spawn a new thread for each host that is logged in. These host threads are what i am talking about. A client will log in and be shown a window with a list of the online host users. I have the client selecting a host user and sending the request to the server...... here is where my problem is. I need to, based on the name the client send, connect the client with the desired host. So, If I make a method in the host class is it accessable outside the thread?
If not, how can I add a client object to a running host thread?
Sorry if I'm not explaining this correctly.
Mike
David: So, what you're saying is that each host thread should bind it's own server socket, and then in order for a client to connect to it it will just send a join request to that socket?That does seem to make more sense then the way I'm doing things :)I don't know why but I never thought of doing that, lol.
> I have another question regarding this topic. Should
> I treat my host threads as client programs within the
> server? IE when a host thread is started create a
> socket and connect to the bound ServerSocket? or
> should I just open a socket and start sending data ?
Sorry I don't understand the question. I'm not a sockets expert and don't play with them much. You're reinventing a well worn wheel here - there must be oodles of references to open source chat servers out there that you could learn from.
You've got this back to front. There is no connecting to the server socket from inside the server, and no joining the socket either.
You will start one ServerSocket. Clients will then connect. You will have a server thread looping around ServerSocket.accept(). Every time you accept a socket you will start a new thread to handle it representing that client.
You need to have a good look at the Sockets tutorial for a start.
ejpa at 2007-7-14 21:57:17 >
