Solved

limit client connections to two threads

Posted on 2003-12-05
16
491 Views
Last Modified: 2010-04-16
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
Comment
Question by:eire_ireland
  • 10
  • 4
  • 2
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9882547
++clientsConnected;
if (clientsConnected > 3)
    break;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9882551
DOH!

if (clientsConnected > 2)

and that goes immediately after start of while
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9882564
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
 
LVL 15

Expert Comment

by:jimmack
ID: 9882570
Don't forget to decrement clientsConnected when one of the clients closes their connection ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9882589
Unless it's two clients *ever* ;-)
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9882636
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
 

Author Comment

by:eire_ireland
ID: 9882813
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9882829
You could only do that *after* you allowed it to connect
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9882835
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9882897
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
 

Author Comment

by:eire_ireland
ID: 9883641
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 9883682
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
 

Author Comment

by:eire_ireland
ID: 9883735
Ok, ill give that a go, cheers
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9883785
8-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9883799
Think polymorphic - the only difference between ClientThread and ServerBusyThread ought to be in the run method theoretically ;-)
0
 

Author Comment

by:eire_ireland
ID: 9884047
ya, i see that now... its the exact same thread really, just different output
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java passing arguments (type error) 2 48
GUI builder for Eclipse? 8 27
windows explorer path to command prompt 5 33
Oracle SQL syntax check  without executing 6 38
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now