0
// Reads NetworkStream into a byte buffer.

NetworkStream ns;
System.Net.Sockets.TcpClient client = new TcpClient();

byte[] receiveBytes = new byte[client.ReceiveBufferSize];
ns.Read(receiveBytes, 0, (int)client.ReceiveBufferSize);
String returndata = Encoding.UTF8.GetString(receiveBytes);

I am successfully reading from a client and storing the result into a string called returndata. However, when I try to concatenate returndata with anything, no concatenation occurs. Ex: String.Concat(returndata, "test") returns returndata, as does returndata + "test".

Does anyone know why this is happening?

Edit: Steve W is correct; i found out later that returndata.Length was always returning 8192.

4
  • Would be better if you showed the actual code you are having trouble with. You've posted the code you've got working and neglected the code you haven't. Commented Feb 13, 2009 at 19:59
  • Can you include the code for the concatenation? Commented Feb 13, 2009 at 19:59
  • @tacogordito: Did you check the return value of ns.Read()? Commented Feb 13, 2009 at 20:02
  • Agree with Anthony and Zach: why would you leave out the code that is causing the problems! Commented Feb 13, 2009 at 20:13

5 Answers 5

5

Are you assigning it to a string or back to itself?

returndata = string.Concat(returndata, "test");
returndata += "test";
Sign up to request clarification or add additional context in comments.

1 Comment

Psychic debugging - gotta love it :)
5

To expand on jhunter's answer, the Concat method doesn't alter the contents of the original string variable, it just returns the concatenated result. If you want returndata to contain the result of the concatenation, you need to use returndata = string.Concat(returndata, "test").

2 Comments

Its not just the Concat method, NO method directly modifies strings in C#. Every string is a unique, immutable object. If you change a string, it makes a new copy.
That's true--bad choice of words on my part.
4

I believe the problem is related to not keeping track of the total number of bytes read. Your byte buffer, set to ReceiveBufferSize, is more than likely larger than the actual number of bytes read. By taking into account the actual bytes read, and then passing it to the Encoding.UTF8.GetString() method, you should get a valid string that can be concatenated. Here's an example:


NetworkStream ns;
System.Net.Sockets.TcpClient client = new TcpClient();

byte[] receiveBytes = new byte[client.ReceiveBufferSize];
int bytesRead = ns.Read(receiveBytes, 0, receiveBytes.Length);
String returndata = Encoding.UTF8.GetString(receiveBytes,0,bytesRead);
returndata = returndata.Trim(new char[] {'\0'});

Note also the other suggestion about reading a null terminator and including it in the bytes read also could be an issue. I've included a Trim of the string to make sure null terminators are removed.

Comments

3

I'll have a guess:

The received string includes a '\0' terminator. You ought to be able to check that with the debugger.

4 Comments

Excellent suggestion, considering the context. +1
Wrong. Only C strings are null terminated.
@Tanveer: Yes, so what if the sender is a C/C++ program, that includes the '\0' ? .NET doesn't need the EOS char but can handle it - with results like tacogordito describes.
.net strings actually include a nul terminator, it's just that (by virtue of the length value) it can also include nulls as well (though it will no longer function as expected when passed to old style C functions via marshalling)
2

Strings are immutable. this means that, when you add (concatenate) another string to your string, you'll receive a new string instance.
The original string itself won't be changed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.