网络程序设计——2.利用TCP/UDP完成文件传输的设计和实现(上)

运行环境

VMware搭载的Linux虚拟机

Linux版本:Ubantu14.04LTS

 

一、实验目的

1.利用循环面向连接的模型完成固定文件的传输(考虑服务器地址、端口号的设定方式)

2.由固定文件扩展成手动输入或选择文件;

3.参考connectTCP、connectUDP的方式对程序进行抽象、封装;(见下篇)

二、实验内容

TCP编程

1.tcp_server.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT 8888
#define BUFFER_SIZE 1024

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(int sd)
{
    ssize_t size = 0;
    char buffer[BUFFER_SIZE];
    FILE *stream;
    char filepath[100];

    strcpy(buffer,"please enter a path!\n");
    write(sd,buffer,BUFFER_SIZE);
    int length = 0;
    memset(filepath,'\0',sizeof(filepath));

    length = read(sd,filepath,100);
    if(length < 0){
        printf("recv error!\n");
    }
    else
    {
        char filename[100] = {'\0'};
        get_filename(filepath,filename);
        printf("server: filename:\n%s",filename);

        if( (stream=fopen(filename, "w")) == NULL){
            printf("server:open file error!\n");
            return;
        }
        while(1){/*读取文件并写入文件流*/
            size = read(sd, buffer, BUFFER_SIZE);
            printf("server:size:%d\n",size);
            if(size <= 0){
                break;
            }
            int write_len=fwrite(buffer, sizeof(char), size, stream);
        }
        printf("recv finished!\n");
        fclose(stream);
    }
}

int main(int argc, char *argv[]){
    int socksd,sockcd;
    struct sockaddr_in server,client;
    pid_t pid;
    if( (socksd = socket(AF_INET,SOCK_STREAM,0)) < 0)
    {
        printf("socket create error!\n");
        return -1;
    }
    printf("socket create success!\n");

    /*加入此代码是为了避免再次打开服务器程序出现bind error的错误*/
    int on = 1;
    int ret = setsockopt(socksd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

    memset(&server,0,sizeof(server));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(PORT);
    if( (bind(socksd,(struct sockaddr*)&server,sizeof(server)) < 0))
    {
        printf("socket bind error!\n");
        return -1;
    }
    printf("socket bind success!\n");

    if( (listen(socksd,10)) < 0)
    {
        printf("socket listen error!\n");
        return -1;
    }
    printf("socket listen success!\n");

    printf("waiting...\n");
    /*处理sigchld信号,防止产生僵尸进程*/
    if(signal(SIGCHLD, SIG_IGN) == SIG_ERR){
            perror("signal error");
            return EXIT_SUCCESS;
        }
    while(1){
        socklen_t addr_len = sizeof(struct sockaddr);
        if( (sockcd = accept(socksd, (struct sockaddr*)&client, &addr_len)) < 0)
        {
            //出错
            continue;
        }
        printf("server:accept\n");
        /*建立一个新进程来处理到来的连接*/
        pid = fork();
        if(pid == 0)
        {
            process_conn_server(sockcd);
            close(socksd);/*在子进程中关闭服务器的监听*/
            exit(0);
            return 0;
        }
        else{
            close(sockcd);/*在父进程中关闭客户端的监听*/
        }
    }
    return 0;
}

2.tcp_client.c

#include 
#include 
#include 
#include 
#include 
#include 

#define PORT 8888
#define BUFFER_SIZE 1024

void process_conn_client(int s)
{
    ssize_t size = 0;
    char buffer[BUFFER_SIZE];
    FILE *stream;

    int length = 0;
    char filepath[100] = {'\0'};
    size = read(s, buffer, BUFFER_SIZE);
    printf("%s",buffer);

    scanf("%s",filepath);
    write(s,filepath,100);

    if( (stream = fopen(filepath,"r")) == NULL) {
            printf("client:open file error!\n");
            return;
    }
    printf("sending!\n");
    while(1){
        size = fread(buffer,sizeof(char),BUFFER_SIZE,stream);
        if(size <= 0){
            break;
        }
        write(s,buffer,size);
    }
    printf("send finished!\n");
    fclose(stream);
}

int main(int argc, char *argv[]){
    int socksd;
    struct sockaddr_in server_addr;

    if( (socksd = socket(AF_INET,SOCK_STREAM,0)) < 0)
    {
        printf("socket create error!\n");
        return -1;
    }
    printf("socket create success!\n");

    memset(&server_addr, 0 , sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(PORT);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);//本机地址
    /*将用户输入的字符串类型的IP地址转为整型*/
    inet_pton(AF_INET,argv[1],&server_addr.sin_addr);

    if( (connect(socksd,(struct sockaddr*)&server_addr,sizeof(struct sockaddr)) < 0)){
        printf("client conn fail\n");
        return -1;
    }
    printf("client conn!\n");
    process_conn_client(socksd);
    close(socksd);
    return 0;
}

运行结果

    编译打开服务器程序:

网络程序设计——2.利用TCP/UDP完成文件传输的设计和实现(上)_第1张图片

    编译并打开客户端程序:

网络程序设计——2.利用TCP/UDP完成文件传输的设计和实现(上)_第2张图片

    其中,输入路径是windata下的1.png文件(其中我用的windata是一个与windows共享的文件夹,在linux下操作它,windows也会有相应的操作结果),如下:

网络程序设计——2.利用TCP/UDP完成文件传输的设计和实现(上)_第3张图片

    然后服务器端在将它传输到exp2文件夹内,如下:

    exp2文件夹,传输前效果:

网络程序设计——2.利用TCP/UDP完成文件传输的设计和实现(上)_第4张图片

    传输后效果:

网络程序设计——2.利用TCP/UDP完成文件传输的设计和实现(上)_第5张图片

UDP编程

3.udp_server.c

因为自己的udp并没有做好,但是应大家的要求还是写全吧,以下是借用别人的代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include   //for malloc
#include 

#define MAXLINE 4096
#define BUFFER_SIZE 1024

int main()
{
    int sockfd,temp,n,num;
    struct sockaddr_in server,client;
    char filename[100];
    char filepath[100];

    char *buffer;//file buffer
    int fileTrans;

    buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
    bzero(buffer,BUFFER_SIZE);
    //memset(buffer,0,sizeof(buffer));
    int lenfilepath;
    FILE *fp;
    int writelength;
    socklen_t addrlen;
//创建套接字
    if((sockfd = socket(AF_INET,SOCK_DGRAM,0))<0)
    {
        printf("socket build error!\n");
    }
    else
    {
        printf("socket build success!\n");
    }
    memset(&server,0,sizeof(server));
    server.sin_family= AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(6666);
//绑定套接字
    if((bind(sockfd,(struct sockaddr*)&server,sizeof(server)))==-1)
    {
        printf("bind error!\n");
    }
    else
    {
        printf("bind success!\n");
    }
    addrlen=sizeof(client);
    while(1)
    {
            memset(filename,'\0',sizeof(filename));
            memset(filepath,'\0',sizeof(filepath));
//接受文件路径,返回文件路径的长度
            lenfilepath = recvfrom(sockfd,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
            {
                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);
            }
            printf("filename :%s\n",filename);
            //以写的方式打开文件
            fp = fopen(filename,"w");
            if(fp!=NULL)
            {
                //接受文件内容buffer,存在buffer中
                while((fileTrans =recvfrom(sockfd,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 

4.udp_client.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include   //for malloc
#include 

#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);
    //memset(buffer,0,sizeof(buffer));

//创建套接字
    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(6666);
    if(inet_pton(AF_INET,"127.0.0.1",&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);
        //memset(buffer,0,sizeof(buffer));
    }
    fclose(fp);

    close(sockcd);



    return 0;
}

    UDP的编译过程结果同TCP,亲测可以跑。

 

对于想要进一步学习的网友,可以去找下篇封装成connectTCP和connectUDP的实现代码。

你可能感兴趣的:(socket)