Reputation: 225
I'm trying to communicate between two computers through the TCP/IP protocol on C++. I'm sending and receiving on the same socket. But there is a problem, I have no indicator that the second computer is trying to send me something.
Is there any flag or indicator that tells me that someone is sending something to me and I have to receive?
Upvotes: 1
Views: 1416
Reputation: 1766
Use the select() function to wait for an event on one or more sockets. With a zero timeout, you can also check if there is any data available.
Upvotes: 2
Reputation: 2686
That depends on the particular socket API. In the most common case, you just hang on receive (or with async socket APIs you park a callback) and that will unblock as soon as there is data. Some socket APIs have ways for you to tell whether there's data available to fetch, but the simplest way is simply to receive asynchronously and wait for the socket stack to raise the callback on you.
Upvotes: 0