| Author |
Multiple client chat server
|
Rob Brew
Ranch Hand
Joined: Jun 23, 2011
Posts: 88
|
|
Hi all.
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?
Thanks in advance.
Rob.
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
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.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
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.
Writing the Server Side of a Socket
|
 |
Rob Brew
Ranch Hand
Joined: Jun 23, 2011
Posts: 88
|
|
Thanks for your replies.
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?
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
Something like
|
 |
Rob Brew
Ranch Hand
Joined: Jun 23, 2011
Posts: 88
|
|
When i do the following multiple clients can connect and work.:
When i do:
the second client crashes with the error message:
.
I need to use different names for Thread t, such as t1 t2 etc. How do i do this?
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9948
|
|
|
you cannot have dynamic variable names in java.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Rob Brew
Ranch Hand
Joined: Jun 23, 2011
Posts: 88
|
|
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.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
I don't know why you are getting hung up on "names" for threads. You create a thread like this:
Is there some feature of your application which needs to keep track of the threads you create?
|
 |
Rob Brew
Ranch Hand
Joined: Jun 23, 2011
Posts: 88
|
|
When i use:
i get
.
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.
|
 |
Rob Brew
Ranch Hand
Joined: Jun 23, 2011
Posts: 88
|
|
Problem solved, ServerSocket was in the while loop, not outside of it
|
 |
 |
|
|
subject: Multiple client chat server
|
|
|