This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Multiple client chat server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multiple client chat server" Watch "Multiple client chat server" New topic
Author

Multiple client chat server

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
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: 18541
    
    8

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: 99
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: 99
When i do the following multiple clients can connect and work.:


When i do:


the second client crashes with the error message:
java.net.SocketException: Connection reset
.

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: 11152
    
  16

you cannot have dynamic variable names in java.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
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: 18541
    
    8

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: 99
When i use:
i get
java.net.SocketException: Connection reset
.

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: 99
Problem solved, ServerSocket was in the while loop, not outside of it
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Multiple client chat server
 
Similar Threads
Client disconnected from server after about 10 minutes of inactivity
Threaded Application won't run.
I need help improving this chat server and chat client!
Java networking, nothing happens
How do you formally disconect a client from a server?