Archived Forum Post

Index of archived forum posts

Question:

I want to receive a notification (event) when data arrives on my socket?

Dec 23 '16 at 10:09

I am using ActiveX Socket with FoxPro and do not trigger events when receiving data in the buffer on an active connection.


Answer

This answer is general and applies to any programming language on any operating system.

I get this question from time to time, and the short answer is that your application needs to either poll the socket for incoming data, or it must wait on the socket for data to arrive. Of course, if you have the ability to start your own background thread, you could read the socket in the background and notify your foreground thread (by some application-defined means) when data is available.

You could also make a call to one of the asynchronous socket reading/waiting methods, such as ReceiveBytesAsync, SelectForReadingAsync, etc., in which case the background task would wait on the socket. However, the TaskCompleted callback is only possible in some programming languages (such as C++). For the ActiveX, it's generally not possible because the TaskCompleted callback would be in the background thread, and it would have to cross a thread boundary to get to the application code running in the foreground thread.

I sense that many people have this notion that somehow, when data arrives on a socket, that the operating system can magically notify a program, and that some "event" can fire. This isn't the case. Not with a high-level programming language. Maybe it's possible with code closer to the "metal" such as C/C++, but even that's not usually a good idea. (Google "SIGIO handler" to see what I mean..)


Answer

Ah the beauty of the UNIX OS!