Packet loss in 1server&multipleClients model

Started by
2 comments, last by hplus0603 13 years, 5 months ago
Hi,

By using winsock I am building a networked application :

1) My each client has 1 thread to process `SendToServer` message queue and another thread to receive and push to a received messages queue.

So with n clients , I have 2n threads

2) My server has 1 connection-acceptor thread , 1 toSend message queue processor
and have multiple threads per client to receive from clients.

So with 1 server , I have n+2 threads

The problem is :


When I build a test application , when sender sends packages to clients , all of them can
receive them succesfully ( server -> clients is ok , no packet loss )

But when clients send messages to one server , some of them are randomly lost.
(clients -> server is not ok)

Is this normal ? If not what should I change , thread numbers or what could you suggest ?
(Thread delays , or in other words changing miliseconds of sleep calls in thread runs do not change anything )
Advertisement

While it may not be directly related to your question, you may want to consider the approach to your server, depending on the load you expect the server to handle.

More specifically and generally, a server should never allocate a separate thread for each client connection. In a high client connection scenario, you will find that your server will eventually reach a point where performance becomes a big issue due to thread-per-client approach.

What you should look into is using select() or IOCP if you're programming on Windows in order to minimize the number of server-side threads. In an IOCP example, a single server thread manages a large number of client connections simultaneously by utilizing a worker thread pool concept.

You can also look into boost::asio library too.

I'd suggest you look into this sooner rather than later as the approach one takes with their server and client connection support can affect how you inevitably code the remainder of your server.
BTW I fixed my code , in my test code , I didnt give enough time to establish connections between server and the clients , after adding some more code it is ok now.

Thanks for suggestions. For a thread pool implementation , I`d like to ask
if there is any suitable design pattern for implemetation ?
I suggest you re-write your server as a single-threaded application using select().

Also, if you're using TCP, you have to frame each packet with a length prefix (just as if you were reading data from a file).

More hints can be found in the Forum FAQ.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement