Reputation: 2934
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?
Upvotes: 0
Views: 932
Reputation: 20576
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
Upvotes: 1
Reputation: 18633
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.
Upvotes: 0