• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 524
  • Last Modified:

limit client connections to two threads

How can I limit connections to a server to two seperate threads(i.e only two clients can connect to the server) Im using the code below to accept connections from a server.

while (true) {

    Socket s = serve.accept();
    Thread t = new ClientThread(s);
    t.start();
   
}
0
eire_ireland
Asked:
eire_ireland
  • 10
  • 4
  • 2
1 Solution
 
CEHJCommented:
++clientsConnected;
if (clientsConnected > 3)
    break;
0
 
CEHJCommented:
DOH!

if (clientsConnected > 2)

and that goes immediately after start of while
0
 
CEHJCommented:
I'm still not awake ;-)

>>and that goes immediately after start of while

no it doesn't - it goes after

Socket s = serve.accept();
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
jimmackCommented:
Don't forget to decrement clientsConnected when one of the clients closes their connection ;-)
0
 
CEHJCommented:
Unless it's two clients *ever* ;-)
0
 
jimmackCommented:
Yeah.  I thought of that as I clicked the Submit button ;-)

Since I'm commenting anyway...

>>  I'm still not awake ;-)

I'm guessing it's about 8am where you are (from comments you've made in other questions).  I've noticed a little confusion when you've referred to some of your comments, so I just thought I'd let you know that the timestamps are localisd (eg. I get your last comment here as:  Date: 12/05/2003 02:59PM GMT).  I'm sure you've realised that and I always just look at the minutes, but I just thought I'd mention it ;-)
0
 
eire_irelandAuthor Commented:
So if clientsConnected is two, is there a way I can send a message to the third, fourth, etc client trying to connect to the server that the server cant take any more connections?
0
 
CEHJCommented:
You could only do that *after* you allowed it to connect
0
 
CEHJCommented:
Jim, no - it's actually the middle ofthe afternoon - that 02:59PM GMT is right.

I didn't knwo about the time being localised. I'm sure i've only ever seen PST on my browser here.
0
 
CEHJCommented:
Of course, you could give other connecting threads a different program flow, and tell them they can't proceed.

I'm a little mystified by this though, as i thought you were doing 'rock paper scissors'. Can't any number of players play that game consecutively until some arbitrary cut-off point (no pun intended)?
0
 
eire_irelandAuthor Commented:
I just want two people(clients) to be able to play the game (its a two player game), If another client trys to connect I want message to be sent to that client informing that no connections can be made to the server at this time. How would I go about coding this?
0
 
CEHJCommented:
Conceptually much the same as above

>>if (clientsConnected > 2)

you could then create a new type of client thread

ServerBusyThread that connects, sends the busy message, then disconnects. When both clients have exited, clientsConnected can be reset to zero.
0
 
eire_irelandAuthor Commented:
Ok, ill give that a go, cheers
0
 
CEHJCommented:
8-)
0
 
CEHJCommented:
Think polymorphic - the only difference between ClientThread and ServerBusyThread ought to be in the run method theoretically ;-)
0
 
eire_irelandAuthor Commented:
ya, i see that now... its the exact same thread really, just different output
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 10
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now