views:

202

answers:

4

i'm reading about way to implemnt client-server in the most efficient manner, and i bumped into that link : http://msdn.microsoft.com/en-us/library/ms740550%28VS.85%29.aspx

saying :

"Concurrent connections should not exceed two, except in special purpose applications. Exceeding two concurrent connections results in wasted resources. A good rule is to have up to four short lived connections, or two persistent connections per destination "

i can't quite get what they mean by 2... and what do they mean by persistent?

let's say i have a server who listens to many clients , whom suppose to do some work with the server, how can i keep just 2 connections open ? what's the best way to implement it anyway ? i read a little about completion port , but couldn't find a good examples of code, or at least a decent explanation.

thanks

+5  A: 

Did you read the last sentence:

A good rule is to have up to four short lived connections, or two persistent connections per destination.

Hard to say from the article, but by destination I think they mean client. This isn't a very good article.

anon
That's how I read the article as well (which isn't so much of an article as some brief statements made without a lot of explanation). In general, a server doesn't dictate the lifetime of a connection to its clients, unless the server itself is going down. The clients dictate the lifetime. The server can dictate however that each client only gets a single connection.
Matt Jordan
+1  A: 

A persistent connection is where a client connects to the server and then performs all its actions without ever dropping the connection. Even if the client has periods of time when it does not need the server, it maintains its connection to the server ready for when it might need it again.

A short lived connection would be one where the client connects, performs its action and then disconnects. If it needs more help from the server it would re-connect to the server and perform another single action.

As the server implementing the listening end of the connection, you can set options in the listening TCP/IP socket to limit the number of connections that will be held at the socket level and decide how many of those connections you wish to accept - this would allow you to accept 2 persistent connections or 4 short lived connections as required.

Big GH
A: 

What they mean by, "persistent," is a connection that is opened, and then held open. It's pretty common problem to determine whether it's more expensive to tie up resources with an "always on" connection, or suffer the overhead of opening and closing a connection every time you need it.

It may be worth taking a step back, though.

If you have a server that has to listen for requests from a bunch of clients, you may have a perfect use case for a message-based architecture. If you use tightly-coupled connections like those made with TCP/IP, your clients and servers are going to have to know a lot about each other, and you're going to have to write a lot of low-level connection code.

Under a message-based architecture, your clients could place messages on a queue. The server could then monitor that queue. It could take messages off the queue, perform work, and place the responses back on the queue, where the clients could pick them up.

With such a design, the clients and servers wouldn't have to know anything about each other. As long as they could place properly-formed messages on the queue, and connect to the queue, they could be implemented in totally different languages, and run on different OS's.

Messaging-oriented-middleware like Apache ActiveMQ and Weblogic offer API's you could use from C++ to manage and use queues, and other messaging objects. ActiveMQ is open source, and Weblogic is sold by Oracle (who bought BEA). There are many other great messaging servers out there, so use these as examples, to get you started, if messaging sounds like it's worth exploring.

Stephen Harmon
...of course, if your use case is lower-level than I have assumed, the overhead of using messaging-oriented-middleware may be too high. It all depends upon the type of work the clients and servers are doing.
Stephen Harmon
Since the link is from MSDN, and you've tagged this with C++, you may have everything you need with MSMQ. I haven't used it, but it seems like it's in the right neighborhood.I had trouble with anchor tags in comments. Here's the MS official page on it:http://www.microsoft.com/windowsserver2003/technologies/msmq/default.mspx
Stephen Harmon
A: 

I think key words are "per destination". Single tcp connection tries to accelerate up to available bandwidth. So if you allow more connections to same destination, they have to share same bandwidth.

This means that each transfer will be slower than it could be and server has to allocate more resources for longer time - data structures for each connection.

Because establishing tcp connection is "time consuming", it makes sense to allow establish second connection in time when you are serving first one, so they are overlapping each other. for short connections setup time could be same as for serving the connection itself (see poor performance example), so more connections are needed for filling all bandwidth effectively.

(sorry I cannot post hyperlinks yet) here msdn.microsoft.com/en-us/library/ms738559%28VS.85%29.aspx you can see, what is poor performance.

here msdn.microsoft.com/en-us/magazine/cc300760.aspx is some example of threaded server what performs reasonably well.

you can limit number of open connections by limiting number of accept() calls. you can limit number of connections from same source just by canceling connection when you find out, that you allready have more then two connections from this location (just count them).

For example SMTP works in similar way. When there are too many connections, it returns 4xx code and closes your connection.

Also see this question: What is the best epoll/kqueue/select equvalient on Windows?

Petr Chloupek