How to check if there are available data to read before to call recv in a blocking socket context on Windows PCsHow to prevent SIGPIPEs (or handle them properly)socket recv() doesn't receive dataRead data from a client socket in JavaWindows TCP socket recv delayblocking recv() that receives no data (TCP)TCP Socket Data Send/Recv Operationhow to make a blocking recv callWindows XP socket error with recv()How to deal with block recv() using selectIs it OK to loop over recv / read to read all data from socket
How can I reorder triggered abilities in Arena?
Why do banks “park” their money at the European Central Bank?
How do I make my image comply with the requirements of this photography competition?
Is there any way to keep a player from killing an NPC?
I don't have the theoretical background in my PhD topic. I can't justify getting the degree
If someone uses the Command spell and says "drop", what happens?
Changing JPEG to RAW to use on Lightroom?
Why does Windows store Wi-Fi passwords in a reversible format?
Understanding how the determinant of the multidimensional normal likelihood can overrule the prior probability
Evaluated vs. unevaluated Association
Where does learning new skills fit into Agile?
Why is there a difference between predicting on Validation set and Test set?
Where can/should I, as a high schooler, publish a paper regarding the derivation of a formula?
Is first Ubuntu user root?
How is linear momentum conserved in case of a freely falling body?
Ghidra: Prepend memory segment in assembly listing view
Rent contract say that pets are not allowed. Possible repercussions if bringing the pet anyway?
Foreign language movie, people enter a church but find they can't leave
Can an ISO file damage—or infect—the machine it's being burned on?
Need to number each equation in an optimization problem
moon structure ownership
Does ostensible/specious make sense in this sentence?
Could George I (of Great Britain) speak English?
If an earthquake can destroy buildings why it cant kill us according to physics?
How to check if there are available data to read before to call recv in a blocking socket context on Windows PCs
How to prevent SIGPIPEs (or handle them properly)socket recv() doesn't receive dataRead data from a client socket in JavaWindows TCP socket recv delayblocking recv() that receives no data (TCP)TCP Socket Data Send/Recv Operationhow to make a blocking recv callWindows XP socket error with recv()How to deal with block recv() using selectIs it OK to loop over recv / read to read all data from socket
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I wrote a short TCP client application to test a SW broker using blocking sockets. It works well and I am happy with it. Now I need to modify it and I have a problem reading not solecited messages (messages coming from the server not initiated by the client) because recv() blocks the program flow until there is no data to read.
Good solution for me may be to find a way to check if there are available data to read before calling recv() or that recv() may exit if there are no data to read within a certain period.
So I tried with this function:
static bool isDataAvailable(int socket)
fd_set sready;
struct timeval nowait;
FD_ZERO(&sready);
FD_SET((unsigned int) socket, &sready);
memset((char *) &nowait, 0, sizeof(nowait));
bool res = select(socket + 1, &sready, NULL, NULL, &nowait);
if(FD_ISSET(socket, &sready))
res = true;
else
res = false;
return res;
//------------------------------------------------------------------------
but the result is that after the call all my application works as I selected to work using non blocking sockets. I normally use it on Linux and it works. I found some info around the web about select() and seems to me it would work also under Windows. But... something is going wrong.
Any help and suggestion will be appreciated.
Thanks,
Massimo
c windows sockets
add a comment |
I wrote a short TCP client application to test a SW broker using blocking sockets. It works well and I am happy with it. Now I need to modify it and I have a problem reading not solecited messages (messages coming from the server not initiated by the client) because recv() blocks the program flow until there is no data to read.
Good solution for me may be to find a way to check if there are available data to read before calling recv() or that recv() may exit if there are no data to read within a certain period.
So I tried with this function:
static bool isDataAvailable(int socket)
fd_set sready;
struct timeval nowait;
FD_ZERO(&sready);
FD_SET((unsigned int) socket, &sready);
memset((char *) &nowait, 0, sizeof(nowait));
bool res = select(socket + 1, &sready, NULL, NULL, &nowait);
if(FD_ISSET(socket, &sready))
res = true;
else
res = false;
return res;
//------------------------------------------------------------------------
but the result is that after the call all my application works as I selected to work using non blocking sockets. I normally use it on Linux and it works. I found some info around the web about select() and seems to me it would work also under Windows. But... something is going wrong.
Any help and suggestion will be appreciated.
Thanks,
Massimo
c windows sockets
How do you use this function? What behavior did you expect? If you only callrecvafter this function returnstrue, then of course your program will (in a way) behave like the socket was non-blocking, you don't call therecvfunction which does the blocking.
– Some programmer dude
Mar 27 at 19:04
Also, don't forget to check for errors fromselect.
– Some programmer dude
Mar 27 at 19:04
Well... I would expect select returns a positive value if there are available data to read calling recv(). If selects returns a negative value may be a select error or the socket is closed. So, I started as simple as possible to see it working or not just in case of no data available.
– Massimo Manca
Mar 27 at 19:37
Yes, sure, I can check better select errors at least now to understand how to implement my function.
– Massimo Manca
Mar 27 at 19:37
1
select()will return> 0if the socket is readable, 0 on timeout, and -1 on error. So, since you are only checking 1 socket, you can do this instead:return select(...) > 0;
– Remy Lebeau
Mar 27 at 20:11
add a comment |
I wrote a short TCP client application to test a SW broker using blocking sockets. It works well and I am happy with it. Now I need to modify it and I have a problem reading not solecited messages (messages coming from the server not initiated by the client) because recv() blocks the program flow until there is no data to read.
Good solution for me may be to find a way to check if there are available data to read before calling recv() or that recv() may exit if there are no data to read within a certain period.
So I tried with this function:
static bool isDataAvailable(int socket)
fd_set sready;
struct timeval nowait;
FD_ZERO(&sready);
FD_SET((unsigned int) socket, &sready);
memset((char *) &nowait, 0, sizeof(nowait));
bool res = select(socket + 1, &sready, NULL, NULL, &nowait);
if(FD_ISSET(socket, &sready))
res = true;
else
res = false;
return res;
//------------------------------------------------------------------------
but the result is that after the call all my application works as I selected to work using non blocking sockets. I normally use it on Linux and it works. I found some info around the web about select() and seems to me it would work also under Windows. But... something is going wrong.
Any help and suggestion will be appreciated.
Thanks,
Massimo
c windows sockets
I wrote a short TCP client application to test a SW broker using blocking sockets. It works well and I am happy with it. Now I need to modify it and I have a problem reading not solecited messages (messages coming from the server not initiated by the client) because recv() blocks the program flow until there is no data to read.
Good solution for me may be to find a way to check if there are available data to read before calling recv() or that recv() may exit if there are no data to read within a certain period.
So I tried with this function:
static bool isDataAvailable(int socket)
fd_set sready;
struct timeval nowait;
FD_ZERO(&sready);
FD_SET((unsigned int) socket, &sready);
memset((char *) &nowait, 0, sizeof(nowait));
bool res = select(socket + 1, &sready, NULL, NULL, &nowait);
if(FD_ISSET(socket, &sready))
res = true;
else
res = false;
return res;
//------------------------------------------------------------------------
but the result is that after the call all my application works as I selected to work using non blocking sockets. I normally use it on Linux and it works. I found some info around the web about select() and seems to me it would work also under Windows. But... something is going wrong.
Any help and suggestion will be appreciated.
Thanks,
Massimo
c windows sockets
c windows sockets
asked Mar 27 at 18:54
Massimo MancaMassimo Manca
265 bronze badges
265 bronze badges
How do you use this function? What behavior did you expect? If you only callrecvafter this function returnstrue, then of course your program will (in a way) behave like the socket was non-blocking, you don't call therecvfunction which does the blocking.
– Some programmer dude
Mar 27 at 19:04
Also, don't forget to check for errors fromselect.
– Some programmer dude
Mar 27 at 19:04
Well... I would expect select returns a positive value if there are available data to read calling recv(). If selects returns a negative value may be a select error or the socket is closed. So, I started as simple as possible to see it working or not just in case of no data available.
– Massimo Manca
Mar 27 at 19:37
Yes, sure, I can check better select errors at least now to understand how to implement my function.
– Massimo Manca
Mar 27 at 19:37
1
select()will return> 0if the socket is readable, 0 on timeout, and -1 on error. So, since you are only checking 1 socket, you can do this instead:return select(...) > 0;
– Remy Lebeau
Mar 27 at 20:11
add a comment |
How do you use this function? What behavior did you expect? If you only callrecvafter this function returnstrue, then of course your program will (in a way) behave like the socket was non-blocking, you don't call therecvfunction which does the blocking.
– Some programmer dude
Mar 27 at 19:04
Also, don't forget to check for errors fromselect.
– Some programmer dude
Mar 27 at 19:04
Well... I would expect select returns a positive value if there are available data to read calling recv(). If selects returns a negative value may be a select error or the socket is closed. So, I started as simple as possible to see it working or not just in case of no data available.
– Massimo Manca
Mar 27 at 19:37
Yes, sure, I can check better select errors at least now to understand how to implement my function.
– Massimo Manca
Mar 27 at 19:37
1
select()will return> 0if the socket is readable, 0 on timeout, and -1 on error. So, since you are only checking 1 socket, you can do this instead:return select(...) > 0;
– Remy Lebeau
Mar 27 at 20:11
How do you use this function? What behavior did you expect? If you only call
recv after this function returns true, then of course your program will (in a way) behave like the socket was non-blocking, you don't call the recv function which does the blocking.– Some programmer dude
Mar 27 at 19:04
How do you use this function? What behavior did you expect? If you only call
recv after this function returns true, then of course your program will (in a way) behave like the socket was non-blocking, you don't call the recv function which does the blocking.– Some programmer dude
Mar 27 at 19:04
Also, don't forget to check for errors from
select.– Some programmer dude
Mar 27 at 19:04
Also, don't forget to check for errors from
select.– Some programmer dude
Mar 27 at 19:04
Well... I would expect select returns a positive value if there are available data to read calling recv(). If selects returns a negative value may be a select error or the socket is closed. So, I started as simple as possible to see it working or not just in case of no data available.
– Massimo Manca
Mar 27 at 19:37
Well... I would expect select returns a positive value if there are available data to read calling recv(). If selects returns a negative value may be a select error or the socket is closed. So, I started as simple as possible to see it working or not just in case of no data available.
– Massimo Manca
Mar 27 at 19:37
Yes, sure, I can check better select errors at least now to understand how to implement my function.
– Massimo Manca
Mar 27 at 19:37
Yes, sure, I can check better select errors at least now to understand how to implement my function.
– Massimo Manca
Mar 27 at 19:37
1
1
select() will return > 0 if the socket is readable, 0 on timeout, and -1 on error. So, since you are only checking 1 socket, you can do this instead: return select(...) > 0;– Remy Lebeau
Mar 27 at 20:11
select() will return > 0 if the socket is readable, 0 on timeout, and -1 on error. So, since you are only checking 1 socket, you can do this instead: return select(...) > 0;– Remy Lebeau
Mar 27 at 20:11
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55384591%2fhow-to-check-if-there-are-available-data-to-read-before-to-call-recv-in-a-blocki%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55384591%2fhow-to-check-if-there-are-available-data-to-read-before-to-call-recv-in-a-blocki%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How do you use this function? What behavior did you expect? If you only call
recvafter this function returnstrue, then of course your program will (in a way) behave like the socket was non-blocking, you don't call therecvfunction which does the blocking.– Some programmer dude
Mar 27 at 19:04
Also, don't forget to check for errors from
select.– Some programmer dude
Mar 27 at 19:04
Well... I would expect select returns a positive value if there are available data to read calling recv(). If selects returns a negative value may be a select error or the socket is closed. So, I started as simple as possible to see it working or not just in case of no data available.
– Massimo Manca
Mar 27 at 19:37
Yes, sure, I can check better select errors at least now to understand how to implement my function.
– Massimo Manca
Mar 27 at 19:37
1
select()will return> 0if the socket is readable, 0 on timeout, and -1 on error. So, since you are only checking 1 socket, you can do this instead:return select(...) > 0;– Remy Lebeau
Mar 27 at 20:11