用户注册



邮箱:

密码:

用户登录


邮箱:

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

发表随想


还能输入:200字

芙蓉妹妹    -  云代码空间

——

利用TCP完成文件传输

2018-11-05|22阅||

摘要:利用循环面向连接的模型完成固定文件的传输(考虑服务器地址、端口号的设定方式);手动输入或选择文件

一、客户端(client.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

#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_STREAM,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.91.128",&server.sin_addr)<0)

        printf("inet_pton error!\n");

    if(connect(sockcd,(struct sockaddr*)&server,sizeof(server))<0)

        printf("connect 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 = 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);

    }

    fclose(fp);

    close(sockcd);

    return 0;

}

二、服务端(sever.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

#define MAXLINE 4096

#define BUFFER_SIZE 1024

int main()

{

    int sockfd,temp,n;

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

    int lenfilepath;

    FILE *fp;

    int writelength;

    if((sockfd = socket(AF_INET,SOCK_STREAM,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(8888);

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

        printf("bind error!\n");

    else

        printf("bind success!\n");

    if(listen(sockfd,10)==-1)

        printf("listen error!\n");

    else

        printf("listen success!\n");

    printf("waiting..\n");

    while(1)

    {

        if((temp = accept(sockfd,(struct sockaddr*)NULL,NULL))>0)

        {

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

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

            lenfilepath = recv(temp,filepath,100,0);

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

            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)

            {

                while(fileTrans =recv(temp,buffer,BUFFER_SIZE,0))

                {

                    if(fileTrans<0)

                    {

                        printf("recv error!\n");

                        break;

                    }

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

                    if(writelength <fileTrans)

                    {

                        printf("write error!\n");

                        break;

                    }

                    bzero(buffer,BUFFER_SIZE);

                }

                printf("recv finished!\n");

                fclose(fp);

            }

            else

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

            close(temp);

        }

        else

        {

            printf("accept error!\n");

            close(temp);

        }

    }

    close(sockfd);

    return 0;

}
三、实验截屏
服务端;

客户端:

实验前无test.txt文件:

试验后出现了test.txt:

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