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 6112259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:43:29+00:00 2026-05-23T14:43:29+00:00

I’m working on a UDP based file-sharing program. Let me post some sample code

  • 0

I’m working on a UDP based file-sharing program. Let me post some sample code before explain the problem.

while (true) 
{
    Data toRecv;
    int bytesRead = recvfrom(s->_UPDsock, (char*)&toRecv, sizeof(toRecv), 0,(SOCKADDR*)&remoteAddress, &remoteAddresslength);
    if(bytesRead > 0)
    {
        string temp(toRecv.chunk,(bytesRead-sizeof(int)));
        if(!checker)
        {
            //total packet amount.
            totalChunkAmount = toRecv.ACK;
            checker = true;
        }
    }
}

As you can see at line “13” I’m initializing the totalChunkAmount by using the variable I got from UDP’s recvFrom function. I need to initialize that value only ONCE, that’s why I’m using it inside a bool if() check. and after the initialization, I’m flipping the bool value to true, so it will not be initialized again. Is there any other way to achieve the same result, but not using the ugly bool switching method.

  • 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-23T14:43:30+00:00Added an answer on May 23, 2026 at 2:43 pm

    Set totalChunkAmount to an invalid state before it’s initialized and check for that state. For example if totalChunkAmount is an int then,

    totalChunkAmount = -1; // say -1 is invalid value
    while(true)
    {
     ...
     if(-1 == totalChunkAmount)
       totalChunkAmount = toRecv.ACK; // ACK can never be -1
    }
    

    Likewise, if totalChunkAmount is a pointer then you may set it to NULL (0).

    [Edit Note: I am just wondering, in your while loop mainly you are initializing variable then why don’t your break out of the loop after that ? If you can do so it’s lot more cleaner and no such checks are needed.]

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I'm parsing an XML file, the creators of it stuck in a bunch social

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.