简体   繁体   English

客户端服务器python -C ++

[英]client server python -C++

I have written simple client(C++) server(Python) communication using boost asio and protocol buffer. 我已经使用boost asio和协议缓冲区编写了简单的客户端(C ++)服务器(Python)通信。 I pass array back and forth . 我来回传递数组。 My problem when I pass array from server ( python) to client (C++) I have only about 50 elements of my first array on the C++ output. 我将数组从服务器(python)传递到客户端(C ++)时遇到的问题是,我在C ++输出上的第一个数组只有大约50个元素。 How to solve this problem I have to pass 10 arrays with 10000 elements. 如何解决此问题,我必须传递带有10000个元素的10个数组。

piece of code client c++ reading data on client : 客户端c ++读取客户端上的数据的一段代码:

            boost::asio::transfer_exactly(65536) */);
    boost::asio::streambuf b;
    boost::asio::streambuf::mutable_buffers_type bufs = b.prepare(10000000);
    size_t n = socket.receive(bufs);

    b.commit(n);
    std::istream is(&b);
    std::string s;
    is >> s;


     object1.ParseFromString(s);
     std::cout << object1.DebugString();
            // this line doesn't output allarray's elemenents. 


}
catch (std::exception& e)
{
    //std::cerr << e.what(luuu) << std::endl;
}
std::cout << "\nClosing";
std::string dummy;
    }

Python server: Python服务器:

    import socket 
    from test_pb2 import Person
    import dataf_pb2

    host = 'localhost'
    port = 10000
    backlog = 55
    size = 1024 
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    s.bind((host,port))
    s.listen(backlog)

    element = dataf_pb2.ParticleMatrix()
    for i in range(1000):
    element.xPox.append(i * 0.53434)

    for i in range(1000):
    element.yPox.append( i * 0.53434)

    for i in range(1000):
    element.zPox.append(i * 0.53434)

    message= element.SerializeToString()
    element1 = dataf_pb2.ParticleMatrix()

    while 1: 
      client, address = s.accept()
      print ('Client connected')
      data = client.recv(50000)
      print data
      if data:
      object1 = dataf_pb2.ParticleMatrix()
      object1.ParseFromString(data)
      print object1.__str__()
      print object1.ListFields()
      client.send(message)
      client.close()

Have you attempted using sendall instead of just send? 您是否尝试过使用sendall而不是仅仅发送? I've been working on a similar client/server (in pure c++ though) and personally it looks like a buffer problem to me. 我一直在研究类似的客户端/服务器(虽然使用纯c ++),个人看来对我来说是一个缓冲区问题。

Another thing I would suggest is attempting to capture the loopback (if client/server are running from same machine) and looking at the packets to see what/how much data is actually getting sent. 我建议的另一件事是尝试捕获回送(如果客户端/服务器从同一台计算机运行),然后查看数据包以查看实际发送了多少数据。 Wireshark is a useful tool for this. Wireshark是一个有用的工具。

Also would be useful to add in some error handling on send/recv so you can check total bytes that are actually being sent if you don't want to do the packet capturing. 在send / recv上添加一些错误处理也很有用,因此如果您不想进行数据包捕获,则可以检查实际发送的总字节数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM