wxWidgets 从套接字读取

发布于 2024-12-12 07:27:39 字数 369 浏览 0 评论 0原文

我在将从 wxWidgets 中的套接字读取的内容转换为 wxString 时遇到问题。我这样做是这样的:

wxChar * readBuffer = new wxChar[256];
wxSocketClient * connection = new wxSocketClient();
connection->Connect(addr, true);
connection->Read(readBuffer, 256);
wxString wasRead(readBuffer);
std::cout << wasRead.mb_string() << std::endl;

它一直挂在打印字符串上,有更好的方法吗?

I am having problems converting what is being read from a socket in wxWidgets to a wxString. I am doing like so:

wxChar * readBuffer = new wxChar[256];
wxSocketClient * connection = new wxSocketClient();
connection->Connect(addr, true);
connection->Read(readBuffer, 256);
wxString wasRead(readBuffer);
std::cout << wasRead.mb_string() << std::endl;

It keeps hanging up on printing the string, is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

信愁 2024-12-19 07:27:39

您的 readBuffer 很可能不会以 null 终止。事实上,它可能只包含部分消息。

最简单的“修复”是确保它以 null 终止。

您可以使用 LastCount() 来确定实际读取的字节数。

然而,真正的解决方法是在服务器和客户端之间设置一个简单的协议,以便您可以确定何时收到整个消息,然后将其打印出来。

connection->Read(readBuffer, 255);       // leave room for null terminator
readBuffer[connection->LastCount()/2] = L'\0';      // ensure null terminated

我假设您正在使用 unicode 构建。如果不需要,请删除 /2

Your readBuffer might well not be null terminated. In fact, it might contain only a partial message.

The simplest 'fix' is to ensure that it is null terminated

You can use LastCount() to determine number of bytes actually read.

However, the real fix is to set up a simple protocol between your server and client, so that you can determine when the entire message has been received and only then print it out.

connection->Read(readBuffer, 255);       // leave room for null terminator
readBuffer[connection->LastCount()/2] = L'\0';      // ensure null terminated

I am assuming you are using a unicode build. Remove the /2 if you aren't

少女情怀诗 2024-12-19 07:27:39

如果挂起意味着崩溃,则传输字符串末尾可能会缺少 \0。据我所知,wxString(wxChar*) 将采用以 null 结尾的字符串。

If by hanging up you mean crashes, you might be missing a \0 at the end of your transmitted string. From what I know, wxString(wxChar*) would take a null-terminated string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文