接收端
while ((bytes = recv(hClntSOCK, buf_fileindex, 1024, 0)) > 0)//读取文件内容,每次一个1024字节
{
if (buf_fileindex == "本次图片发送完成")
{
cout << buf_fileindex << endl;
break;
}
fwrite(buf_fileindex, 1, bytes, fp);//把buf里存的东西send给服务端
}
发送端
for (vector<string>::iterator iter = file.begin(); iter != file.end();iter++)
{
cout <<"this is filename size" << sizeof((*iter).c_str()) << endl;
/*int xiabiao = 0;
string temp[] = { 0 };
for (vector<string>::iterator iter = file.begin(); iter != file.end(); iter++)
{
for(i)
}*/
if (send(*s, (*iter).c_str(), (*iter).length() + 1, 0) == -1)
{
cout << "文件名发送失败" << endl;
break;
}
else
{
cout << "文件名发送成功" << endl;
}
FILE* fp = fopen((*iter).c_str(), "rb");//用文件名创造二进制文件
if (fp == NULL)
{
cout << "打开错误,请重新尝试" << endl;
return 0;
}
else
{
cout << "文件打开成功,等待传输" << endl;
}
char buf[1024] = { 0 };
int bytes;
start = timeGetTime();
while ((bytes = fread(buf, 1, 1024, fp)) > 0)//读取文件内容,每次一个1024字节
{
int n = send(*s, buf, 1024, 0);//把buf里存的东西send给服务端
if (n == -1)
{
cout << "传输失败" << endl;
break;
}
else
{
cout << "传输成功" << endl;
cout << "正在传输中···" << endl;
}
//Sleep(1000);
}