wxWidgets 从套接字读取
我在将从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 readBuffer 很可能不会以 null 终止。事实上,它可能只包含部分消息。
最简单的“修复”是确保它以 null 终止。
您可以使用 LastCount() 来确定实际读取的字节数。
然而,真正的解决方法是在服务器和客户端之间设置一个简单的协议,以便您可以确定何时收到整个消息,然后将其打印出来。
我假设您正在使用 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.
I am assuming you are using a unicode build. Remove the /2 if you aren't
如果挂起意味着崩溃,则传输字符串末尾可能会缺少
\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.