Entire string lost when removing null characters

  • Thread starter Thread starter tcomer
  • Start date Start date
T

tcomer

Hello,

The application that I'm working on sends a request to a server, and
then receives the response via a udp configured socket. It queries a
game server, so I don't have any control over the format or length of
the data received. Everything works as it should, except for handling
the received response. Heres what I have:

1) sock.SendTo(sendBytes, 0, sendBytes.Length, SocketFlags.None, iep);
2) sock.BeginReceive(recBytes, 0, recBytes.Length, SocketFlags.None,
OnBeginReceive, null); // recBytes[4096]
3) string temp = System.Text.Encoding.Default.GetString(recBytes, 0,
recBytes.Length);
4) string results = temp.Replace('\0', '\\');

At line 3, the contents of the temp string is:

"ÿÿinfoResponse\0\a\0\0\0\0\0\0\0\n\0l\0\0si_name\0^8Game Server
Name\0net_serverPunkbusterEnabled\01\0net_serverDedicated
\01\0si_version\0ETQW 1.0.10826.32242 win-x86 Sep 12 2007
20:03:01\0si_gameReviewReadyWait\00\0si_disableGlobalChat
\00\0si_noProficiency\00\0si_allowLateJoin\01\0si_minPlayers
\02\0si_readyPercent\051\0si_disableVoting\00\0si_adminStart
\00\0si_motd_3\0^7#executiveorder on irc.GameSurge.net
\0si_motd_2\0^7http://www.executive-order.net\0si_motd_1\0^7Official
^<CAL ^N6v6 ^7Match Server\0si_irc\0#executiveorder\0si_email
\[email protected]\0si_adminname\0wh0racle.exe\0si_website
\0http://www.executive-order.net\0si_teamForceBalance\01\0si_timelimit
\020\0si_rules\0sdGameRulesStopWatch\0si_spectators\01\0si_pure
\01\0si_needPass\00\0si_teamDamage\01\0si_privateClients
\02\0si_maxPlayers\032\0si_antiLagForgiving\00\0si_antiLagOnly
\00\0si_antiLag\01\0bot_enable\01\0gamename\0baseETQW-1\0si_map\0maps/
area22.entities\0\0\0 
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" ...continues
to null about 200-300 more times"

.... which is ridiculous in the first place. But at line 4, the
contents of results is nothing except "\\\\\\\\\\\\\\\\\\...etc".
II've tried using Replace, Split, Remove.. none of it works. Any hints
as to why the operations fail?
 
tcomer said:
[...]
.... which is ridiculous in the first place. But at line 4, the
contents of results is nothing except "\\\\\\\\\\\\\\\\\\...etc".
II've tried using Replace, Split, Remove.. none of it works. Any hints
as to why the operations fail?

I don't know. Seems like Replace would do what you seem to be trying to
do (though why the backlash is a suitable replacement for a null
character, I'm not sure).

But, why is it ridiculous for there to be null characters in the data?
Do you have any reason to believe that this application protocol is
string-only? The data you posted contains things that, at least to me,
look like it may be intended to be interpreted as a struct of some sort,
with character arrays embedded. Perhaps a more appropriate solution
would be to interpret it that way instead.

Is there some documentation for this protocol that you can share, that
would provide some insight into the larger issue you're dealing with?

Pete
 
tcomer said:
The application that I'm working on sends a request to a server, and
then receives the response via a udp configured socket. It queries a
game server, so I don't have any control over the format or length of
the data received. Everything works as it should, except for handling
the received response. Heres what I have:

1) sock.SendTo(sendBytes, 0, sendBytes.Length, SocketFlags.None, iep);
2) sock.BeginReceive(recBytes, 0, recBytes.Length, SocketFlags.None,
OnBeginReceive, null); // recBytes[4096]
3) string temp = System.Text.Encoding.Default.GetString(recBytes, 0,
recBytes.Length);

It sounds unlikely that you should be using Encoding.Default here. Does
the format specify the encoding of the data?
4) string results = temp.Replace('\0', '\\');

At line 3, the contents of the temp string is:

...continues
to null about 200-300 more times"

... which is ridiculous in the first place.

Why do you think it's ridiculous? What did you expect to get? Note that
you're currently decoding the whole byte array, even if you only
received a few bytes.
But at line 4, the
contents of results is nothing except "\\\\\\\\\\\\\\\\\\...etc".
II've tried using Replace, Split, Remove.. none of it works. Any hints
as to why the operations fail?

That sounds very unlikely to me. I've never seen string.Replace fail in
that way.

Can you create a short but complete program which demonstrates the
problem? If you just hard-code the binary data received, you should
still be able to show the problem.
 
That sounds very unlikely to me. I've never seen string.Replace fail in
that way.

Can you create a short but complete program which demonstrates the
problem? If you just hard-code the binary data received, you should
still be able to show the problem.

Definitely, I will do that later today when I get some time. I've
never seen if fail either, but for whatever reason it's finding the
entire string to be '\0' if I use Replace. As far as the encoding
goes, there is no official documentation regarding sending commands or
requests to the game servers, so I'm kind of on my own with that.
Default seems to be the only format that allows me to view readable
data.
 
Definitely, I will do that later today when I get some time. I've
never seen if fail either, but for whatever reason it's finding the
entire string to be '\0' if I use Replace. As far as the encoding
goes, there is no official documentation regarding sending commands or
requests to the game servers, so I'm kind of on my own with that.
Default seems to be the only format that allows me to view readable
data.

Ok, I don't exactly know why.. but it seems to be working fine now? I
haven't tried a Split quite yet, but atleast the Replace seems to be
doing what it's supposed to do. Thanks for the help guys, I'm lost for
words as to why it failed "temporarily".?!
 
Ok, I don't exactly know why.. but it seems to be working fine now?
I haven't tried a Split quite yet, but atleast the Replace seems to
be doing what it's supposed to do. Thanks for the help guys, I'm lost
forwords as to why it failed "temporarily".?!

Well, when that sort of thing happens to me it's practically always
user error. :) That is, the code worked fine, it's just that I made
a mistake when trying to observe what it was doing.

Pete
 

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

Similar Threads


Back
Top