Main Site Documentation

Help to send "\0" on a socket


#1

I am trying to send a null char (0x00) at the end of my string. like adding “\0” to the end. Using WireShark i can see that it is not doing this.

I have tried these two way to do this, but neither are working, any suggestions ?


const string policy = "secure=\"false\"/>\0";

byte[] reply = Encoding.UTF8.GetBytes(policy);
m_clientSocket.Send(reply);

i then tried this, though i would increase the buffer size by one.



byte[] reply = new byte[policy.Length + 1];

reply = Encoding.UTF8.GetBytes(policy);
m_clientSocket.Send(reply);


#2

This works, but i know its not doing it the correct way.



byte[] reply = new byte[policy.Length];
                                
reply = Encoding.UTF8.GetBytes(policy);

m_clientSocket.Send(reply, reply.Length, SocketFlags.None);

byte[] t = new byte[1];
t[0] = 0;

m_clientSocket.Send(t, 1, SocketFlags.None);



I have also tried this, but then i get a exception when it runs.



byte[] reply = new byte[policy.Length + 1];
                                
reply = Encoding.UTF8.GetBytes(policy);

m_clientSocket.Send(reply, reply.Length  + 1, SocketFlags.None);



#3

Can you do it like this?


const string policy = "secure=\"false\"/>0";
 
byte[] reply = Encoding.UTF8.GetBytes(policy);
reply[reply.Length - 1] = 0; // Overwrite last character '0' with '\0'
m_clientSocket.Send(reply); 


#4

that worked, thanks.

I was reading here: http://msdn.microsoft.com/en-us/library/ms228362.aspx about the /0

where it says

so out of curiosity, is there a bug somewhere why when i add it in my string it is not working right ?


#5

I full .NET a string can indeed contain \0 characters. But the implementation of NETMF is slightly different, where \0 means a terminator.


#6

Yes, I encountered this when working on a recent project. I was suprised to learn that NETMF does not support null characters in strings, especially since they seem to support unicode characters.

Here is a link I found about this subject: http://netmf.codeplex.com/workitem/945


#7

It is weird that some things i have finding out that .net seems to have .netmf does not. Early on i was told they are trying to save space. however not having the ability to us /0 is just absurd.