18,356
社区成员
发帖
与我相关
我的任务
分享
//发送数据
int SendHunk(SOCKET h,char *lpBuf,int nBufLen)
{
int nSend = -1 ;
int nSendAll = 0;
if( h != SOCKET_ERROR && lpBuf != NULL && nBufLen > 0 )
{
//循环发送,直到所有数据都发送完毕或出错就返回
do {
nSend = ::send(h, //已经连接的句柄
lpBuf+nSendAll, //跳过已经发送的数据
nBufLen-nSendAll, //计算剩余要发送的数据长度
0);
if( nSend > 0 )
nSendAll +=nSend;//累加已经成功发送的数据长度
else
break;
} while( nSendAll < nBufLen );
}
return nSendAll;
}
//接收数据
int RecvHunk(SOCKET h,char *lpBuf,int nBufLen,int nWillLen)
{
int nRecv = -1 ;
int nRecvAll = 0;
if( h != SOCKET_ERROR && lpBuf != NULL && nBufLen > 0 && nWillLen >0 && nBufLen >= nWillLen )
{
//循环接收,直到数据达到要接收的长度或出错就返回
do {
nRecv = ::recv(h, //已经连接的句柄
lpBuf+nRecvAll,//跳过已经接收的数据
nWillLen-nRecvAll,//计算剩余要接收的数据长度
0);
if( nRecv >0 )
nRecvAll = nRecv;//当前已经接收的长度
else
break;
} while( nRecvAll < nWillLen );
}
return nRecvAll;
}