i'm trying to write a chat server which can handle multiple clients, i've looked online and can't figure out how to name my threads, i thought:
int i = 0;
Thread t[i] = new Thread(inr);
t[i++].start();
would do it but this won't compile. Also realised i get a JVM bind error when the second client connects, if i don't pass the socket to the thread what do i pass?
You need to create 1 ServerSocket which then creates multiple Sockets.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
I don't know what you're talking about when you say "name my threads". Threads don't need names. Let me point you to Oracle's tutorial about client-server socket handling, that might help more.
Paul i start a new thread, t, which passes the sockets to class incomingreader. But when the loop goes round i can't start a new thread t as it's already defined. So how do i have t1, t2, t3 etc as different threads spawned from main?
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
so how would i go about creating multiple threads whenever i get a Server.accept? i don't want to change the name of a variable, merely to give each thread a different name. I've seen other code, such as below from simlple chat server /client use:
name[integer counter] as the name, but this won't work for me. No idea why not.
When i implement the same code again in the same loop (as in the earlier post) multiple clients can connect without problem. I was thinking it was because t was using the same name but now i'm not sure.