gpt4 book ai didi

python - 使用 boost-asio 的 C++ 和 Python 程序之间的客户端服务器

转载 作者:行者123 更新时间:2023-11-30 04:03:50 25 4
gpt4 key购买 nike

我是 C++ 初学者。请给我建议并说出我做错了什么。我必须创建客户端服务器通信 Python 客户端、C++ 服务器。我在 C++ 上创建了服务器,它可以工作,但我只能使用 telnet 与它通信。我的代码:

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

int main()
{
const int SERVER_PORT = 50013;

try {
boost::asio::io_service io_service;
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(),SERVER_PORT);
boost::asio::ip::tcp::acceptor acceptor(io_service, endpoint);
boost::asio::ip::tcp::socket socket(io_service);

std::cout << "Server ready" << std::endl;
{
acceptor.accept(socket);
int foo [5] = { 16, 2, 77, 40, 12071 };
boost::asio::write(socket, boost::asio::buffer(foo));
socket.close();
}
}
catch(std::exception& ex)
{
std::cerr << "Exception: " << ex.what() <<std::endl;
}
return 0;
}

我想通过客户端 Python 与它通信:

#!/usr/bin/python           # This is client.py file

import socket # Import socket module

s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 50013 # Reserve a port for your service.

s.connect((host, port))
print s.recv(1024)
s.close() # Close the socket when done

但是我的客户端没有任何结果

最佳答案

But I don't have any result on my client

你有,但你解释错了:

s.connect((host, port))
print binascii.hexlify(s.recv(1024))

结果:

10000000020000004d00000028000000272f0000

关于python - 使用 boost-asio 的 C++ 和 Python 程序之间的客户端服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24149741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com