stackofervlow
stackofervlow

Reputation: 125

handle client buffer in tcp server

Since i read a lot text and code about socket programming i decided to go like that:

TCP Server:

I want to be able to handle 800-1200 client connections at the same time. How do i handle client buffers? Every single example i read worked with just one solely buffer. Why dont people use something like:

typedef struct my_socket_tag {
    socket sock;
    char* buffer;
} client_data;

Now I am able to give the buffer away from a receiver-thread to a dispatch-request-thread and the receiving could go on on another socket while the first client specific buffer is processed.

Is that common practice? Am I missing the point?

Please give some hints, how to improve my question next time, thank you!

Upvotes: 4

Views: 832

Answers (1)

Roman Dmitrienko
Roman Dmitrienko

Reputation: 4205

The examples are usually oversimplified. Scalability is a serious issue, and I suggest it would be better to begin with simpler applications; handling a thousand of client connections is possible but in most applications it will require quite a careful development. Socket programming may get tricky.

There are different kinds of server applications; there is no single approach that would fit all tasks perfectly. There are lots of details to consider (is it a stream or datagram oriented service? are the connections, if any, persistent? does it involve lots of small data transfers, or few huge transfers, or lots of huge transfers? Et cetera, et cetera). This is why you are not likely to see any common examples in books.

If you choose threading approach, be careful not to create too many threads; one thread per client is usually (but not always) a bad choice. In some cases, you can even handle everything in a single thread (using async IO) without sacrificing any performance.

Having said that, I would recommend learning C++ and boost asio (or a similar framework). It takes care of many scalability-related problems, so there's no point in reinventing the wheel.

You may study the Architecture of Open Source Applications book (freely available). There are quite a few relevant examples that you may find useful.

Upvotes: 2

Related Questions