1. 程式人生 > >網路程式設計——3.TCP/UDP實現跨平臺檔案傳輸

網路程式設計——3.TCP/UDP實現跨平臺檔案傳輸

一、實驗目的

    在上次檔案傳輸的程式基礎上,利用循環面向連線、迴圈無連線方式完成linux和windows平臺的檔案傳輸,並完成客戶端、伺服器端程式碼的封裝。

二、實驗分析

     1.使用TCP實現

  •   在上次實驗基礎上,使用tcp_server.c的程式碼,只需修改為迴圈方式即可,然後作為Linux伺服器使用
  •   編寫Windows下的客戶端程式,進行檔案傳送

     2.使用UDP實現

  •   在上次實驗的基礎上,使用udp_client.c的程式碼,只需更改IP和埠號即可
  •   編寫Windows下伺服器端程式,進行檔案接收

三、實驗內容

1.循環面向連線方式(TCP)

1.tcp_server.c

//Run in Linux
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <signal.h>

#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");
    send(sd,buffer,BUFFER_SIZE,0);
    int length = 0;
    memset(filepath,'\0',sizeof(filepath));

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

        if( (stream=fopen(filename, "w")) == NULL){
            printf("server:open file error!\n");
            return;
        }
        while(1){/*讀取檔案並寫入檔案流*/
            size = recv(sd, buffer, BUFFER_SIZE,0);
            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");
        //different from exp2
        process_conn_server(sockcd);
        close(sockcd);/*在子程序中關閉伺服器的監聽*/
    }
    return 0;
}

2.tcp_client.c

//Run in Windows
#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);
void process_conn_client(SOCKET s);

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 process_conn_client(SOCKET s)
{
    ssize_t size = 0;
    char buffer[BUFFER_SIZE];
    FILE *stream;

    char filepath[100] = {'\0'};
    size = recv(s, buffer, BUFFER_SIZE,0);
    if(size < 0)
        printf("read error!\n");
    printf("%s",buffer);

    scanf("%s",filepath);
    send(s,filepath,100,0);

    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;
        }
        send(s,buffer,size,0);
    }
    printf("send finished!\n");
    fclose(stream);
}

int main()
{
    SOCKET client;
    int err;
    addr server_addr;

    Initsocket();

    if( (client = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET )
    {
        printf("no more socket resource\n");
        return 1;
    }
    //伺服器地址初始化
    server_addr.sin_family = PF_INET;
    server_addr.sin_port = htons(PORT);
    server_addr.sin_addr.S_un.S_addr = inet_addr("192.168.195.137");//使用自己伺服器端的IP
    if( (err = connect(client, (struct sockaddr*)
&server_addr, sizeof(addr))) == INVALID_SOCKET)
    {
        printf("error %d:cannot connect to server\n",err);
        return 1;
    }
    else
        printf("link server is successful\n");
     process_conn_client(client);
     closesocket(client);
     WSACleanup();
     return 0;
}

3.執行結果

  • Linux作為伺服器端

  • Windows作為客戶端

2.迴圈非連線方式(UDP)

1.udp_server.c

//Run in Windows
#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);
            }
            //客戶端如果檔名不存在直接終止程式導致filename是null
            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;
}

2.udp_client.c

//Run in Linux
#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);
    //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(8888);
    if(inet_pton(AF_INET,"192.168.195.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;
}

3.執行結果

  • LINUX作為客戶端

  • WINDOWS作為伺服器端

  • 傳輸成功