请问在发送方发过来的数据长度大于,我缓冲区一次能读取的数据量,是不是可以循环读取?
SOCKET socket;
char buffer[200];
int len=200;
.............
while(true)
{
while(recv(sokcet,buffer,len,0)!=0)
{
}
break;
}
SOCKET socket;
char buffer[200];
int len=200;
.............
while(true)
{
while(recv(sokcet,buffer,len,0)!=0)
{
}
break;
}
recv后面的参数可以设置接受的数据大小
你可以建立一个应用协议。建立报头,每次把数据加上报头发出去,接受端在进行解析,根据报头知道该次数据有多大?然后,根据数据包的文件片断序号将文件组合。
struct header
{
int nSize;//该次发送数据大小
int nFileSectionID//文件分割序号
}接收数据可以这样:
int nBytesSize = 0; // 数据报头保存该次数据发送尺寸
int dwBytes = 0;
int nBytesRecv = 0;
if( (dwBytes = Receive( &nBytesSize, sizeof(int) )) == SOCKET_ERROR ) {
Clear();
return;
} char* pData = new char[nBytesSize];
if( pData == NULL ) {
Clear();
return;
}
memcpy( pData, &nBytesSize, sizeof(int) );
char* lpBuf = pData + sizeof(int);
int nTempSize = nBytesSize - sizeof(int);
while( nBytesRecv < nTempSize )
{
if( (dwBytes = Receive( lpBuf + nBytesRecv, nTempSize - nBytesRecv ))
== SOCKET_ERROR )
{
if( GetLastError() != WSAEWOULDBLOCK ) {
delete [] pData;
Clear();
return;
}
}
else
{
TRACE( "Recv size = %d \n", dwBytes );
nBytesRecv += dwBytes;
}
}
to :ww09124(wangwei) 那就读到错误为止了,不过这样很不好。呵呵,
我的是 19062916