0

assume we have an application which uses winsock to implement tcp communication. for each socket we create a thread and block-receiving on it. when data arrives, we would like to notify other threads (listening threads).

i was wondering what is the best way to implement this:

  1. move away from this design and use a non-blocking socket, then the listening thread will have to iterate constantly and call a non-blocking receive, thus making it thread safe (no extra threads for the sockets)

  2. use asynchronous procedure calls to notify listening threads - which again will have to alert-wait for apc to queue for them.

  3. implement some thread safe message queue, where each socket thread will post messages to it, and the listener, again, will go over it every interval and pull data from it.

also, i read about WSAAsyncSelect, but i saw that this is used to send messages to a window. isnt there something similar for other threads? (well i guess apcs are...)

Thanks!

roybj
  • 278
  • 3
  • 14
  • [PostThreadMessage function](http://msdn.microsoft.com/en-us/library/windows/desktop/ms644946%28v=vs.85%29.aspx) – Arno Dec 04 '12 at 10:31

2 Answers2

0

Use I/O completion ports. See the CreateIoCompletionPort() and the GetQueuedCompletionStatus() functions of the Win32 API (under File Management functions). In this instance, the socket descriptors are used in place of file handles.

fthiella
  • 48,073
  • 15
  • 90
  • 106
Dave
  • 1
0

You'll always be better off abstracting the mechanics of socket API (listening, accepting, reading & writing) in a separate layer from the application logic. Have an object that captures the state of a connection, which is created during an incoming connection and you can maintain buffers in this object for the incoming and outgoing traffic. This will allow your network interface layer to be independent of the application code. This will also make the code cleaner by separating the application functionality from the underlying communication mechanism.

Blocking or non-blocking socket decision depends on the level of scalability that your applications needs to achieve. If your application needs to support hundreds of incoming connections, adopting a thread-per-socket approach is not going to be very wise. You'll be better off going for an Io ports based implementation, which will make your app immensely scaleable at added code complexity. However, if you only foresee a few 10s of connections at any point in time, you can go for an asynchronous sockets model using Win32 events or messages. Win32 events based approach doesn't scale very well beyond a certain limit as you would have to manage multiple threads if the number of concurrent sockets exceed 63 (as WaitForMultipleObjects can only support a max of 64 sockets). Windows message based mechanism doesn't have this limitation though. OHOH, Win32 event based approach does not require a GUI window to work.

Check out WSAEventSelect along with WSAAsyncSelect API documentation in MSDN.

You might want to take a look at boost::asio package as well. It provides a neat (though a little complex) C++ abstraction over sockets API.

Hari Mahadevan
  • 920
  • 7
  • 14