利用TCP完成文件传输的设计和实现

一、实验目的

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

2)有固定文件扩展成手动输入或选择文件。

3)参考connectTCP的方式对程序进行抽象、封装。

 

服务器代码:

#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 MAXLINE 4096
#define BUFFER_SIZE 1024
void recvfilename(int temp)
{
    char filename[100];
    char filepath[100];
    memset(filename,'\0',sizeof(filename));
    memset(filepath,'\0',sizeof(filepath));
    char *buffer;//file buffer
    buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
    bzero(buffer,BUFFER_SIZE);
    int lenfilepath;
    int fileTrans;
    lenfilepath = recv(temp,filepath,100,0);
    printf("filepath :%s\n",filepath);
    
    FILE *fp;
    int writelength;
    if(lenfilepath<0)
    {
        printf("recv error!\n");
    }
    //从路径中提取出文件名
    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 =recv(temp,buffer,BUFFER_SIZE,0))
        {
            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;
            }
            bzero(buffer,BUFFER_SIZE);
            //memset(buffer,0,sizeof(buffer));
        }
        printf("recv finished!\n");
        fclose(fp);
    }
}
//创建套接字等一系列操作
int build_sock()
{
    int sockfd;
    struct sockaddr_in server;
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd < 0)
    {
        printf("socket build error!\n");
        exit(1);
    }
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");
        exit(1);
    }
printf("bind success!\n");

    //listen监听
    if(listen(sockfd,10) == -1)
    {
        printf("listen error!\n");
        exit(1);
    }
    printf("listening..\n");
    return sockfd;
}
int main()
{
    int temp;//accept
    struct sockaddr_in client;
    socklen_t addrlen;
    int sockfd;
    
    //创建套接字
    sockfd = build_sock();
    
    addrlen = sizeof(client);
    while(1)
    {
        temp = accept(sockfd,(struct sockaddr*)&client,&addrlen);
        if(temp <= 0)
        {
            printf("accept error!\n");
            close(temp);
        }
        else
        {
            printf("client IP:   %s\n",inet_ntoa(client.sin_addr));
        }

        //接受文件路径,返回文件路径的长度
        recvfilename(temp);

    }
    return 0;
}

 

 

客户端代码:

#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

#define BUFFER_SIZE 1024
//传送路径还有文件一系列操作
void send_file(int sockcd)
{
    char filepath[100];
    FILE *fp;
    int lenpath; //filepath length
    char *buffer;//file buffer
    int fileTrans;
    buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
    bzero(buffer,BUFFER_SIZE); 
    printf("file path:\n");
    scanf("%s",filepath);//get filepath

    fp = fopen(filepath,"r");//opne file
    if(fp==NULL)
    {
        printf("filepath not found!\n");
        exit(1);

    }
    printf("filepath : %s\n",filepath);
    lenpath = send(sockcd,filepath,strlen(filepath),0);// 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);
        if(send(sockcd,buffer,fileTrans,0)<0)
        {
            printf("send failed!\n");
            break;      
        }
        bzero(buffer,BUFFER_SIZE); 
        //memset(buffer,0,sizeof(buffer));  
    }
    fclose(fp);

    

}
int main()
{
    int sockcd = connectTCP("localhost","6666");
    send_file(sockcd);
    close(sockcd);
    return 0;
}

封装函数如下,可以调用connectTCP或connectUDP直接进行连接

 

 

 运行服务器代码,

运行客户端代码 

 这时候出现客户端ip地址

 

 客户端传送文件

 服务器收到文件

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值