簡體   English   中英

連接后增強從TCP服務器訪問TCP客戶端IP地址+端口

[英]Boost Accessing TCP client IP address + Port from TCP server after connection

我已經使用boost asio實現了tcp客戶端和tcp服務器。 代碼如下。 (test.cpp, test.hpp and makefile)

可執行文件可以按以下方式運行

(server side)

$ ./client_server 1 5005

(client side)

$ ./client_server 0 5005

tcp_server類在接受連接時應知道遠程端點(client_ip地址+客戶端端口地址)。 但是似乎沒有變量似乎將其存儲在服務器端。我想存儲客戶端端點信息。 我該怎么做呢?

TEST.CPP

#include "test.hpp"
#include <iostream>
#include <exception>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <algorithm>
#include <sstream>
#include <iomanip>

const int ARG_COUNT = 2;
const int MAX_PACKETS = 25;
const int LOWEST_PORT = 1024;
const int HIGHEST_PORT = 65000;

static char message_array[8192];

void gen_random_string(char *s, const int len)
{
    static const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";

    for (int i = 0; i < len; ++i) {
        s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }
    s[len] = 0;
}

class tcp_connection :public boost::enable_shared_from_this<tcp_connection>
{
public:
    typedef boost::shared_ptr<tcp_connection> pointer;

    static pointer create(boost::asio::io_service& io_service)
    {
        return pointer(new tcp_connection(io_service));
    }

    boost::asio::ip::tcp::tcp::socket& socket()
    {
        return socket_;
    }

    void start()
    {
        gen_random_string(message_array, 8192);
        std::string message(message_array);

        boost::asio::async_write(socket_, boost::asio::buffer(message),
                                 boost::bind(&tcp_connection::handle_write, shared_from_this(),
                                             boost::asio::placeholders::error,
                                             boost::asio::placeholders::bytes_transferred));
    }

private:
    tcp_connection(boost::asio::io_service& io_service)
        : socket_(io_service)
    {
    }

    void handle_write(const boost::system::error_code& /*error*/,
                      size_t /*bytes_transferred*/)
    {
    }

    boost::asio::ip::tcp::socket socket_;
    std::string message;
};

class tcp_server
{
public:
    tcp_server(boost::asio::io_service& io_service,int port_number)
        : acceptor_(io_service, boost::asio::ip::tcp::tcp::endpoint(boost::asio::ip::tcp::tcp::v4(), port_number))
    {
        std::cout << "TCP server listening on " << port_number << std::endl;
        start_accept();
    }

private:
    void start_accept()
    {
        tcp_connection::pointer new_connection =
            tcp_connection::create(acceptor_.get_io_service());

        acceptor_.async_accept(new_connection->socket(),
                               boost::bind(&tcp_server::handle_accept, this, new_connection,
                                           boost::asio::placeholders::error));
    }

    void handle_accept(tcp_connection::pointer new_connection,
                       const boost::system::error_code& error)
    {
        if (!error)
        {
            new_connection->start();
            start_accept();
        }
    }

    boost::asio::ip::tcp::tcp::acceptor acceptor_;
};


void runTCPServer ( CmdLineOpts input )
{
    try
    {
        boost::asio::io_service io_service;
        tcp_server server(io_service,input.port);
        io_service.run();
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
}

class tcp_client
{
public:
    tcp_client(
        boost::asio::io_service& io_service,
        const std::string& host,
        const std::string& port
    ) : io_service_(io_service), socket_(io_service, boost::asio::ip::tcp::tcp::endpoint(boost::asio::ip::tcp::tcp::v4(), 0))
    {
        boost::asio::ip::tcp::tcp::resolver resolver(io_service_);
        boost::asio::ip::tcp::tcp::resolver::query query(boost::asio::ip::tcp::tcp::v4(), host, port);
        boost::asio::ip::tcp::tcp::resolver::iterator iter = resolver.resolve(query);
        endpoint_ = *iter;
        boost::asio::connect(socket_, iter);
    }

    ~tcp_client()
    {
        socket_.close();
    }


    void recieve_from() {
        /*Initialize our endpoint*/
        size_t len = socket_.read_some(
                         boost::asio::buffer(recv_buf), error);

        if (error == boost::asio::error::eof)
            std::cout << "Connction close cleanly by peer" << std::cout;
        else if (error)
            throw boost::system::system_error(error); // Some other error.

        std::cout.write(recv_buf.data(), len) << std::endl;

    }

private:
    boost::asio::io_service& io_service_;
    boost::asio::ip::tcp::tcp::socket socket_;
    boost::asio::ip::tcp::tcp::endpoint endpoint_;
    boost::array<char, 2048> recv_buf;
    boost::asio::ip::tcp::endpoint sender_endpoint;
    boost::system::error_code error;

};

void runTCPClient(std::string portStr)
{
    try
    {
        boost::asio::io_service io_service;
        tcp_client client(io_service, "localhost", portStr);
        client.recieve_from();
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

}

void runClient( CmdLineOpts input )
{
    runTCPClient(input.portStr);

}

void runServer( CmdLineOpts input )
{
    runTCPServer(input);
}

/**
 * * Usage: client_server <protocol> <port> <num of packets>
 * */
bool cmdline_parse ( int argc, char *argv[], CmdLineOpts *input )
{
    bool result = true;
    if (argc - 1 == ARG_COUNT)
    {
        // arg 1: server or client
        int arg1 = std::stoi(argv[1]);
        if (arg1 == 0 || arg1 == 1)
        {
            input->servOrClient = arg1;
        }
        else
        {
            std::cout << "Invalid client server choice.\nUsage: client_server <client (0) or server(1)> <protocol> <port>" << std::endl;
            result = false;
        }
        // arg 3: port
        int arg2 = std::stoi(argv[2]);
        if (arg2 > LOWEST_PORT && arg2 < HIGHEST_PORT )
        {
            input->port = arg2;
            input->portStr = argv[2];
        }
        else
        {
            std::cout << "Invalid port, must be between " << LOWEST_PORT << " and " << HIGHEST_PORT << std::endl;
            std::cout << "Usage: client_server <client (0) or server(1)> <protocol> <port>" << std::endl;
            result = false;
        }

    }
    else
    {
        std::cout << "Usage: client_server <client (0) or server(1)> <protocol> <port>" << std::endl;
        result = false;
    }

    return result;
}



int main ( int argc, char *argv[] )
{
    CmdLineOpts input;
    if (cmdline_parse(argc, argv, &input))
    {
        if(input.servOrClient == 1)
        {
            runServer(input);
        }
        else if(input.servOrClient == 0)
        {
            runClient(input);
        }
    }
    else
    {
        return 1;
    }

    return 0;
}

test.hpp

#ifndef CLIENT_SERVER_H_INCLUDED                                                                                                                                                                                   
#define CLIENT_SERVER_H_INCLUDED

#include <string>

struct CmdLineOpts
{
    std::string portStr;
    int port;
    int servOrClient;
};

void runTCPServer ( CmdLineOpts input );

void runTCPClient ( std::string port_str);

bool cmdline_parse ( int argc, char *argv[], CmdLineOpts input );
#endif

Makefile文件

TARGET = client_server                                                                                                                                                                                             
LIBS = -lboost_system -lpthread
CXX = g++ 
CXXFLAGS = -std=c++11 -g -Wall -pedantic

.PHONY: default all clean

default: $(TARGET)
all: default

OBJECTS = $(patsubst %.cpp, %.o, $(wildcard *.cpp))
HEADERS = $(wildcard *.hpp)

%.o: %.cpp $(HEADERS)
  $(CXX) $(CXXFLAGS) -c $< -o $@

.PRECIOUS: $(TARGET) $(OBJECTS)

$(TARGET): $(OBJECTS)
  $(CXX) $(OBJECTS) $(LIBS) -o $@

clean:
      -rm -f *.o
      -rm -f $(TARGET)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM