Precautions for while infinite loop in socket programming

1. Sleep must be added to the while infinite loop

2. The client, if the connect fails, close the socket and reinitialize sd = 1

if(connect() == -1)
{  
    shutdown(sd,2);
    delay(100);
    close(sd);
    sd =-1;
}

3. The assignment of the ip address in the client, use

inet_addr(ip); That is, ip is dotted decimal.

4、

while(1)
{


while(1)
{
    int ret = func();
    if(ret<=0)
        break; 
    if(ret == 1)
        break;   
    else
        continue;
        
}
     if(ret<=0)
        continue;

    //do sth.

}

If in the while infinite loop, each step will exit the loop, then this while infinite loop is only used to control the program flow! ! ! !

Guess you like

Origin blog.csdn.net/modi000/article/details/123246280