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

标签 python c++ sockets boost-asio

我是 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/

相关文章:

python - 在返回向量的函数上使用 Numpy Vectorize

python - 来自 sys.getrefcount 的意外值

c++ - c++11 编译器中出现额外限定错误的问题

c++ - 适用于 Linux(和 Windows)的异步 C++ 通信库

c++ - boost socket 。格式化/重新安装 ubuntu 后,工作代码不再工作。权限问题?

c# - 套接字监听器同时进行多个 AcceptAsync 调用

python - 将 main() 函数中的用户 input() 传递到下一个函数中的字符串

python - HTTPS代理服务器python

c++ - 覆盖中的几个基本问​​题

c++ - 格式化输出c++的问题