gpt4 book ai didi

c++ - 从python3客户端向cpp服务器发送垃圾

转载 作者:行者123 更新时间:2023-12-03 07:23:30 24 4
gpt4 key购买 nike

我有一个CPP服务器和python3客户端,但是当我发送数据时,我得到的是垃圾值(错误的数字和字符串中间的\ 0)
我尝试使用this answerC++ TCP socket garbage value

客户端代码(python3):

MESSAGE = "HELLO WORLD"
def send_via_socket(message):
print(len(message))
s.send(bytes([len(message)]))
data = pack('<Q%ds' % len(message), len(message), message.encode())
s.send(data)
return True

SERVER_IP = "127.0.0.1"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Create a socket object
ans = s.connect((SERVER_IP, PORT))
if ans is None:
write_to_log('connection to server establish')
send_via_socket(MESSAGE)
else:
write_to_log('connection failed')
message = s.recv(1024)
write_to_log(str(message, 'utf-8'))
res = res
except socket.error as err:
message = f"socket creation failed with error %s" % err
write_to_log(message)
return res

和服务器中的读取功能(cpp)
int Server::readString(int fd, std::string &str)
{
size_t len;
char *buf;

auto bytesReceived =read(fd, &len, sizeof(len));
if(bytesReceived == -1)
{
return bytesReceived;
}
buf = new char[len];
bytesReceived = read(fd, buf, len);
if(bytesReceived == -1)
{
return bytesReceived;
}

str.assign(buf, len);
delete []buf;
return bytesReceived;
}

我也有CPP客户,并且工作正常

最佳答案

我最后发现的答案是使用Oleguer Canal functions

它解决了所有垃圾问题

关于c++ - 从python3客户端向cpp服务器发送垃圾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59520147/

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