• 13
• 18
• 19
• 27
• 9

# Reacting to incoming TCP/IP messages in GUI applications

This topic is 4587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I intend to write my application in Python (using wxPython), but the question I'm about to ask is of a general nature. Assume you have GUI-based application, called program A. Program A is connected to some other program, program B, via TCP/IP. The other program, program B, may send messages to program A at any time. Program A must react to these messages as quickly as possible. The problem is in my program A, how, where and when do I check whether I have received data from program B? Do I just use timer that fires every xxx msecs and causes a function to be executed in which I do a select() and then recv()?

##### Share on other sites
No, you set a socket to listen to a port. When information exists on the socket, you will get an asynchronous message.

##### Share on other sites
None of the sockets I can think of work that way. I mean surely just because you create a socket doesn't mean your program will automatically be notified when data is available to be read from the socket. Normally you do this by either just doing a blocking recv(), which will suspend the calling thread until something is available, or you use select() to find out if there is data to be read from the socket. How would I go about creating one of those asynchronous sockets (bear in mind that I'm not using C++/MFC)?

##### Share on other sites
Asynchronous sockets work that way.

edit: whoops :p

##### Share on other sites
Umm ... okay, I don't think I understand what you are saying. I know in MFC there's a class CAsyncSocket that does what you say, but I think it only does that by using some kind of Windows message that gets posted to the GUI thread or whatever. I really have no idea how I'd create one of those async sockets myself if I can't use CAsyncSocket. I mean "you will get an asynchronous message" to me implies that some kind of callback function will get called in which I can then read the data from the socket and react accordingly. But how do I set up a plain vanilla socket to fire a callback function as soon as data arrives on the socket, and what's more, how do I get it to run in the GUI thread?

##### Share on other sites
Couldn't you create a thread that waits for the desired event to occur?

DWORD CALLBACK ThreadProc( LPVOID lpArg ){  WSAEVENT hEvent;  WSANETWORKEVENTS Events;  SOCKET pSocket = *(SOCKET*)(lpArg);  hEvent = WSACreateEvent();  WSAEventSelect(pSocket,hEvent,FD_READ | FD_WRITE);  while( TRUE )  {      WSAWaitForMultipleEvents(1,&hEvent,FALSE,WSA_INFINITE,FALSE);      WSAEnumNetworkEvents(pSocket,hEvent,&Events);      if(Events.lNetworkEvents & FD_READ)      {        // Respond to event      }  }  return 0;}

##### Share on other sites
Yup, could do that. But then I'd still react in a different thread than the GUI thread, which is not good since part of reacting to the incoming message is to pop up a dialog box, edit a text control, etc. and I can only do that from within the GUI thread. I mean I could probably set up a mechanism whereby the socket thread sets some kind of global flags when it has read a message. The GUI thread would then have to check these flags periodically and update the screen, etc. but I think this is rather clumsy and cumbersome. Is there no way to do it all directly in the GUI thread?

##### Share on other sites
You could setup a timer wich checks the state of the socket through the select call or something like that.
You can define a message like const WM_DATAONSOCKET = WM_APP + 100; and then when the socket thread detects that data is ready to read it will send that message to your messageque.

##### Share on other sites
Quote:
 Original post by BrocketinoYou could setup a timer wich checks the state of the socket through the select call or something like that.

Okay, that's what I was thinking in the first place. ;-) I thought there might be more elegant ways of achieving what I want, but I guess not. Thanks for helping, all! :)