簡單的C++檔案伺服器--Linux C++客戶端從服務端獲取檔案

大囚長發表於2019-01-21

client.cpp

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#define MAXLINE 4096

int main(int argc, char** argv){
    int   sockfd, len;
    char  buff[MAXLINE];
    struct sockaddr_in  servaddr;
    FILE *fp;
    int n;

    if( argc != 2){
        printf("usage: ./client <ipaddress>\n");
        return 0;
    }

    if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
        return 0;
    }

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(6666);
    if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
        printf("inet_pton error for %s\n",argv[1]);
        return 0;
    }

    if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
        printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
    }
    if( ( fp = fopen("sbt_client.with-apt-boost","ab") ) == NULL ){
        printf("File.\n");
        close(sockfd);
        exit(1);
    }

    while(1){
        n = read(sockfd, buff, MAXLINE);
        if(n == 0)
            break;
        fwrite(buff, 1, n, fp);
    }
    buff[n] = '\0';
    printf("recv msg from server: %s\n", buff);
    close(sockfd);
    fclose(fp);

    return 0;
}

server.cpp

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>

#define MAXLINE 4096

int main(int argc, char** argv){
    int  listenfd, sockfd, len;
    struct sockaddr_in  servaddr;
    char  buff[4096];
    FILE *fp;
    int  n;

    if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
        printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
    }
    printf("----init socket----\n");

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(6666);
    //設定埠可重用
    int contain;
    setsockopt(listenfd,SOL_SOCKET, SO_REUSEADDR, &contain, sizeof(int));

    if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
        printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
    }
    printf("----bind sucess----\n");

    if( listen(listenfd, 10) == -1){
        printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
    }
    if((fp = fopen("/root/sbt_client.with-apt-boost","rb") ) == NULL )
    {
        printf("File open.\n");
        close(listenfd);
        exit(1);
    }

    printf("======waiting for client's request======\n");
    while(1){
        struct sockaddr_in client_addr;
        socklen_t size=sizeof(client_addr);
        if( (sockfd = accept(listenfd, (struct sockaddr*)&client_addr, &size)) == -1){
            printf("accept socket error: %s(errno: %d)",strerror(errno),errno);
            continue;
        }
        bzero(buff,sizeof(buff));
        while(!feof(fp)){
            len = fread(buff, 1, sizeof(buff), fp);
            if(len != write(sockfd, buff, len)){
                printf("write.\n");
                break;
            }
        }
        close(sockfd);
        fclose(fp);
    }
    close(listenfd);
    return 0;
}

基於boost庫的非同步檔案伺服器

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>

#define MAXLINE 4096

// 非同步伺服器類
class Server {

private:
	// 檔案指標
	FILE *fp;

	// 讀取buff長度
	int len;

	// 檔案buff
	char  buff[MAXLINE];

	// 服務例項
	boost::asio::io_service& ios_;

	// 接收器例項
	boost::asio::ip::tcp::acceptor acceptor_;

	// socket智慧指標
	typedef boost::shared_ptr<boost::asio::ip::tcp::socket> socket_ptr;

public:

	Server(boost::asio::io_service& _ios) : ios_(_ios),
		acceptor_(_ios, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 6666)) {
		// 預設執行
		start();
	}

	// 啟動網路偵聽的操作入口
	void start(void) {
		// 自定義的智慧指標
		socket_ptr socket(new boost::asio::ip::tcp::socket(ios_));
		// 非同步偵聽,若有服務連線,則自動呼叫Server::accept_handler函式,並將error, socket傳入作為引數
		acceptor_.async_accept(*socket,
			boost::bind(&Server::accept_handler, this,
				boost::asio::placeholders::error/* 此處作為佔位符 */, socket));
	}

	// 請求者響應後觸發的處理器
	void accept_handler(const boost::system::error_code& _ec, socket_ptr _socket) {
		// 錯誤碼檢測
		if (_ec) {
			return;
		}
		// 列印當前連線進來的客戶端
		std::cout << "client: " << _socket->remote_endpoint().address() << std::endl;
		//開啟檔案
		if ((fp = fopen("/root/sbt_client.with-apt-boost", "rb")) == NULL)
		{
			printf("File open error.\n");
			exit(1);
		}
		// 非同步傳送檔案
		bzero(buff, sizeof(buff));
		while (!feof(fp)) {
			len = fread(buff, 1, sizeof(buff), fp);
			// 非同步傳送檔案單詞讀取buff到客戶端,傳送成功後,自動呼叫Server::write_handler函式
			_socket->async_write_some(boost::asio::buffer(buff, len),
				boost::bind(&Server::write_handler, this,
					boost::asio::placeholders::error/* 此處作為佔位符 */));	
		}
		// 關閉檔案讀取
		fclose(fp);
		// 非同步傳送 "hello CSND_Ayo" 訊息到客戶端,傳送成功後,自動呼叫Server::write_handler函式
		//_socket->async_write_some(boost::asio::buffer("hello CSND_Ayo"),
		//	boost::bind(&Server::write_handler, this,
		//		boost::asio::placeholders::error/* 此處作為佔位符 */));

		// 啟動新的非同步監聽
		start();
	}

	// 完成非同步寫操作後的處理器
	void write_handler(const boost::system::error_code& _ec) {
		std::cout << "server: send message complete." << std::endl;
	}

};



int main(void) {
	try {
		std::cout << "server started." << std::endl;
		// 建造服務物件
		boost::asio::io_service ios;
		// 構建Server例項
		Server server(ios);
		// 啟動非同步呼叫事件處理迴圈
		ios.run();
	}
	catch (std::exception& _e) {
		std::cout << _e.what() << std::endl;
	}
	std::cout << "server stopping." << std::endl;
	return 0;
}

相關文章