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