• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple client chat server

 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to create 1 ServerSocket which then creates multiple Sockets.
 
Paul Clapham
Sheriff
Pie
Posts: 20980
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like

 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you cannot have dynamic variable names in java.
 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20980
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved, ServerSocket was in the while loop, not outside of it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic