C ++套接字编程(read()write())

时间:2015-04-26 19:47:54

标签: c++ sockets

我正在编写一个程序,将某些文件的内容从客户端传输到服务器,但我遇到了一些问题:

  1. 客户端为服务器写入的字节数不等于服务器读取的字节数。
  2. 我的解决方案是发送服务器文件的大小吗?
  3. client.cpp

    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <iostream>
    #include <cstdlib>
    #include <sys/stat.h>
    #include <errno.h>
    #include <dirent.h>
    #include <cstdio>
    #include <string>
    #include <fstream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sstream>
    
    using namespace std ;
    
    const int BF_SIZE = 2048 ;
    
    void R_directory ( int socket_id ){
        DIR *dir ;
        struct dirent *pent ;
        dir = opendir( "address of files" ) ;
        string path = "address of files" ;
    
        char BF_read[BF_SIZE] ;
        int n ;
    
        if ( dir == NULL ){
            cout << "No such directory" << endl ;
        }
        else{
            while (  pent = readdir ( dir ) ){
                if ( pent == NULL ) break ;
                if( !strcmp( pent->d_name , ".") || !strcmp ( pent->d_name , "..") ) continue ;
                string abspath = path , a ;
                for ( int i = 0 ; i < pent->d_name[i] ; i++ ) abspath.push_back( pent->d_name[i] ) ;
                FILE *fi = NULL ;
                fi = fopen ( abspath.c_str() , "r" ) ;
                struct stat fi_sp ;
                stat( abspath.c_str() , &fi_sp );
                if ( fi == NULL ){
                    cout << "Can't open file " << abspath << endl ;
                    continue ;
                }
                if ( fi_sp.st_size == 0 ){
                    cout << "File " << abspath << " is empty" << endl ;
                    continue ;
                }
                int file_size = 0 ;
                file_size = fi_sp.st_size ;
                write ( socket_id , &file_size , 4 ) ;
                cout << abspath << " " << file_size << endl ;
                while ( ( n = fread ( BF_read , 1 , BF_SIZE , fi ) ) > 0 && file_size > 0){
                    cout << n << endl ;
                    file_size -= n ;
                    if ( file_size > 0 && n >= BF_SIZE ){
                        write ( socket_id , BF_read , sizeof ( BF_read ) ) ;
                    }
                    else{
                        write ( socket_id , BF_read , n - 1 ) ;
                    }
                }
    
                cout << "=====================================" << endl ;
                fclose(fi);
            }
            int file_size = 0 ;
            write ( socket_id , &file_size , 4 ) ;
        }
        closedir(dir);
        return ;
    }
    
    int main ( int argc , char* argv[] ){
        int sockfd = 0,n = 0;
        struct sockaddr_in serv_addr;
    
        if((sockfd = socket(AF_INET, SOCK_STREAM, 0))< 0)
        {
            cout << "Error : Could not create socket" << endl ;
            return 1;
        }
    
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(5000);
        serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    
        if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0)
        {
            cout << "Error : Connect Failed" << endl ;
            return 1;
        }
    
        R_directory( sockfd );
    
        return 0 ;
    }
    

    server.cpp

    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <iostream>
    #include <fstream>
    
    using namespace std ;
    
    const int BF_SIZE = 2048 ;
    
    int main(void)
    {
        int listenfd = 0,connfd = 0;
    
        struct sockaddr_in serv_addr;
    
        char sendBuff[BF_SIZE] , recvBuff[BF_SIZE] ;
        int numrv;
    
        listenfd = socket(AF_INET, SOCK_STREAM, 0);
        printf("socket retrieve success\n");
    
        memset(&serv_addr, '0', sizeof(serv_addr));
        memset(sendBuff, '0', sizeof(sendBuff));
    
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        serv_addr.sin_port = htons(5000);
    
        bind(listenfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr));
    
        if(listen(listenfd, 10) == -1){
            printf("Failed to listen\n");
            return -1;
        }
    
        connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL);
        ofstream cout ( "output.txt" , std::ios::app ) ;
    
        while(1)
        {    
            int file_size = 0 , n ;
            n = read ( connfd , &file_size , 4 ) ;
            //cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl ;
            if ( file_size == 0 ) break ;
            while ( file_size > 0 ){
                n = read( connfd , recvBuff, sizeof(recvBuff) ) ;
                cout << n << endl ;
                if ( n == 0 ) break ;
                file_size -= n ;
                /*for ( int i = 0 ; i < n ; i++ ){
                    cout << recvBuff[i] ;
                }*/
            }
            cout << endl ;
        }
        close(connfd);
    
        return 0;
    }
    

    在客户端,我发送的字节数等于文件大小(以字节为单位),但在服务器中我有更多的字节 - 为什么?

0 个答案:

没有答案