gpt4 book ai didi

c++ - 在网络中通过 Socket 进行文件传输时,此程序中的文件传输错误是什么?

转载 作者:行者123 更新时间:2023-11-28 02:57:24 25 4
gpt4 key购买 nike

我已经写了一个 C++ 演示示例。它将文件从服务器传输到客户端。当我在本地主机上运行这个程序时,它工作正常。但是,当我通过网络运行该程序时,文件传输不正确。接收到的图像尺寸大于发送的图像尺寸。另外,为什么同样适用于本地主机?我也尝试过更改端口号。

这是程序-

服务器

/** man 2 socket **/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{

char msg[] = "Shreyas..first socket prog";
int sock , sock_active;
struct sockaddr_in server, client;
int sent,ret;
unsigned int len;
char buffer[1024];


FILE *fp;

if ( (sock = socket(AF_INET,SOCK_STREAM,0)) == -1 )
//if ( (sock = socket(AF_INET,SOCK_DGRAM ,0)) == -1 )
{
perror("Sock:");

}

server.sin_family = AF_INET;
server.sin_port = htons(15000);
//server.sin_addr.s_addr = INADDR_ANY;
inet_aton("136.170.195.17", &(server.sin_addr));

bzero(&server.sin_zero, 8);

len = sizeof(struct sockaddr_in);

if( ret = (bind( sock, (struct sockaddr *)&server, len)) == -1 )
{
perror("bind :");
}

ret = listen(sock, 0);


while(1)
{

if( (sock_active = accept(sock, (struct sockaddr *)&client, &len)) == -1 )
{
perror("Problem in active socket:");

}


fp = fopen("./Tiger.JPG","rb");

if( fp == NULL )
{
cout<<"Error open file";
return -1;

}



memset(buffer, 1024,0);
int packets = 0;
int count;

//while ( fgets(buffer,1024,fp ) != NULL )

while( ! feof(fp) )
{
packets++;
cout<<"Client IP address is "<<inet_ntoa(client.sin_addr)<<endl;
cout<<"Client Port address is "<<ntohs(client.sin_port)<<endl;
/** Fread is reliable when using to find out the EOF , in feof(fp **/
count = fread(buffer,1,sizeof(buffer),fp);
/** fgets doesn't move the FP correctly */
//fgets(buffer,sizeof(buffer),fp);
cout<<"Read number of bytes ="<<count;
sent = send(sock_active, buffer, sizeof(buffer),0);
cout<<"The number of bytes sent ="<<sent<<"packet number = "<<packets<<endl;
memset(buffer, 1024,0);

}
cout<<"CLose current socket"<<endl;
close(sock_active);
fclose(fp);
}
cout<<"CLosing socket now" <<endl;

close(sock);

return 0;
}

客户端套接字程序-

/** man 2 socket **/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <iostream>
#include <unistd.h>


using namespace std;

int main(int argc, char *argv[])
{
struct sockaddr_in server;
int ret;
int sock;
int read_val = 1;
unsigned int len;

if ( (sock = socket(AF_INET,SOCK_STREAM,0)) == -1 )
{
perror("Sock:");

}

if( argc != 2 )

{

cout<<"Pass the Server IP "<<endl;
exit(EXIT_FAILURE);

}

server.sin_family = AF_INET;
server.sin_port = htons(15000);
inet_aton(argv[1], &(server.sin_addr));
bzero(&server.sin_zero, 8);
len = sizeof(server);


if ( (ret = connect(sock, (struct sockaddr *)&server,sizeof(server))) == -1)
{
perror("Connect failed:");
exit(-1);
}

char msg[1024];
memset(msg,0,1024);
FILE *fp_w;

fp_w = fopen("./try.JPG","wb");

while( read_val)
{
read_val = recv(sock,(char *)msg,sizeof(msg),0);
fwrite(msg,1,sizeof(msg),fp_w);
}
cout<<"Read is complete"<<endl;
fclose(fp_w);
close(sock);

return 0;

}

最佳答案

也许您应该在您的客户端中修改这段代码。

while( read_val)
{
read_val = recv(sock,(char *)msg,sizeof(msg),0);
fwrite(msg,1,sizeof(msg),fp_w);
}

recv 函数不会总是收到您想要接收的字节数。您必须使用 recv 的返回值来了解实际读取的字节数,并使用该计数写入文件。

我会把这个 block 写成,

while( read_val)
{
read_val = recv(sock,(char *)msg,sizeof(msg),0);
if ( read_val > 0)
{
fwrite(msg,1,read_val,fp_w); // I am using `read_val` while writing.
}
}

在使用文件/套接字 io API 时,假设它读/写了您要求的确切字节数是不好的。

关于c++ - 在网络中通过 Socket 进行文件传输时,此程序中的文件传输错误是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21672542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com