• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Multiple client chat server

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to create 1 ServerSocket which then creates multiple Sockets.
 
Sheriff
Posts: 28321
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like

 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 28321
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem solved, ServerSocket was in the while loop, not outside of it
 
Do Re Mi Fa So La Tiny Ad
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic