gpt4 book ai didi

c - udp socket编程文件传输

转载 作者:太空宇宙 更新时间:2023-11-03 23:40:41 25 4
gpt4 key购买 nike

服务器端代码.........

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<sys/socket.h>

#define BUFLEN 503
#define PORT 8885

void die(char *s)
{
perror(s);
exit(1);
}

int main(void)
{
struct sockaddr_in si_me, si_other;

int s, i,j, slen = sizeof(si_other) , recv_len;
char buf[BUFLEN];

if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die("socket");
}

memset((char *) &si_me, '1', sizeof(si_me));
//printf("%d",si_me);

si_me.sin_family = AF_INET;
si_me.sin_port = PORT;
si_me.sin_addr.s_addr = htonl(INADDR_ANY);

if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
{
die("bind");
}
//memset(buf,0,503);
char fname[20];
FILE *fp;
recv_len = recvfrom(s, buf, 20, 0, (struct sockaddr *) &si_other, &slen);

char fna[100];
memset(buf,0,503);

recv_len = recvfrom(s, buf, 20, 0, (struct sockaddr *) &si_other, &slen);

strcpy(fna,buf);
//printf("%c\n",fna);
int len= strlen(fna);
printf("%d",len);

unsigned long mm = atoi(buf);
//printf("mm value: %ld\n",mm);

fp=fopen(fna,"wb");
int itr=1;
memset(buf,0,503);
while(itr*503<mm)
{
if ((recv_len = recvfrom(s, buf, 503, 0, (struct sockaddr *)&si_other, &slen)) == -1)
{
die("recvfrom()");
}
fwrite(buf,503, 1, fp);
memset(buf,0,503);
//printf("Loop no: %d",i)
//for(i=0;i<=itr;i++);

itr++;
}

//printf("Loop no: %d\n",i);
printf("%d",(mm%503));
recv_len = recvfrom(s, buf, (mm%503), 0, (struct sockaddr *) &si_other, &slen);

fwrite(buf,(mm%503), 1, fp);
memset(buf,0,503);
fclose(fp);
close(s);

return 0;
}

客户端代码.........

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<sys/socket.h>

#define SERVER "127.0.0.1"
#define BUFLEN 503
#define PORT 8885

void die(char *s)
{
perror(s);
exit(1);
}
unsigned long fsize(char* file)
{
//String bbb=file;
FILE * f = fopen(file, "r");
fseek(f, 0, SEEK_END);
unsigned long len = (unsigned long)ftell(f);
printf("Total size: %d \n",len);
fclose(f);
return len;
}

int main(void)
{
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];
char message[BUFLEN];

if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die("socket");
}

memset((char *) &si_other, '1', sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = PORT;
//printf("Htons= %d \n",htons(PORT));

if (inet_aton(SERVER , &si_other.sin_addr) == 0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
//memset(message,0,503);
char fname[20];
printf("Enter Filename with extension: ");
scanf("%s",&fname);
sendto(s, fname, 20 , 0 , (struct sockaddr *) &si_other, slen);

memset(message,0,503);

unsigned long siz = fsize(fname);
printf("siz: %ld\n",siz);

char str[10];
sprintf(str, "%d", siz);
int send1=sendto(s, str, 20 , 0 , (struct sockaddr *) &si_other, slen);
printf("value of send1= %d",send1);

FILE *f;

f=fopen(fname,"rb");
memset(message,0,503);
fread(message, 503,1,f);

int itr =1;
while(itr*503<siz){

if (sendto(s, message, 503 , 0 , (struct sockaddr *) &si_other, slen)==-1)
{
die("sendto()");
}
memset(message,0,503);
fread(message, 503,1,f);
itr++;
}
fread(message, (siz % 503),1,f);
int send2=sendto(s, message, (siz % 503) , 0 , (struct sockaddr *) &si_other, slen);
printf("\n value of send 2= %d",send2);

memset(message,0,503);
fclose(f);
close(s);

return 0;
}

这是 UDP 套接字编程中的图像、视频、文本文件等文件传输代码。(从客户端到服务器端文件夹)。

任何人都可以向我解释为什么双方都使用 WHILE 循环,它的目的是什么?此外,一个问题是使用此程序,文件从客户端正确发送到服务器,但在到达服务器端后文件名被更改。

unsigned long len = (unsigned long)ftell(f);
printf("Total size: %d \n",len);

经过测试,我发现服务器端更改的文件名被设置为客户端 len 变量中文件指针的当前位置。( len 的值 与服务器端的变量 mm 和客户端的变量 'siz' 相同。

例如:

如果我从服务器端发送“Wallpaper.png”,它将到达服务器端并且文件以名称“164101”保存。但图像显示正常。

为什么 memset 使用了这么多次??它的实际用途是什么??

最佳答案

您的客户端和服务器使用相同的文件传输协议(protocol),定义如下:

  • 前20个字节包含文件名
  • 20字节包含文件长度
  • 实际字节按 503 字节的 block 发送和接收
  • 在最后获取最终 block (小于 503 字节)

在服务器端,您弄乱了应该存储文件名 fname 的变量和应该存储文件长度 fna 的变量。您可能应该将后者重命名为 flen,如以下代码片段所示:

...
char fname[20]; // variable to store file name
FILE *fp;
recv_len = recvfrom(s, buf, 20, 0, (struct sockaddr *) &si_other, &slen);
strcpy(fname, buf); // got 20 bytes in buf, copy to fname : this is the file name
printf("File name : %s\n", fname);

char flen[20];
memset(buf,0,503); // reset NULL bytes to buf

recv_len = recvfrom(s, buf, 20, 0, (struct sockaddr *) &si_other, &slen);
strcpy(flen, buf); // got 20 bytes, copy to flen : this is the file length (as a string)
printf("Length as string : %s\n", flen);
printf("Length as integer : %d\n", atoi(flen));

unsigned long mm = atoi(buf); // convert file length as a string to an integer variable
printf("mm value: %ld\n",mm);

fp=fopen(fname,"wb"); // use the proper variable as the file name
int itr=1;
...

希望这对您有所帮助!

关于c - udp socket编程文件传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47031592/

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