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();
   
}
eire_irelandAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
++clientsConnected;
if (clientsConnected > 3)
    break;
CEHJCommented:
DOH!

if (clientsConnected > 2)

and that goes immediately after start of while
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();
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

jimmackCommented:
Don't forget to decrement clientsConnected when one of the clients closes their connection ;-)
CEHJCommented:
Unless it's two clients *ever* ;-)
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 ;-)
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?
CEHJCommented:
You could only do that *after* you allowed it to connect
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.
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)?
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?
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eire_irelandAuthor Commented:
Ok, ill give that a go, cheers
CEHJCommented:
8-)
CEHJCommented:
Think polymorphic - the only difference between ClientThread and ServerBusyThread ought to be in the run method theoretically ;-)
eire_irelandAuthor Commented:
ya, i see that now... its the exact same thread really, just different output
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.