- 论坛徽章:
- 0
|
发送函数代码如下:
int Socket::Send(int nHandle,const char *pstrDataBuf,unsigned int &nLen,int nFlags /* = 0 */)
{
if ((nHandle < 0) || (NULL == pstrDataBuf))
{
return -1;
}
int nResult = 0;
int nRemainSize = (int)nLen;
int nSentSize = 0;
const char *pstrTmp = pstrDataBuf;
fd_set writefds;
time_t tStartTime = time(NULL);
int nTimeout = 3;
struct timeval tv = {0};
do
{
if (time(NULL) > tStartTime + nTimeout)
{
return -1;
}
FD_ZERO(&writefds);
FD_SET(nHandle, &writefds);
tv.tv_usec = 0;
tv.tv_sec = 1;
if ((nResult = select(nHandle + 1, NULL, &writefds,NULL,&tv)) == 0)
{
return -1;
}
else if ( nResult < 0 )
{
if ((errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == EINTR))
{
continue;
}
return errno;
}
nSentSize = send(nHandle,pstrTmp,nRemainSize,nFlags);
//发送失败
if (nSentSize < 0)
{
if ((errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == EINTR))
{
continue;
}
else
{
nLen -= nRemainSize;
return errno;
}
}
//发送成功
pstrTmp += nSentSize;
nRemainSize -= nSentSize;
}while(nRemainSize > 0);
//fprintf(stderr,"send size = %d",nSentSize);
return 0;
} |
|