I’m developing an embedded system that connects to a PC over ethernet using TCP sockets. The PC will be the TCP client, and the embedded system the server. If I understand it correctly, the classical communication model is that the client sends some data to the server, and the server responds every time. The server never initiates communication.
What I’d like to do is send commands from the PC to the embedded system, without corresponding responses. The embedded system will then execute the tasks described by the commands. If the embedded system has an error, or has some status message to report back, it will send these back, without being polled by the PC.
I’ve figured out everything but the receiving on the PC side. I’m programming the PC app in C#.net Can I set up a callback that fires whenever data has been received on the socket? Every example I’ve seen uses the receive method in a blocking fashion.
You can use asynchronous I/O on the socket stream. This is almost exactly what you’re asking for – you start a receive operation and give it a callback to call when the receive is complete.
Check out the BeginReceive/EndReceive functions.
More info here: http://msdn.microsoft.com/en-us/library/bbx2eya8.aspx