我的服务器需要向客户端发送两个大小为 500kb 的图像。客户端接收到第一张图片没有丢失任何数据,但是当服务器将另一张图片发送给客户端时,recv()在服务器发送数据之前返回0,然后服务器send()返回-1 .
这是我的 send() 和 recv() 代码:
服务器:
fp2 = fopen(totaldir2,"rb");
if(fp1==NULL)
{
cout<<"ERROR while openning: "<<totaldir2<<endl;
getchar();
exit(1);
}
fseek (fp2, 0 , SEEK_END);
IFileSize2 = ftell (fp2);
rewind (fp2);
picture2 = new char[sizeof(char)*IFileSize2+1];
if (picture2 == NULL) {exit (2);}
itoa (IFileSize2,CFileSize2,10);
countData = 0;
do{
sentData = send(sConnect,CFileSize2,strlen(CFileSize2)+1,NULL);
countData+=sentData;
}while(countData<strlen(CFileSize2)+1);
read = fread_s(picture2, IFileSize2, sizeof(char),IFileSize2, fp2);
countData = 0;
do{
sentData = send(sConnect, picture2, read, 0);
countData += sentData ;
}while(countData<read);
此代码运行两次以发送两个图像。
客户:
recv(sConnect, len2, sizeof(len2) ,NULL); \\recv returns 0 here before server sends the data.
FileSize2 = atoi(len2);
picture2 = new char[sizeof(char)*FileSize2+1];
fp2 = fopen("temp\\image2.bin","wb");
if(fp2==NULL)
{
closesocket(sConnect);
WSACleanup();
exit(1);
}
recv(sConnect, picture2, FileSize2, 0); \\same here
fwrite(picture2, sizeof(char), Received2, fp2);
此代码运行两次以接收两个图像。
当我在服务器中的 send() 之后放置 Sleep() 时,客户端会收到所有数据和所有图像,但 Sleep 取决于客户端网络。并且服务器获得了很多客户端。该连接与 TCP 一起使用。