zoukankan      html  css  js  c++  java
  • Linux下基于C实现的socket简单文件下载实例

    此实例是客户端向服务器端下载文件:

    服务器端代码实现:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    #include <errno.h>
    #include <fcntl.h>

    #define FD_SERVER_PORT 8088 /*set port*/
    #define FD_BUFFERSIZE 1024 /*set buffer size*/
    #define FD_LISTENQ 10   /*set listen max conncent*/

    int main(int argc,char *argv[])
    {
    int fd_listenfd,fd_connfd,fd_filefd; /*descriptor*/
    int fd_read,fd_write;
    int struct_size;
    struct sockaddr_in fd_servaddr,fd_cliaddr;
    char *fd_filename;
    char *p;
    char buffer[FD_BUFFERSIZE];

    fd_listenfd=socket(AF_INET,SOCK_STREAM,0); /*create socket*/
    if(fd_listenfd==-1){
       perror("fd_socket");
       exit(1);
    }

    memset(&fd_servaddr,0,sizeof(fd_servaddr)); /*servaddr set 0*/

    fd_servaddr.sin_family=AF_INET;    /*init serveraddr*/
    fd_servaddr.sin_addr.s_addr=INADDR_ANY;
    fd_servaddr.sin_port=htons(FD_SERVER_PORT);

    /*bind fd_listenfd*/

    if(-1==(bind(fd_listenfd,(struct sockaddr *)&fd_servaddr, sizeof(fd_servaddr)))){
       perror("fd_bind");
       exit(1);
    }

    /*listen fd_listenfd*/
    if(-1==(listen(fd_listenfd,FD_LISTENQ))){
       perror("fd_listen");
       exit(1);
    }

    /*file download server start*/
    while(1){
       printf("file download server starting......\n");

       memset(&fd_cliaddr,0,sizeof(fd_cliaddr));
       struct_size=sizeof(fd_cliaddr);

       fd_connfd=accept(fd_listenfd,(struct sockaddr *)&fd_cliaddr,&struct_size);
       if(-1==fd_connfd){
        perror("fd_accpet");
        continue;
       }

       fd_filename="/root/tmp.txt";
       printf("will download file name is:%s\n",fd_filename);
       fd_filefd=open(fd_filename,O_RDONLY);
       if(-1==fd_filefd){
        perror("open will download file");
        continue;
       }
       while(fd_read=read(fd_filefd,buffer,FD_BUFFERSIZE)){
        if(-1==fd_read){
         perror("read will download file");
         break;
        }

        p=buffer;
        while(fd_write=write(fd_connfd,p,fd_read)){
         if(-1==fd_write){
          perror("write client file");
          break;
         }
         else if(fd_read==fd_write) break;
         else if(fd_write>0){
          p+=fd_write;
          fd_read-=fd_write;   
         }
        }
       }
       if(-1==fd_read||-1==fd_write) continue;
       close(fd_filefd);
       close(fd_connfd);
       printf("file already is downloaded!\n");
    }
    close(fd_listenfd);
    return 0;
    }

    客户端代码实现:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    #include <errno.h>
    #include <fcntl.h>

    #define FD_SERVER_PORT 8088 /*set port*/
    #define FD_BUFFERSIZE 1024 /*set buffer size*/

    int main(int argc,char *argv[])
    {
    int fd_sockfd,fd_filefd; /*descriptor*/
    int fd_read,fd_write;
    int struct_size;
    struct sockaddr_in fd_sockaddr;
    char *fd_filename;
    char *p;
    char buffer[FD_BUFFERSIZE];

    if (argc!=2) {
       perror("fd_Usage: <./client> <server IP>\n");
       exit(1);
    }
    fd_sockfd=socket(AF_INET,SOCK_STREAM,0); /*create socket*/
    if(fd_sockfd==-1){
       perror("fd_socket");
       exit(1);
    }

    memset(&fd_sockaddr,0,sizeof(fd_sockaddr)); /*servaddr set 0*/

    fd_sockaddr.sin_family=AF_INET;    /*init serveraddr*/
    inet_aton(argv[1],&fd_sockaddr.sin_addr);
    fd_sockaddr.sin_port=htons(FD_SERVER_PORT);

    if(-1==(connect(fd_sockfd,(struct sockaddr *)&fd_sockaddr,sizeof(fd_sockaddr)))){
       perror("fd_connect");
       exit(1);
    }
    printf("start connecting..........\n");

    fd_filename="./backup.txt";
    fd_filefd=open(fd_filename,O_RDWR|O_CREAT|O_TRUNC,S_IRWXU);
    if (-1==fd_filefd) {
       perror("open the backup file");
       exit(1);
    }
    while(fd_read=read(fd_sockfd,buffer,FD_BUFFERSIZE)) {
       if(-1==fd_read){
        perror("read server file");
        exit(1);
       }

       /*p is used to wirte error*/
       p=buffer;
       while(fd_write=write(fd_filefd,p,fd_read)){
        if(-1==fd_write){
         perror("write localhost file");
         exit(1);
        }
        else if(fd_read==fd_write) break;
        else if(fd_write>0){
         p+=fd_write;
         fd_read-=fd_write;
        }
       }
    }
    close(fd_filefd);
    close(fd_sockfd);
    printf("finsh file download!\n");
    return 0;

    }

  • 相关阅读:
    融资担保公司
    典当公司
    保险代理、经纪公司互联网保险
    财产、人身、养老保险公司
    105家基金子公司
    LogStash Download
    cmd使用管理员权限运行,启动路径不是当前目录
    Module controller in JMeter
    Elasticsearch-->Get Started--> Exploring Your Data
    What are the differences between Flyweight and Object Pool patterns?
  • 原文地址:https://www.cnblogs.com/zzxap/p/2175801.html
Copyright © 2011-2022 走看看