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;








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










share|improve this question
























  • 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












  • 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 > 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


















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










share|improve this question
























  • 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












  • 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 > 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














0












0








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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












  • 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 > 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


















  • 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












  • 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 > 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

















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













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
);



);













draft saved

draft discarded


















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.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?