save send

来源:互联网 发布:php微信扫码登录源码 编辑:程序博客网 时间:2024/05/16 18:51
 

int DataSend(SOCKET s, char *DataBuf, int DataLen)
{
    if (DataLen <= 0 || DataBuf == NULL)
    {
        printf("Sending data must not be null!\n");
        return 0;
    }

    int nBytesLeft = DataLen; 
    int nBytesSent = 0;
    int ret;
    //set socket to blocking mode 
    //int iMode = 0; 
    //ioctlsocket(s, FIONBIO, (u_long FAR*) &iMode);
    int iFlags;
    int iRtn;

    iFlags = fcntl(s, F_GETFL);
    iFlags &= ~O_NONBLOCK;
    fcntl(s, F_SETFL, iFlags);
    while(nBytesLeft > 0) 
    { 
        ret = send(s, DataBuf + nBytesSent, nBytesLeft, 0); 
        if(ret <= 0)
        {
            printf("Sending data failed!\n");
            break;
        }
        nBytesSent += ret;
        nBytesLeft -= ret;
    }
    return nBytesSent; 
}

原创粉丝点击