Linux TCP 文件传输模块(2)

server_addr.sin_family = AF_INET;
        server_addr.sin_port = htons(server_port);
        server_addr.sin_addr.s_addr = inet_addr(server_ip);
#ifdef DEBUG
        printf("server ip: %s.\n",inet_ntoa(server_addr.sin_addr));
#endif
        if(connect(sock_fd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1)
        {
#ifdef DEBUG
                printf("Cannot connect to server.\n");
#endif
                close(sock_fd);
                return FAILED;
        }

//First send member "file_name_size" and "file_to_send"
        send(sock_fd,&netfile->file_name_size,sizeof(netfile->file_name_size),0);
        send(sock_fd,netfile->file_to_send,strlen(netfile->file_to_send),0);
        //Then send file_data_size
        send(sock_fd,&netfile->file_data_size,sizeof(netfile->file_data_size),0);
        //Then send file data
        while(1)
        {
               bytes_read = read(netfile->fd,netfile->buf,BUF_SIZE);
                if(bytes_read > 0)
                        send(sock_fd,netfile->buf,bytes_read,0);
                else
                        break;
        }

close(netfile->fd);
        netfile->fd_stat = FD_NOT_SET;
        close(sock_fd);

return SUCCESS;
}

/*----close----*/
void close_net_file(netfile_t *netfile)
{
        if(netfile == NULL)
                return ;

if(netfile->fd_stat == FD_OK)
        {
                netfile->fd_stat = FD_NOT_SET;
                close(netfile->fd);
        }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwsxgy.html