Discussion:
Winsock : Sample server with Multiple Thread for receiving and Sending
(too old to reply)
Ganapathi Hegde
2005-06-17 10:22:07 UTC
Permalink
Hi

In Multithreaded Winsock server,usually we have 1 thread for each client
after accepting the connection. This thread is responsible for receiving &
sending data to the client.For asynchronous handling, Is it possible to have
2 socket threads for each client say 1 thread for receiving messages & the
other thread for sending messages after accepting the client connection. The
receiving socket thread should not block the transmitting socket thread. It
should be possible that Rx & Tx executed independently without blocking each
other. I don't wont to use CAsyncSocket MFC class. I need to use Winsock
socket calls.
Is there any sample application????

Regards,
Ganapathi
Joseph M. Newcomer
2005-06-17 18:18:13 UTC
Permalink
I'd suggest this is not a good approach. You should consider handling a socket, both send
and receive, in a single thread. MFC and CAsyncSocket will not support splitting across
threads. If you need to do a long computation, you can spin off a computational thread to
do the work, and have it communicate the results back to the controlling thread. That's
what I've done.

Why do you need to use Winsock socket calls? (Note that these will not support
asynchronous socket I/O well across multiple threads, either; WSAAsyncSelect requires that
all events be routed to the same window, which means a single thread.

Perhaps you are approaching the problem from the wrong direction. I've certainly
implemented multithreaded servers where the sending and receiving is done completely
concurrently, neither blocking the other, using CAsyncSocket. It is actually pretty
trivial. Using raw Winsock is like programming in assembly code to solve a C++ problem.

I can't give out sample code because it is proprietary, but the philosophy is
straightforward. In my case, the sending was low-overhead, but the received message
required substantial processing, which I did in a separate thread. I queued up requests
for the service thread, which then did a PostThreadMessage to send the responses back
(which did not necessarily come back in FIFO order because I was using I/O completion
ports for handling the responses from the file system so the service thread was also fully
asynchronous). Today, I would probably have not created the separate service thread, but
that work was five years ago.
joe
Post by Ganapathi Hegde
Hi
In Multithreaded Winsock server,usually we have 1 thread for each client
after accepting the connection. This thread is responsible for receiving &
sending data to the client.For asynchronous handling, Is it possible to have
2 socket threads for each client say 1 thread for receiving messages & the
other thread for sending messages after accepting the client connection. The
receiving socket thread should not block the transmitting socket thread. It
should be possible that Rx & Tx executed independently without blocking each
other. I don't wont to use CAsyncSocket MFC class. I need to use Winsock
socket calls.
Is there any sample application????
Regards,
Ganapathi
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Loading...