I am working on a UDP based file sharing program. Let me post some sample code before explaining 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) {
As you can see on line "13", I initialize totalChunkAmount using a variable derived from UDP recvFrom . I need to initialize this value only once, so I use it inside the bool if() check. and after initialization, I translate the bool value to true , so it will not be initialized again. Is there any other way to achieve the same result, but not use the ugly bool switch method.
source share