码迷,mamicode.com
首页 > 系统相关 > 详细

linux网络编程-(socket套接字编程UDP传输)

时间:2015-10-27 23:58:56      阅读:461      评论:0      收藏:0      [点我收藏+]

标签:

今天我们来介绍一下在linux网络环境下使用socket套接字实现两个进程下文件的上传,下载,和退出操作!

在socket套接字编程中,我们当然可以基于TCP的传输协议来进行传输,但是在文件的传输中,如果我们使用TCP传输,会造成传输速度较慢的情况,所以我们在进行文件传输的过程中,最好要使用UDP传输。

在其中,我们需要写两个程序,一个客户端,一个服务端,在一个终端中,先运行服务端,在运行客户端,在服务端和客户端都输入IP地址和端口号,注意服务端和客户端的端口号要相同,然后选择功能,在linux网络编程中,使用UDP进行传输属于比较简单的操作,所以直接上代码吧,详细的讲解我将会在之后上传!

client(客户端):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "protocol.h"

/*--------------------------*/
int socketfd ;
int addrlen;
struct sockaddr_in server;
struct protocol sentbuf;
struct protocol recvbuf;
int num;
char ip[20];
int port;
int choice ;
char filename[100];

/*--------------------------*/
void ShowMenu();
void DownLoad();
void UpLoad();
void ShutDown();


int main(){
/*---------------------------*/
    socketfd = socket(AF_INET , SOCK_DGRAM , 0 );
    if(socketfd == -1){
        perror("socket failed!\n");
        exit(1);
    }
/*---------------------------*/
    printf("please input ip of server:\n");
    scanf("%s",ip);
    printf("please input port of server:\n");
    scanf("%d",&port);
/*---------------------------*/
    bzero(&server , sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = inet_addr(ip);
    addrlen = sizeof(server);
/*---------------------------*/
    while(1){
        ShowMenu();
        scanf("%d",&choice);
        if(choice == DOWNLOAD){
            printf("client download!\n");
            DownLoad();
        }
        else if(choice == UPLOAD){
            UpLoad();
        }
        else if(choice == SHUTDOWN){
            printf("client shutdown!\n");
            ShutDown();
            break;
        }
        else{
            printf("please input the right choice!\n");
        }
    }
    close(socketfd);
    return 0;
}

void ShowMenu(){
    printf("please make a choice:\n");
    printf("0:shutdown!\n");
    printf("1:download!\n");
    printf("2:upload!\n");
}

void DownLoad(){
    bzero(&recvbuf , sizeof(recvbuf));
    bzero(&sentbuf , sizeof(sentbuf));
    bzero(filename , sizeof(filename));
    printf("please input the filename:\n");
    scanf("%s",sentbuf.buf);
    sentbuf.command = DOWNLOAD;
    sendto(socketfd , &sentbuf , sizeof(sentbuf), 0 , (struct sockaddr*)&server , sizeof(server));
    bcopy(sentbuf.buf , filename , sizeof(sentbuf.buf));
    recvfrom(socketfd  , &recvbuf , sizeof(recvbuf) , 0 , (struct sockaddr*)&server , &addrlen);
    printf("recvbuf:%d\n",recvbuf.command);
    if(recvbuf.command == YES){
        printf("YES!\n");
        int choice_1;
        printf("if you input a 5 , the file transmission start!\n");
        scanf("%d",&choice_1);
        if(choice_1 == START){
            sentbuf.command = START;
            sendto(socketfd , &sentbuf , sizeof(sentbuf) , 0 , (struct sockaddr*)&server , sizeof(server));
            int no = 0 ;
            int fd = open(filename , O_CREAT | O_TRUNC | O_WRONLY , 0644 );
            if(fd < 0){
                perror("creat file is failed!\n");
                exit(1);
            }
            bzero(&recvbuf , sizeof(recvbuf));
            while( ( num = recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , 0 , (struct sockaddr*)&server , &addrlen)) > 0){
                if( recvbuf.command == CONTENT ){
                    if(no == recvbuf.no){
                        write(fd , recvbuf.buf , recvbuf.len);
                        bzero(&recvbuf , sizeof(recvbuf));
                    }
                    else{
                        perror("The file no is not same,  Some massage is missed!error occured!\n");
                        break;
                    }
                }
                if( recvbuf.command == END){
                    close(fd);
                    printf("transmission is successful!\n");
                    break;
                }
            }
        }
    }
    else if(recvbuf.command == NO){
        perror("No such file on server!\n");
    }
    else{
        perror("recvbuf.command error!\n");
        exit(1);
    }
}

void ShutDown(){
    sentbuf.command = SHUTDOWN;
    sendto(socketfd , &sentbuf , sizeof(sentbuf) , 0 , (struct sockaddr*)&server , sizeof(server));
    printf("client is end!\n");
}

void UpLoad(){
    bzero(&recvbuf , sizeof(recvbuf));
    bzero(&sentbuf , sizeof(sentbuf));
    bzero(filename , sizeof(filename));
    printf("please input you want to upload filename:\n");
    scanf("%s",sentbuf.buf);
    sentbuf.command = UPLOAD;
    sendto(socketfd , &sentbuf , sizeof(sentbuf), 0 , (struct sockaddr*)&server , sizeof(server));
    bcopy(sentbuf.buf , filename , sizeof(sentbuf.buf));
    int fd ;
    fd = open(filename , O_RDONLY);
    if(fd < 0){
        perror("The file is not exist!\n");
        exit(1);
    }
    recvfrom(socketfd , &recvbuf , sizeof(recvbuf), 0 , (struct sockaddr*)&server , &addrlen);
    if( recvbuf.command == START ){
        int no = 0 ;
        while( ( num = read(fd , sentbuf.buf , INFOLEN)) > 0 ){
            sentbuf.no = no ;
            sentbuf.command = CONTENT;
            sentbuf.len = strlen(sentbuf.buf);
            sendto(socketfd , &sentbuf , sizeof(sentbuf) , 0 , (struct sockaddr*)&server,sizeof(server));
            no++;
            bzero(&sentbuf , sizeof(sentbuf));
        }
        bzero(&sentbuf , sizeof(sentbuf));
        sentbuf.command = END;
        sendto(socketfd , &sentbuf , sizeof(sentbuf) , 0 , (struct sockaddr*)&server , sizeof(server));
    }
    else if(recvbuf.command == NO){
        printf("not transmission!\n");
    }
    else{
        perror("error! wrong choice!\n");
    }
}

server(服务端):

    #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "protocol.h"

/*-----------------------变量声明区------------------*/
    int socketfd;
    int addrlen;
    struct sockaddr_in server;
    struct sockaddr_in client;
    struct protocol sentbuf;
    struct protocol recvbuf;
    int num;
    char ip[20];
    int port;
    int choice;

int main(){

    /*-------------create UDP socket------------*/
    if((socketfd = socket(AF_INET,SOCK_DGRAM,0)) == -1){
        perror("socket error\n");
        exit(1);
    }

    /*-----------------IO-----------------------*/
    printf("Please input the ip:\n");
    scanf("%s",ip);
    printf("Please input the port:\n");
    scanf("%d",&port);

    /*-----------------bind----------------------*/
    bzero(&server,sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = inet_addr(ip);
    if (bind(socketfd,(struct sockaddr *)&server,sizeof(server)) == -1){
        perror("bind error\n");
        exit(1);
    }

    /*---------------------调试信息------------
    addrlen = sizeof(client);
    recvfrom(socketfd,&recvbuf,sizeof(recvbuf),0,(struct sockaddr *)&client,&addrlen);
    num = strlen(recvbuf.buf);
    recvbuf.buf[num] = ‘\0‘;
    printf("command %d\n",recvbuf.command );
    printf("len %d\n",recvbuf.len );
    printf("no %d\n", recvbuf.no);
    printf("buf %s\n",recvbuf.buf ); */

    addrlen = sizeof(client);
    while(1){
    bzero(&recvbuf,sizeof(recvbuf));
    num =recvfrom(socketfd,&recvbuf,sizeof(recvbuf),0,(struct sockaddr *)&client,&addrlen);
    choice = recvbuf.command;
    if(choice == DOWNLOAD){
        char buf[100];
        int fd;
        fd = open((recvbuf.buf),O_RDONLY);
        if(fd <0){
            sentbuf.command = NO;
            sendto(socketfd,&sentbuf,sizeof(sentbuf),0,(struct sockaddr *)&client,sizeof(client));
            printf("no such file!\n");
            exit(1);
        }
        else{
            sentbuf.command = YES;
            sendto(socketfd,&sentbuf,sizeof(sentbuf),0,(struct sockaddr *)&client,sizeof(client));
            recvfrom(socketfd,&recvbuf,sizeof(recvbuf),0,(struct sockaddr *)&client,&addrlen);
            if(recvbuf.command == START){
                int no =0;
                while((num = read(fd,sentbuf.buf,INFOLEN)) >0){
                    sentbuf.no = no;
                    sentbuf.command = CONTENT;
                    sentbuf.len = strlen(sentbuf.buf);
                    sendto(socketfd,&sentbuf,sizeof(sentbuf),0,(struct sockaddr *)&client,sizeof(client));
                    no++;
                    bzero(&sentbuf,sizeof(sentbuf));
                }
                bzero(&sentbuf,sizeof(sentbuf));
                sentbuf.command = END;
                sendto(socketfd,&sentbuf,sizeof(sentbuf),0,(struct sockaddr *)&client,sizeof(client));
            }
        }
    }
    else if(choice == UPLOAD){
        printf("The client want to upload the file:  %s\n",recvbuf.buf);
        printf("Please choice start or no? \n");
        printf("5 :start,  4: no\n");
        scanf("%d",&sentbuf.command);
        sendto(socketfd,&sentbuf,sizeof(sentbuf),0,(struct sockaddr *)&client,sizeof(client));
        if(sentbuf.command ==START){
            int no =0;
            int fd =open(recvbuf.buf,O_CREAT | O_TRUNC |O_WRONLY,0644);
            if(fd < 0){
                perror("create file error\n");
                exit(1);
            }
            bzero(&recvbuf,sizeof(recvbuf));
            while((num = recvfrom(socketfd,&recvbuf,sizeof(recvbuf),0,(struct sockaddr *)&server,&addrlen)) >0){
                if(recvbuf.command == CONTENT){
                    if(no == recvbuf.no){
                    write(fd,recvbuf.buf,recvbuf.len);
                    printf("kkk%s\n",recvbuf.buf );
                    bzero(&recvbuf,sizeof(recvbuf));
                }
                else{
                    perror("The file no is not same.Some message is missed! error occured! \n");
                    break;
                }
                }
                if(recvbuf.command == END){
                    close(fd);
                    printf("transmission is successful!\n");
                    break;
                }
            }
        }
        else if(sentbuf.command == NO){
            printf("Not to trans the file\n");
        }
        else{
            perror("error! wrong choice!\n");
            exit(1);
        }

    }
    else if (recvbuf.command == SHUTDOWN){
        printf("Now the server is shutdown!\n");
        break;
    }
}
    
    /*----------------------close ----------*/
    close(socketfd);
    return 0;
}

makefile:

main:udpserver.o udpclient.o
    gcc -o udpserver udpserver.o
    gcc -o udpclient udpclient.o
udpserver.o:udpserver.c
    gcc -c udpserver.c
udpclient.o:udpclient.c
    gcc -c udpclient.c

 

linux网络编程-(socket套接字编程UDP传输)

标签:

原文地址:http://www.cnblogs.com/turnips/p/4915782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!