用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

芙蓉妹妹    -  云代码空间

——

利用UDP完成linux和windows平台的文件传输

2018-11-05|28阅||

摘要:利用循环无连接方式完成linux和windows平台的文件传输(考虑服务器地址、端口号的设定方式);完成客户端、服务器端代码的封装。

一、服务端(Windowsserver.c)

#pragma comment(lib,"ws2_32")

#include <stdio.h>

#include <winsock.h>

#include <fcntl.h>

#define PORT 8888

#define BUFFER_SIZE 1024

typedef struct sockaddr_in addr;

int Initsocket(void);

int Initsocket(void)

{

    WSADATA wsadata;

    WORD version;

    int err;

    version = MAKEWORD(2, 2);

    err = WSAStartup(version, &wsadata);

    if(err)

    {

        printf("Error %d:winsock not available\n",err);

        return 1;

    }

    return 0;

}

void get_filename(char *filepath,char *filename)

{

    /*解析文件名*/

    int i=0,k=0;

    for(i=strlen(filepath); i>=0; i--)

    {

        if(filepath[i]!='/')

            k++;

        else

            break;

    }

    strcpy(filename,filepath+(strlen(filepath)-k)+1);

}

void process_conn_server(SOCKET s)

{

    addr client;

    int addrlen;

    char filename[100];

    char filepath[100];

    char *buffer;//file buffer

    int fileTrans;

    buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);

    memset(buffer,0,BUFFER_SIZE);

    int lenfilepath;

    FILE *fp;

    int writelength;

    addrlen = sizeof(client);

    while(1)

    {

        memset(filename,'\0',sizeof(filename));

        memset(filepath,'\0',sizeof(filepath));

        //接受文件路径,返回文件路径的长度

        lenfilepath = recvfrom(s,filepath,100,0,(struct sockaddr*)&client,&addrlen);

        ntohl(client.sin_addr.s_addr);

        printf("%s",inet_ntoa(client.sin_addr));

        printf("filepath :%s\n",filepath);

        if(lenfilepath<0)

        {

            printf("recv error!\n");

            exit(1);

        }

        //从路径中提取出文件名

        else

            get_filename(filepath,filename);

        printf("filename :%s\n",filename);

        //以写的方式打开文件

        fp = fopen(filename,"w");

        if(fp!=NULL)

        {

            //接受文件内容buffer,存在buffer

            while((fileTrans =recvfrom(s,buffer,BUFFER_SIZE,0,(struct sockaddr*)&client,&addrlen)))

            {

                printf("fileTrans: %d\n",fileTrans);

                if(fileTrans<0)

                {

                    printf("recv error!\n");

                    break;

                }

                //buffter中的文件内容写到fp指针指的文件中

                writelength = fwrite(buffer,sizeof(char),fileTrans,fp);

                if(writelength <fileTrans)

                {

                    printf("write error!\n");

                    break;

                }

                memset(buffer,0,BUFFER_SIZE);

            }

            printf("recv finished!\n");

            fclose(fp);

        }

        //客户端如果文件名不存在直接终止程序导致filenamenull

        else

            printf("filename is null!\n");

    }

}

int main()

{

    SOCKET sockfd;

    int err;

    addr server_addr;

    Initsocket();

    if( (sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET )

    {

        printf("no more socket resource\n");

        return 1;

    }

    //服务器地址初始化

    memset(&server_addr,0,sizeof(server_addr));

    server_addr.sin_family = PF_INET;

    server_addr.sin_port = htons(PORT);

    server_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

    if( (err = bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr))) == -1)

        printf("bind error!\n");

    else

        printf("bind success!\n");

    process_conn_server(sockfd);

    closesocket(sockfd);

    WSACleanup();

    return 0;

}

二、服务端(Linuxclient.c)

#include <stdio.h>

#include <sys/socket.h>

#include <sys/types.h>

#include <string.h>

#include <netinet/in.h>

#include <errno.h>

#include <memory.h>

#include <stdlib.h>  //for malloc

#include <arpa/inet.h>

#define BUFFER_SIZE 1024

int main()

{

    int sockcd;

    struct sockaddr_in server;

    char filepath[100];//file to translate

    FILE *fp;

    int lenpath; //filepath length

    char *buffer;//file buffer

    int fileTrans;

    buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);

    bzero(buffer,BUFFER_SIZE);

    //创建套接字

    if((sockcd = socket(AF_INET,SOCK_DGRAM,0))<0)

        printf("socket build error!\n");

    memset(&server,0,sizeof(server));

    server.sin_family= AF_INET;

    server.sin_port = htons(8888);

    if(inet_pton(AF_INET,"192.168.137.241",&server.sin_addr)<0)

        printf("inet_pton error!\n");

    printf("file path:\n");

    scanf("%s",filepath);//get filepath

    //以读的方式打开文件

    fp = fopen(filepath,"r");//opne file

    //文件找不到直接退出程序

    if(fp==NULL)

    {

        printf("filepath not found!\n");

        return 0;

    }

    printf("filepath : %s\n",filepath);

    lenpath = sendto(sockcd,filepath,strlen(filepath),0,(struct sockaddr *)&server,sizeof(server));// put file path to sever

    if(lenpath<0)

        printf("filepath send error!\n");

    else

        printf("filepath send success!\n");

    sleep(3);

    while((fileTrans = fread(buffer,sizeof(char),BUFFER_SIZE,fp)) >= 0)

    {

        printf("fileTrans =%d\n",fileTrans);

        //发送文件的内容

        int flag;

        if( (flag = sendto(sockcd,buffer,fileTrans,0,(struct sockaddr *)&server,sizeof(server))) < 0)

        {

            printf("send failed!\n");

            break;

        }

        else if(flag == 0){

            printf("send finished!\n");

            break;

        }

        bzero(buffer,BUFFER_SIZE);

    }

    fclose(fp);

    close(sockcd);

    return 0;

}


三、实验截屏

Windows服务端:

Linux客服端:

实验前无test2.txt:

实验后文件:

顶 0踩 0收藏
分享到:
更多
文章评论
    发表评论