returnVoid

Hi all

I have a text to send over TCP socket:

Code Snippet

public Socket mySendSocket;

...................

byte[] byteData = System.Text.Encoding.ASCII.GetBytes("hello World");

mySendSocket.Send(byteData);

................................................

But my client programm require zero on the end of the socket string.

Haw can I do it:

Haw add 0 to the end off byte array

Is it possible to set Socket to send 0x00 on the end

Thanks in advance



Re: Visual C# General byte array

Adamus Turner

Code Snippet

Dim test() As Byte = System.Text.Encoding.ASCII.GetBytes("Hello World")

Dim test2(test.Length) As Byte

For x As Integer = 1 To test.Length - 1

test2(x) = test(x)

Next

test2(test.Length) = 0

MsgBox(test2(test.Length))

Adamus






Re: Visual C# General byte array

James Curran

Code Snippet

byte[] byteData = System.Text.Encoding.ASCII.GetBytes("hello World\0");

(In .Net, a string has a length. It's not terminated at a NUL character)






Re: Visual C# General byte array

returnVoid

Thanks for quick replay

Jeff give me an answer what I use:

int length = byteData.Length;
Array.Resize(ref byteData, length + 1); //the "ref" might not be necessary
byteData[length] = 0;





Re: Visual C# General byte array

IanG

James Curran's version is more efficient. It doesn't involve making a copy of the data.

Or you could just call Send a second time to send the extra byte...that's probably the simplest solution!