嗨,我正试图让我的C ++客户端向我的python服务器发送一条消息。我的C ++客户端连接到服务器,但不发送消息并退出连接。我的问题是我在做什么错,我按照boost :: asio指南进行操作,它们最终要么确认我的代码可以工作,要么仍然是旧文档,仍然使用io_service而不是io_context。然后,我尝试使用https://www.boost.org/doc/libs/1_73_0/libs/beast/example/websocket/client/sync/websocket_client_sync.cpp上的示例重新开始客户端 但我仍然得到相同的结果。
python服务器:
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#clients = [] #clients connected to
#clients_lock = threading.Lock()
# Bind the socket to the port
server_name = sys.argv[1]
server_address = (server_name, 10000)
print('starting up on %s port %s' % server_address, file = sys.stderr)
sock.bind(server_address)
# Listen for incoming connections
sock.listen(5)
while True:
# Wait for a connection
print('waiting for a connection', file = sys.stderr)
connection, client_address = sock.accept()
try:
print('client connected:', client_address, file = sys.stderr)
while True:
data = connection.recv(1024)
if data.decode() == 'exit':
print(f"The client said: {data.decode()}")
reply = f"You told me: {data.decode()}"
connection.sendall(reply.encode('utf-8'))
else:
break
except KeyboardInterrupt:
print(f"Gracefully shutting down the server!")
except Exception as e:
print(f"Well I did not anticipate this: {e}")
c ++客户端:
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
bool ClientToSocket::Connect()
{
try
{
// Check command line arguments.
std::string text = "hi";
std::string host = "localhost";
char const* port = "10000";
// The io_context is required for all I/O
net::io_context ioc;
// These objects perform our I/O
tcp::resolver resolver{ ioc };
websocket::stream<tcp::socket> ws{ ioc };
// Look up the domain name
auto const results = resolver.resolve(host, port);
// Make the connection on the IP address we get from a lookup
auto ep = net::connect(ws.next_layer(), results);
// Update the host_ string. This will provide the value of the
// Host HTTP header during the WebSocket handshake.
// See https://tools.ietf.org/html/rfc7230#section-5.4
host += ':' + std::to_string(ep.port());
// Set a decorator to change the User-Agent of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::request_type& req)
{
req.set(http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-client-coro");
}));
// Perform the websocket handshake
ws.handshake(host, "/");
// Send the message
ws.write(net::buffer(std::string(text)));
// This buffer will hold the incoming message
beast::flat_buffer buffer;
// Read a message into our buffer
ws.read(buffer);
// Close the WebSocket connection
ws.close(websocket::close_code::normal);
// If we get here then the connection is closed gracefully
// The make_printable() function helps print a ConstBufferSequence
std::cout << beast::make_printable(buffer.data()) << std::endl;
}
catch (boost::system::system_error& error) {
std::cerr << "Error: " << error.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
End Result:
I need the client to send a message to the server and then another client to read the same message and to some calculations with it.