如果将send的每次发送长度定在1000,多次调用send的时候发现会出现发送失败的情况,如果遇到这种情况是不是必须要关闭连接,如果进行一定的延迟后会不会能够继续发送?
例如将发送函数写成如下为什么会有时后出现不能全部成功发送完毕,发送一部分后send函数返回-1?int mComm::SendData(SOCKET ss,int route)
{
int tlen;
int sentnum,timecount;
int totalsent=0;
timecount=0;
tlen=10;
lSendLenOld[route]=0;
while(tlen>0)
{
JudgeEachConnection(ss,route); //判断该连接能不能发送 if (timecount>180)
{
#ifdef _DEBUG
int ti;
ti = WSAGetLastError();
CatchRcvErr(ti);
#endif
mCloseSocket(route);
return -1;
} if (CanSendFlag[route]) //如果能发送
{
CanSendFlag[route] = false;
tlen=lSendLen-lSendLenOld[route]; //lSendLen为要发送的总长度
if(tlen<=0)
{
return 0;
}
if (tlen>1000)
tlen=1000;
sentnum=send(ss,(char*)&SendMsg[lSendLenOld[route]],tlen,0);
if (sentnum==SOCKET_ERROR)
{
if ( WSAEWOULDBLOCK == WSAGetLastError() )
{
Sleep(100);
continue;
}
else
break;
}
lSendLenOld[route]+=sentnum;
}
else
{
timecount++;
Sleep(500);
}
}
return 0;
希望高人指点!!!!
}
例如将发送函数写成如下为什么会有时后出现不能全部成功发送完毕,发送一部分后send函数返回-1?int mComm::SendData(SOCKET ss,int route)
{
int tlen;
int sentnum,timecount;
int totalsent=0;
timecount=0;
tlen=10;
lSendLenOld[route]=0;
while(tlen>0)
{
JudgeEachConnection(ss,route); //判断该连接能不能发送 if (timecount>180)
{
#ifdef _DEBUG
int ti;
ti = WSAGetLastError();
CatchRcvErr(ti);
#endif
mCloseSocket(route);
return -1;
} if (CanSendFlag[route]) //如果能发送
{
CanSendFlag[route] = false;
tlen=lSendLen-lSendLenOld[route]; //lSendLen为要发送的总长度
if(tlen<=0)
{
return 0;
}
if (tlen>1000)
tlen=1000;
sentnum=send(ss,(char*)&SendMsg[lSendLenOld[route]],tlen,0);
if (sentnum==SOCKET_ERROR)
{
if ( WSAEWOULDBLOCK == WSAGetLastError() )
{
Sleep(100);
continue;
}
else
break;
}
lSendLenOld[route]+=sentnum;
}
else
{
timecount++;
Sleep(500);
}
}
return 0;
希望高人指点!!!!
}
{
int ti;
timeval mWaitTime;
fd_set fdRead,fdWrite; mWaitTime.tv_sec = 0;
mWaitTime.tv_usec = 0;
FD_ZERO(&fdRead);
FD_ZERO(&fdWrite); FD_SET(ts,&fdRead);
FD_SET(ts,&fdWrite); ti = select(NULL,&fdRead,&fdWrite,NULL,&mWaitTime);
if (FD_ISSET(ts,&fdRead))
CanRcvFlag[route] = true;
if (FD_ISSET(ts,&fdWrite))
CanSendFlag[route] = true;
else
CanSendFlag[route] = false; FD_ZERO(&fdRead);
FD_ZERO(&fdWrite); }