udp socket编程文件传输

时间:2017-10-31 09:07:58

标签: c sockets ubuntu while-loop memset

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

#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和客户端的变量&#39; siz&#39;

例如:

如果我发送&#34; Wallpaper.png&#34;从服务器端它将到达服务器端,文件以名称&#34; 164101&#34;保存。但图像显示正确。

为什么memset使用了很多次?什么是实际使用?

2 个答案:

答案 0 :(得分:1)

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

  • 前20个字节包含文件名
  • 20个字节包含文件长度
  • 实际字节由503字节的块发送和接收
  • 最后获取
  • 最终块(低于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;
...

希望这有帮助!

答案 1 :(得分:0)

while循环用于将文件拆分为较小的块,这些块适合UDP数据报,这些数据集的大小有限。与TCP不同,UDP不处理这些细节,因此您必须自己实现所有这些。

最终,您还必须实施超时和重新传输,重复数据报检测,以及可能的某种形式的拥塞控制。编写一个好的文件传输协议,在UDP之上打败基于TCP的解决方案是非常困难的。