1

我最近为有问题的服务器解决了这个问题,但事实证明服务器似乎没有回复客户端,甚至没有收到任何发送的数据。这就是我在客户端所做的。

C:\blocking_tcp_echo_client\bin\Debug>blocking_tcp_echo_client 127.0.0.1 4001
Enter message: hello
Exception: read: End of file

这是服务器端。我确实在启动客户端之前启动了服务器。我还用原始代码测试了客户端,它运行良好。

C:\boost_server_class\bin\Debug>boost_server_class
TCPIP_server:
Usage: blocking_tcp_echo_server

blocking_tcp_echo_client.cpp

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

enum { max_length = 1024 };

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 3)
    {
      std::cerr << "Usage: blocking_tcp_echo_client <host> <port>\n";
      return 1;
    }

    boost::asio::io_service io_service;

    tcp::resolver resolver(io_service);
    tcp::resolver::query query(tcp::v4(), argv[1], argv[2]);
    tcp::resolver::iterator iterator = resolver.resolve(query);

    tcp::socket s(io_service);
    s.connect(*iterator);

    using namespace std; // For strlen.
    std::cout << "Enter message: ";
    char request[max_length];
    std::cin.getline(request, max_length);
    size_t request_length = strlen(request);
    boost::asio::write(s, boost::asio::buffer(request, request_length));

    char reply[max_length];
    size_t reply_length = boost::asio::read(s,
        boost::asio::buffer(reply, request_length));
    std::cout << "Reply is: ";
    std::cout.write(reply, reply_length);
    std::cout << "\n";
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

boost_server_class.cpp

#include "TCPIP_server.h"
#include <iostream>

int main()
{
    std::cout<<"TCPIP_server:"<<std::endl;
    TCPIP_server server;

    server.main_server();

    return 0;
}

TCPIP_server.h

#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <cstdlib>
#include <iostream>

using boost::asio::ip::tcp;

const int max_length = 1024;

class TCPIP_server
{
        private:

            typedef boost::shared_ptr<tcp::socket> socket_ptr;
            char data[max_length];

        public:

        TCPIP_server(){}
        ~TCPIP_server(){}
        void session(socket_ptr sock)
        {
            std::cout<<"TCPIP_server session:"<<std::endl;
            try
            {
                for (;;)
                {
                    char data[max_length];

                    boost::system::error_code error;
                    size_t length = sock->read_some(boost::asio::buffer(data), error);
                    if (error == boost::asio::error::eof)
                        break; // Connection closed cleanly by peer.
                    else if (error)
                        throw boost::system::system_error(error); // Some other error.

                    boost::asio::write(*sock, boost::asio::buffer(data, length));
                }
            }
            catch (std::exception& e)
            {
                std::cerr << "Exception in thread: " << e.what() << "\n";
            }
        }
        void server(boost::asio::io_service& io_service, short port)
        {
            tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));

            for (;;)
            {
                socket_ptr sock(new tcp::socket(io_service));
                a.accept(*sock);
                boost::bind(&TCPIP_server::session, this, sock);
            }
        }
        void main_server()
        {
            try
            {
                std::cerr << "Usage: blocking_tcp_echo_server \n";
                boost::asio::io_service io_service;

                using namespace std; // For atoi.
                server(io_service, atoi("4001"));
            }
            catch (std::exception& e)
            {
                std::cerr << "Exception: " << e.what() << "\n";
            }
        }
};
4

1 回答 1

3

你的服务器坏了。

接受连接后,您将创建一个未命名的函子,绑定到TCPIP_server::session()该函子永远不会被调用。接受的套接字将立即超出范围,这将关闭它并且您的客户端会看到eof.

socket_ptr sock(new tcp::socket(io_service));
a.accept(*sock);
boost::bind(&TCPIP_server::session, this, sock); // <-- this does nothing

您可以通过删除仿函数来解决此问题,而是this->session( sock )在接受连接后调用。但是,我不建议使用这种方法,您的服务器是单线程的,因此在第一个连接后它将无法为其他客户端提供服务。相反,我建议遵循异步回显服务器示例,并使用单个线程、单个io_service和异步方法重写您的服务器。

于 2013-04-04T03:18:09.380 回答