gpt4 book ai didi

c - 使用C语言进行Linux网络编程

转载 作者:行者123 更新时间:2023-11-30 20:06:18 30 4
gpt4 key购买 nike

我写了一个关于从服务器向客户端传输文件的程序。但问题是,如果文件大于2G,我只能传输2G文件,只有2G传输成功。

客户端

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <math.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAXLINE 1000
#define SA struct sockaddr
typedef struct
{
int oft;
int len;
char data[MAXLINE];
}Mem;
Mem Dat;
int main(int argc,char *argv[])
{
int sockfd = socket(AF_INET,SOCK_STREAM,0);
int complete;
struct sockaddr_in cli;
ssize_t recv_l,send_l;
size_t block_len;
socklen_t len;
char buf[MAXLINE];
char filename[MAXLINE];
FILE *fp;
bzero(&cli,sizeof(cli));
cli.sin_family = AF_INET;
if (argv[1] == NULL)
argv[1] = "172.16.42.22";

cli.sin_port = htons(5001);
inet_pton(AF_INET,argv[1],&cli.sin_addr.s_addr);
bind(sockfd,(struct sockaddr*)&cli,sizeof(cli));
if (connect(sockfd,(SA*)&cli,sizeof(cli)) == -1)
{
perror("connect");
close(sockfd);
exit(1);
}
//接收文件名列表
while((recv_l = recv(sockfd,buf,sizeof(buf),0)) > 0)
{
buf[recv_l] = '\0';
fputs(buf,stdout);
if(strcmp(buf+recv_l-5,"name\n") == 0)
break;
}
// exit(0);
bzero(filename,sizeof(filename));
while(fgets(filename,sizeof(filename),stdin) != NULL)
// while(gets(filename) != NULL)
{
int i = 0;
for(i = strlen(filename) - 1;i >= 0;i --)
if(filename[i] == '\n'){
filename[i] = '\0';
break;
}
send_l = send(sockfd,filename, strlen(filename),0);
recv_l = recv(sockfd,buf, sizeof(buf),0);
buf[recv_l] = '\0';
puts(buf);
if (strcmp(buf,"success") == 0)
break;
fputs(buf,stdout);
}
puts("filename send success");
//接收文件
if ((fp = fopen(filename,"wb")) == NULL)
{
perror("fopen");
exit(1);
}
while((recv_l = recv(sockfd,&Dat,sizeof(Dat),MSG_WAITALL)))
{
if (recv_l < 0){
perror("recv");
exit(1);
}
fwrite(Dat.data,sizeof(char),Dat.len,fp);
}
puts("file receive success");
fclose(fp);
close(sockfd);
return 0;
}

服务器

#include <sys/types.h>
#include <errno.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <math.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAXLINE 1000
#define SA struct sockaddr
typedef struct
{
int oft;
int len;
char data[MAXLINE];
}Mem;
Mem Dat;
long long sum = 0;
int sendall(int sockfd, void *buf, int *len)
{

int total = 0; // how many bytes we've sent
int bytesleft = *len; // how many we have left to send
int n;
while(total < *len) {

n = send(sockfd, buf+total, bytesleft, 0);
sum += n;
if(sum >= 1073741824){
puts("1G");
sum = 0;
}
if (n != bytesleft) return -1;
if (n == -1) {
break; }
total += n;
bytesleft -= n;
}

*len = total; // return number actually sent here

return n==-1?-1:0; // return -1 on failure, 0 on success
}
int main(int argc,char *argv[])
{
int sockfd = socket(AF_INET,SOCK_STREAM,0);
socklen_t len;
size_t block_len;
int complete;
ssize_t send_l,recv_l;
struct sockaddr_in serv,cli;
char buf[MAXLINE];
char filename[100];
FILE *fp;
bzero(&serv,sizeof(serv));
serv.sin_family = AF_INET;

if (argv[1] == NULL)
argv[1] = "5001";

serv.sin_port = htons(atoi(argv[1]));
serv.sin_addr.s_addr = htonl(0);

bind(sockfd,(SA*)&serv,sizeof(serv));
listen(sockfd,5);
// for( ; ; )
{
len = sizeof(cli);
complete = accept(sockfd,(SA*)&cli,&len);
//发送文件名列表
fp = popen("ls","r");
while( fgets(buf,MAXLINE,fp) != NULL)
{
send(complete,buf,strlen(buf),0);
}
strcpy(buf,"input file name\n");
send(complete,buf, strlen(buf),0);
puts("send list success");
pclose(fp);
//exit(0);
//获取文件名
while( (recv_l = recv(complete,filename,sizeof(filename),0) ) > 0)
{
filename[recv_l] = '\0';
if ((fp = fopen(filename,"rb")) == NULL)
{
strcpy(buf,"filename error please input again");
puts("filename error");
send(complete,buf,strlen(buf),0);
}else{
puts("get filename success");
break;
}
}
//文件名对了
strcpy(buf,"success");
send(complete,buf,strlen(buf),0);
puts("get correct filename");
//传文件
int all = 0;
long long sa = 0;
int len = sizeof(Dat);
while((block_len = fread(Dat.data,sizeof(char),MAXLINE,fp)) > 0)
{
Dat.len = block_len;

if (sendall(complete,&Dat,&len) < 0){
puts("send error");
exit(1);
}
// if (send(complete,&Dat ,sizeof(Dat),0) < 0)
// {
// perror("send");
// exit(1);
// }
}
printf("%lld\n",sum);
printf("%s Tranfer finished\n",filename);
fclose(fp);
close(complete);
}
return 0;
}

最佳答案

在 32 位系统上,默认最大文件大小为 2 GB。您可以使用开关 -D_FILE_OFFSET_BITS=64 编译程序以创建大于 2 GB 的文件或使用 64 位。

关于c - 使用C语言进行Linux网络编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25834755/

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