【问题标题】:Error translating code from Python to C ++将代码从 Python 转换为 C++ 时出错
【发布时间】:2020-09-14 18:51:08
【问题描述】:

我尝试将这段代码从 Python 转换为 C++:

import socket

host, port, length = '127.0.0.1', 3000, 8

sock = socket.create_connection((host, port))
sock.send(b'\x02\x01\x00\x00\x00\x00\x006build\nedition\nnode\nservice\nservices\nstatistics\nversion')
buf = bytearray(length)
buf = bytearray(length)
view = memoryview(buf)
while length > 0:
    nbytes = sock.recv_into(view, length)
    view = view[nbytes:]
    length -= nbytes
print(buf)

使用boost::asio::ip::tcp::socket。下面是负责发送请求和接收响应的代码部分:

boost::asio::io_service service;
boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 3000);
boost::asio::ip::tcp::socket sock(service);
sock.open(boost::asio::ip::tcp::v4());
sock.connect(ep);
sock.write_some(boost::asio::buffer("\x02\x01\x00\x00\x00\x00\x006build\nedition\nnode\nservice\nservices\nstatistics\nversion"));
char buff[1024];
sock.read_some(boost::asio::buffer(buff,1024));
sock.shutdown(boost::asio::ip::tcp::socket::shutdown_receive);
sock.close();
std::cout << buff;

但是,此代码在收到响应时冻结(在执行 sock.read_some() 时)。这里可以更改/纠正什么?

【问题讨论】:

  • 您的意思是sock.read_some() 冻结?服务器在发送数据吗?你验证了吗?
  • @Torxed,是的,Python 代码有效。
  • 我对 Python 代码并不感兴趣,您是否调试过(tcpdump、wireshark 等)以确保数据实际命中您的 C++ 代码? :)

标签: python c++ sockets


【解决方案1】:

我编译了你的 C++ 客户端,只是稍微改动了一下。

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

int main(int, char**) {
    boost::asio::io_service service;
    boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 3000);
    boost::asio::ip::tcp::socket sock(service);
    sock.open(boost::asio::ip::tcp::v4());
    sock.connect(ep);
    sock.write_some(boost::asio::buffer("the test message"));
    char buff[1024];
    auto rcvd_bytes = sock.read_some(boost::asio::buffer(buff,1024));
    std::cout << "rcvd_bytes:" << rcvd_bytes << '\n';
    sock.shutdown(boost::asio::ip::tcp::socket::shutdown_receive);
    sock.close();
    std::cout << buff << '\n';
}

我收到socketserver.TCPServer Example

import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):
    """
    The RequestHandler class for our server.

    It is instantiated once per connection to the server, and must
    override the handle() method to implement communication to the
    client.
    """

    def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        print("{} wrote:".format(self.client_address[0]))
        print(self.data)
        # just send back the same data, but upper-cased
        self.request.sendall(self.data.upper())

if __name__ == "__main__":
    HOST, PORT = "localhost", 3000

    # Create the server, binding to localhost on port 9999
    server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)

    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()

我启动了这两个应用程序,一切似乎都正常。

服务器

127.0.0.1 wrote:
b'the test message\x00'

客户

rcvd_bytes:17
THE TEST MESSAGE

如果没有真正的服务器代码和相应的环境,恐怕很难理解在你的情况下什么不起作用。

【讨论】:

  • Aerospike 服务器(社区版)。我正在用 C++ 编写自己的客户端。
  • 好的,明白了。还有一个问题。如果我正确理解 Python 代码,它会准确读取 8 个字节、一定数量的数据,但在 C++ 中,您使用的是read_some,而不是read。您是否尝试过使用 read 函数并准确读取 8 个字节?字面上和python代码中的一样
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 2019-12-22
  • 2017-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多