udp socket编程文件传输 [英] udp socket programming file transfer

查看:34
本文介绍了udp socket编程文件传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Server Side Code.........

#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;
}

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

Client Side Code........

#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套接字编程中用于图像、视频、文本文件等文件传输的代码.(从客户端到服务器端文件夹).

This is the code for file transfer like images, videos, text files in UDP socket programming.(From Client Side To Server Side folder).

谁能向我解释一下为什么两边都使用 WHILE 循环,它的目的是什么?此外,一个问题是使用这个程序文件从客户端正确发送到服务器,但到达服务器端后文件名被更改.

Can anyone explain to me why the WHILE loop is being used on both the sides, What is its purpose? Also, one problem is using this program the file is sent properly from client to server but after reaching the server side the filename is being changed.

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

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

After testing I found out that the changed file name on the server side is set to the current position of the file pointer that is in len variable in client side.(value of len is same as variable mm in server side and variable 'siz' on client side.

例如:

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

If I send "Wallpaper.png" from server side it will reach the server side and the file is saved by the name "164101". But the image is displayed properly.

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

AND WHY IS memset USED SO MANY TIMES ?? WHAT IS ITS ACTUAL USE ??

推荐答案

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

Your client and server speak the same file transfer protocol, defined as follows :

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

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

On the server side, you're messing with variables that should store the file name fname and the one that should store the file length fna. You should probably rename the latter to flen like in the following snippet :

...
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;
...

希望这会有所帮助!

这篇关于udp socket编程文件传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆