String concatenation issue

T

tcomer

Hello. I'm working on a client/server application and I'm having some
weird issues with concatenating strings. The client program sends some
text:

Object objData = "IssuedCommand:SenderName";
byte[] byData =
System.Text.Encoding.ASCII.GetBytes(objData.ToString());
if (m_clientSocket != null)
{
m_clientSocket.Send(byData);
}


The server receives the data, decodes it and converts it into a string
value:

int iBytes = socketData.m_currentSocket.EndReceive(async);
char[] chars = new char[iBytes + 1];
System.Text.Decoder decode = System.Text.Encoding.ASCII.GetDecoder();
int charLen = decode.GetChars(socketData.dataBuffer, 0, iBytes, chars,
0);

decode.GetChars(socketData.dataBuffer, 0, iBytes, chars, 0);
string szCommand = new string(chars);
string[] receivedData = szCommand.Split(':');

Now at this point, receivedData[0] holds a value of "IssuedCommand" and
receivedData[1] is "SenderName". The problem is that if I try to use
either receivedData value in a string, it will print the correct
receivedData value but it will not display any text that is
concatenated. For example:

MessageBox.Show(receivedData[1].ToString() + " is now online.");

or

rtboxMessage.Text += receivedData[1] + " is now online.";

displays "SenderName" without the appended text " is now online." I
suspect this is a bug as there are no errors that are thrown. I have a
feeling that it may be caused by the Split statement, but I haven't
been able to solve the problem yet. Please let me know if I need to
provide more information.
 
J

Jon Shemitz

tcomer said:
string[] receivedData = szCommand.Split(':');
MessageBox.Show(receivedData[1].ToString() + " is now online.");

or

rtboxMessage.Text += receivedData[1] + " is now online.";

displays "SenderName" without the appended text " is now online." I
suspect this is a bug as there are no errors that are thrown. I have a
feeling that it may be caused by the Split statement, but I haven't
been able to solve the problem yet. Please let me know if I need to
provide more information.

My guess would be that there are null characters in the stream you're
getting back from the server. Since .NET strings have a length field,
the null doesn't affect concatenation and the like - but the
MessageBox class wraps low-level Win32 fns that do use null-terminated
strings.

For example,

MessageBox.Show("Some\0text");

will only show "Some".
 
T

tcomer

My guess would be that there are null characters in the stream you're
getting back from the server. Since .NET strings have a length field,
the null doesn't affect concatenation and the like - but the
MessageBox class wraps low-level Win32 fns that do use null-terminated
strings.

Good call, I completely forgot about the null terminated strings. I
just added a null character to the Split and that fixed it. Thanks a
bunch!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top