在vc中,通过CAsyncSocket派生,每次发送数据量较少,发送端和接收端send和receive的长度参数设置成相等。可以实现每次传送数据的完整性。

解决方案 »

  1.   

    发送端先告訴接收端有多少字节要发,然后就可以进入发送/接收的循环了。
    在这里每次发送/接收的大小都无所谓,windows并不一定是在你send()以后立刻发送,
    详细的见windsock2的文档。C语言实例:只写了发送端的,接收端类似
    send side:
    ....connected
    int length;//length of file
    char *buffer;//contain buffer of file ,size length
    unsigned int nSent=0;
    int nSend;
    int szSend;
    while(nSent<length){
    szSend=length-nSend>1024? 1024:length-nSend;
    nSend=send(sock,buffer,szSend,0);
    if(nSend<0){
    //error code here;
    }
    buffer+=nSend;
    nSent+=nSend;
    }