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/

相关文章:

sockets - 从 UDP 套接字读取应该使用多大的缓冲区?

python - 在 pandas 中创建质量分数列

python - 如何从单个查询中检查多个日期的冲突

python - ForeignKey 小部件的 Django 可搜索下拉菜单

c++ - 无法将 std::packaged_task 移动到 lambda 中

c++ - 为什么 C++ 支持带实现的纯虚函数?

C++:不确定代码是否是多线程的

java - 一个应用程序中的服务器和 ServerSocket : not working

python - 在python中获取日期时间错误

c++ - 使用 BOOST ASIO 缓冲器