<p>I have a client-server app where the client is on a Windows Mobile 6 device, written in C++ and the server is on full Windows and written in C#. </p>

<p>Originally, I only needed it to send messages from the client to the server, with the server only ever sending back an acknowledgement that it received the message.  Now, I would like to update it so that the server can actually send a message to the client to request data.  As I currently have it set up so the client is only in receive mode after it sends data to the server, this doesn't allow for the server to send a request at any time.  I would have to wait for client data.  My first thought would be to create another thread on the client with a separate open socket, listening for server requests...just like the server already has in respect the client.  Is there a way, within the same thread and using the same socket, to all the server to send requests at any time?</p>

<p>Can you use something to the effect of <code>WaitForMultipleObjects()</code> and pass it a receive buffer and an event that tells it there is data to be sent?</p>

[{'answer_id': 1275, 'author': 'sven', 'author_id': 46, 'author_profile': 'https://Stackoverflow.com/users/46', 'pm_score': 4, 'selected': True, 'text': "<p>When I needed to write an application with a client-server model where the clients could leave and enter whenever they want, (I assume that's also the case for your application as you use mobile devices) I made sure that the clients send an <em>online</em> message to the server, indicating they were connected and ready to do whatever they needed doing.</p>\n\n<p>at that time the server could send messages back to the client trough the same open connection.</p>\n\n<p>Also, but I don't know if that is applicable for you, I had some sort of <em>heartbeat</em> the clients sent to the server, letting it know it was still online. That way the server knows when a client was forcibly disconnected from the network and it could mark that client back as offline.</p>\n"}, {'answer_id': 1934, 'author': 'fastcall', 'author_id': 328, 'author_profile': 'https://Stackoverflow.com/users/328', 'pm_score': 2, 'selected': False, 'text': "<p>I'm not clear on whether or not you're wanting to add the asynchronous bits to the server in C# or the client in C++.</p>\n\n<p>If you're talking about doing this in C++, desktop Windows platforms can do socket I/O asynchronously through the API's that use overlapped I/O.  For sockets, WSASend, WSARecv both allow async I/O (read the documentation on their LPOVERLAPPED parameters, which you can populate with events that get set when the I/O completes).</p>\n\n<p>I don't know if Windows Mobile platforms support these functions, so you might have to do some additional digging.</p>\n"}, {'answer_id': 7650, 'author': 'roo', 'author_id': 716, 'author_profile': 'https://Stackoverflow.com/users/716', 'pm_score': 2, 'selected': False, 'text': '<p>Check out <a href="http://tenermerx.com/Asio/" rel="nofollow noreferrer">asio</a>. It is a cross compatable c++ library for asyncronous IO. I am not sure if this would be useful for the server ( I have never tried to link a standard c++ DLL to a c# project) but for the client it would be useful.</p>\n\n<p>We use it with our application, and it solved most of our IO concurrency problems.</p>\n'}, {'answer_id': 13639, 'author': 'Steve Gury', 'author_id': 1578, 'author_profile': 'https://Stackoverflow.com/users/1578', 'pm_score': 3, 'selected': False, 'text': '<p>Using asynchronous communication is totally possible in single thread! </p>\n\n<p>There is a common design pattern in network software development called the reactor pattern (<a href="http://www.amazon.fr/Pattern-Oriented-Software-Architecture-Concurrent-Networked/dp/0471606952/ref=sr_1_9?ie=UTF8&amp;s=english-books&amp;qid=1218980593&amp;sr=1-9" rel="noreferrer">look at this book</a>). Some well known network library provides an implementation of this pattern (<a href="http://www.cs.wustl.edu/~schmidt/ACE.html" rel="noreferrer">look at ACE</a>).</p>\n\n<p>Briefly, the reactor is an object, you register all your sockets inside, and you wait for something. If something happened (new data arrived, connection close...) the reactor will notify you. And of course, you can use only one socket to send and received data asynchronously.</p>\n'}]
['https://Stackoverflow.com/questions/1241', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/194/']