我寫了使用boost asio和協議緩衝區的簡單客戶端(C++)服務器(Python)通信。我來回傳遞數組。當我從服務器(python)傳遞數組到客戶端(C++)時,我的問題在C++輸出中,我的第一個數組只有大約50個元素。如何解決這個問題我必須通過10個數組10000個元素。客戶端服務器蟒蛇-C++
一塊代碼客戶端的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的服務器:
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()
'data = client.recv(50000)' - 你怎麼確定50000在這裏?足夠大嗎?我想你需要一些消息通信協議,發送方在固定長度的報頭中對消息的長度進行編碼。 –
在python方面它完美無缺,50000可以。但在C++客戶端上我寫了10000000(我多次改變了緩衝區的大小,但總是有相同的結果)。所以我認爲問題不在緩衝區 – user3735525