Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 965649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:02:01+00:00 2026-05-16T02:02:01+00:00

I am having an issue in my recv() loop for winsock. I am trying

  • 0

I am having an issue in my recv() loop for winsock. I am trying to terminate the loop when iResult==0, however, the loop only ends when the socket closes. It appears to be hanging at the very last recv() where iResult would equal 0. So any ideas on how to terminate the loop effectively? My ultimate goal (whether iResult == 0 or not; perhaps I am going about this the wrong way) is to stop the loop when all the sent information has been read. Here is the loop.

    do
    {
        iResult = recv(socket, recvbuf, BUFLEN-1, 0);
        if(iResult > 0){
            // Null byte :)
            // Remove that garbage >:(
            recvbuf[iResult] = '\0';
            printf("Recvbuf: %s\n\n\niResult: %d\n",recvbuf,iResult);
            continue; // working properly
        }
        else if(iResult == 0)
            // Connection closed properly
            break;
        else
        {
            printf("ERROR! %ld",WSAGetLastError());
            break;
        }
    } while(iResult > 0);

Like I said, I am receiving all the data, I just cannot exit the loop. The next step would to be write data back to the server, but it hangs here until ping timeout. Socket is SOCK_STREAM and BUFLEN is defined as 0x200

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:02:01+00:00Added an answer on May 16, 2026 at 2:02 am

    By default, instead of returning 0, recv blocks if there’s no data to receive :

    If no incoming data is available at
    the socket, the recv call blocks and
    waits for data to arrive according to
    the blocking rules defined for WSARecv
    with the MSG_PARTIAL flag not set
    unless the socket is nonblocking. In
    this case, a value of SOCKET_ERROR is
    returned with the error code set to
    WSAEWOULDBLOCK. The select,
    WSAAsyncSelect, or WSAEventSelect
    functions can be used to determine
    when more data arrives.

    You can use ioctlsocket to put the socket in non-blocking mode:

    u_long iMode = 1;
    ioctlsocket(socket, FIONBIO, &iMode);
    

    EDIT: Here’s the setsockopt suggestion that I made in an earlier rev, then removed (see comments):

    You can use the setsockopt
    function with the SO_RCVTIMEO option
    to set the socket to timeout on recv
    if no data is available.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having an issue with random individuals trying to access an intranet site with a
The issue I'm having is issue with is I'm trying to get the paintComponent
hope you're having a good day. Another socket issue, another day :) I finally
I am trying to implement BST algorithm using Cormen's pseudo code yet having issue.
i'm trying to create an application using three20 but i'm having issue to set
I having issue that content assistant / intellisense is working in methods such as
i am having issue with loading my selector via a jquery ajax load call
Having an issue here that I have tried everything I can think of but
Having an issue with traversing in jQuery.. hopfully an easy one..but I'm banging my
having same issue, i am totally new in php. i install XAMPP then download

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.