我正在使用一些C ++套接字示例。客户端运行并可以连接到服务器但无法发送文件。我认为send()
存在问题,但我无法修复它。编辑:错误消息是“通过对等方重置连接”
欢迎任何想法。
我使用OpenSuSE和QT 4.7.4
这是发送和接收功能
void str_server(int sock)
{
char buf[1025];
const char* filename = "//home//romanov//Documents//HelloWorld-build-desktop-Qt_4_7_4_in_PATH__System__Release//ss.png";
FILE *file = fopen(filename, "rb");
if (!file)
{
cerr<<"Can't open file for reading";
return;
}
while (!feof(file))
{
int rval = fread(buf, 1, sizeof(buf), file);//read value
if (rval < 1)
{
cerr<<"Can't read from file";
fclose(file);
return;
}
int off = 0;
do
{
int sent = send(sock, &buf[off], rval - off, 0);
if (sent < 1)
{
// if the socket is non-blocking, then check
// the socket error for WSAEWOULDBLOCK/EAGAIN
// (depending on platform) and if true then
// use select() to wait for a small period of
// time to see if the socket becomes writable
// again before failing the transfer...
cout<<"Can't write to socket";
fclose(file);
return;
}
off += sent;
}
while (off < rval);
}
fclose(file);
}
// ===================
void RecvFile(int winsock)
{
int rval;
char buf[1025];
FILE *file = fopen("//home//romanov//Documents//HelloWorld-build-desktop-Qt_4_7_4_in_PATH__System__Release//ss2.png", "wb");
if (!file)
{
printf("Can't open file for writing");
return;
}
do
{
rval = recv(winsock, buf, sizeof(buf), 0);
if (rval < 0)
{
// if the socket is non-blocking, then check
// the socket error for WSAEWOULDBLOCK/EAGAIN
// (depending on platform) and if true then
// use select() to wait for a small period of
// time to see if the socket becomes readable
// again before failing the transfer...
printf("Can't read from socket");
fclose(file);
return;
}
if (rval == 0)
break; //line 159
int off = 0;
do
{
int written = fwrite(&buf[off], 1, rval - off, file);
if (written < 1)
{
printf("Can't write to file");
fclose(file);
return;
}
off += written;
}
while (off < rval);
} //line 175
while (1);
fclose(file);
}
答案 0 :(得分:0)
嗯,问题是我的不好,recvFile()
{{1}},如果你们遇到同样的问题,你应该检查一下。