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

  • SEARCH
  • Home
  • 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 7597295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:07:25+00:00 2026-05-30T22:07:25+00:00

I was reading this winsock example . I am trying to conceptualize how you

  • 0

I was reading this winsock example.

I am trying to conceptualize how you would create a C++ client program that has a persistent TCP/IP connection to a C# .NET server.

The problem I see is that in order for the C++ client to leave the receive loop, the server must close its socket connection to the client.

In my case the server will send to the client every couple seconds. I need to be able to receive one packet from the server and restart the main program loop so the client can perform the rest of its functionality.

If this receive code is in the C++ client’s main loop, the client will never stop receiving if the server never closes the connection to the client:

// Receive until the peer closes the connection

do {

    iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
    if ( iResult > 0 )
        printf("Bytes received: %d\n", iResult);
    else if ( iResult == 0 )
        printf("Connection closed\n");
    else
        printf("recv failed with error: %d\n", WSAGetLastError());

} while( iResult > 0 );
  • 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-30T22:07:27+00:00Added an answer on May 30, 2026 at 10:07 pm

    The example program that you have chosen to work from is designed to send a single request and receive a single response. It uses the state of the connection to indicate the end of the request, and the end of the response.

    You might want to work from a different example program. Search for “winsock chat example” on google.

    On the other hand, to modify this program as you have asked, you could replace the do-while loop with this:

    // Assume that the OP's protocol sends 100-byte packets. 
    // Each while iteration attempts to receive one such packet.
    // The while exits when the other side closes its connection.
    while(1) {
        // Receive 100-byte packet 
        int receivedBytes;
        for(receivedBytes = 0; receivedBytes < 100; ) {
            int iResult = recv(ConnectSocket,
                               recvbuf+receivedBytes,
                               recvbuflen-receivedBytes, 0);
    
            if ( iResult > 0 ) {
                receivedBytes += iResult;
                printf("Bytes received: %d\n", iResult);
            } else if ( iResult == 0 ) {
                printf("Connection closed\n");
                break;
            } else {
                printf("recv failed with error: %d\n", WSAGetLastError());
                break;
            }
        }
        if(receivedBytes == 100) {
            // ... process packet here
        } else {
            break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this tidbit , it would seem that the current user's permission would
Reading this post has left me wondering; are nightly builds ever better for a
After reading this article on thedailywtf.com, I'm not sure that I really got the
After reading this question I attempted to clean out my workspace and found that
After reading this question , I've learned that denormalization is not a solution for
Reading this brief example of using memcached with PHP, I was wondering how memcached
After reading this post I was wondering what would be the best way to
While reading this article, I got a doubt. I understood that while trasferring small
While reading this SO post - Is there a version of JavaScript's String.indexOf() that
After reading this , it is my understanding that declaring a method as const

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.