C++ 服务器无法通过套接字从 python 客户端读取我的消息

编程入门 行业动态 更新时间:2024-10-27 20:30:50
本文介绍了C++ 服务器无法通过套接字从 python 客户端读取我的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我写了一个 python 客户端脚本,通过 tcp 向服务器发送消息.

I have wrote a python client script to send message to the server via tcp.

import socket

TCP_IP = '127.0.0.1'
TCP_PORT = 12003
BUFFER_SIZE = 1024
MESSAGE = b"Hello, World!"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(MESSAGE)
data = s.recv(BUFFER_SIZE)
s.close()

print "received data:", data

服务器可以接收我的数据,但似乎无法读取数据.日志信息复制如下.

The server can receive my data, but it seems it cannot read the data. The log info is copied in following.

I0328 21:45:24.493249   505 prset04.cpp:210] reading the message (3472609771221168177 bytes)
E0328 21:45:25.493088   505 prset04.cpp:162] unable to receive on socket
I0328 21:45:25.493285   505 prset04.cpp:215] echoing the message
E0328 21:45:25.493479   505 prset04.cpp:185] unable to send on socket

我认为 C++ 套接字模块和 python 之间可能有些不同.但我不知道那里出了什么问题,因为我不熟悉 C++ 和套接字.

What I think is there may be something different between C++ socket module and python's. But I don't know what goes wrong there since I am not familiar with C++ and socket.

任何想法或解释都会非常有帮助!

Any ideas or explanation will be greatly helpful!

谢谢

服务器代码:

static void OnClient(const int sk) {
    size_t len;
    char buf[1024];

    // Read the message length.
    if (!ReadBytes(sk, reinterpret_cast<char*>(&len), sizeof(len))) {
        LOG(ERROR) << "unable to read message length";
        return;
    }

    LOG(INFO) << "reading the message (" << len << " bytes" << ")";

    // Read the message.
    ReadBytes(sk, buf, len);

    LOG(INFO) << "echoing the message";

    // Echo the message.
    WriteBytes(sk, buf, len);
}

static bool ReadBytes(const int sk, char* buf, const size_t n) {
    char* ptr = buf;
    while (ptr < buf + n) {
        if (!SetReadTimeout(sk)) {
            return false;
        }

        auto ret = recv(sk, ptr, ptr - buf + n, 0);
        if (ret <= 0) {
            LOG(ERROR) << "unable to receive on socket";
            return false;
        }

        ptr += ret;
    }

    return true;
}

推荐答案

就像 Galik 已经说过的那样,Python 不会像您的程序所期望的那样在字符串之前发送字符串的长度.而 C/C++ 也不会这样做.

Like Galik already said, Python doesn't send the length of the string before the string, like your programm expects it. And C/C++ doesn't do this either.

如果您希望 Python 程序与 C 程序一起工作,则必须考虑以下几点:

If you want your Python program to work with your C programm, there are several things you have to consider:

使用 struct 模块打包您的前面有所需长度的字符串在打包字符串时使用网络字节顺序您应该知道像 size_t 这样的声明依赖于体系结构,并且在 64 位和 32 位体系结构之间有所不同.如果您希望它们在架构之间兼容,您可以使用固定大小的整数,例如 uint32_t. Use the struct module to pack your string with the required length before it Use the network byte order when packing your string You should be aware that declarations like size_t are architecture dependant and vary between 64 and 32 bit architectures. You can use fixed size integers like uint32_t if you want them to be compatible between architectures.

示例代码:

from struct import pack
message = "Hello World"
data = pack('!i%ds' % len(message), len(message), message))

如果您无法更改 C 代码以使用网络字节顺序,您的具体代码应如下所示:

If you can't change the C-Code to use the network byte order, your specific code should look like this:

from struct import pack
message = "Hello World"
data = pack('<Q%ds' % len(message), len(message), message))

< = 使用小端字节序

Q = 使用无符号 64 位整数(sizet_t 在 64Bit 架构上)

Q = Use an unsigned 64 bit integer (sizet_t on 64Bit architectures)

这篇关于C++ 服务器无法通过套接字从 python 客户端读取我的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 08:18:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1406719.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   消息   服务器   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!