socket transmission problem

  • Thread starter Thread starter Dirk Reske
  • Start date Start date
D

Dirk Reske

Hello,

when I send this bytes B4 43 71 40 A0 81 F3 32 C0 80 using socket.Send()
the receiver receives this bytes 3F 43 71 40 3F 3F 3F 32 3F 3F
why?????
 
the byte array is the result of a calculation.
then I send it through socket.Send(byte[])
the receiver receive it by using socket.receive()

but the received bytes are others then the sent!
 
Sending:

string Key = Authorization.GenerateKey(Lock);
Console.WriteLine(GetHex(Key)); // *1
byte[] send = Encoding.ASCII.GetBytes(Key);
this.socket.Send(send);

receiving:

while ((len = socket.Receive(input)) > 0)
{
foreach (byte bt in input)
{
if (bt != 0)
{
Console.Write(bt.ToString("X2") + " "); //Here I get other
Hex values than in *1
}
}
 
Do us and yourself a favor, check the bytes you are sending - so after
Encoding - , you will see they are the same as the receiving bytes.
Then start thinking what you have done to your original bytes.

Willy.

Dirk Reske said:
Sending:

string Key = Authorization.GenerateKey(Lock);
Console.WriteLine(GetHex(Key)); // *1
byte[] send = Encoding.ASCII.GetBytes(Key);
this.socket.Send(send);

receiving:

while ((len = socket.Receive(input)) > 0)
{
foreach (byte bt in input)
{
if (bt != 0)
{
Console.Write(bt.ToString("X2") + " "); //Here I get other
Hex values than in *1
}
}

Joep said:
... since if your code is okay sending 1 implies receiving 1, hence show
the code...
 
sorry....you're all right!!

but why does my function

public static byte[] ToByteArray(string str)
{
byte[] arr = new byte[str.Length];
for (int i = 0; i < arr.Length; i++)
arr = (byte)str;
return arr;
}

return another array then
System.Text.Encoding.ASCII.GetBytes(str) ??

Willy Denoyette said:
Do us and yourself a favor, check the bytes you are sending - so after
Encoding - , you will see they are the same as the receiving bytes.
Then start thinking what you have done to your original bytes.

Willy.

Dirk Reske said:
Sending:

string Key = Authorization.GenerateKey(Lock);
Console.WriteLine(GetHex(Key)); // *1
byte[] send = Encoding.ASCII.GetBytes(Key);
this.socket.Send(send);

receiving:

while ((len = socket.Receive(input)) > 0)
{
foreach (byte bt in input)
{
if (bt != 0)
{
Console.Write(bt.ToString("X2") + " "); //Here I get other
Hex values than in *1
}
}

Joep said:
... since if your code is okay sending 1 implies receiving 1, hence show
the code...
 
Dirk Reske said:
sorry....you're all right!!

but why does my function

public static byte[] ToByteArray(string str)
{
byte[] arr = new byte[str.Length];
for (int i = 0; i < arr.Length; i++)
arr = (byte)str;
return arr;
}

return another array then
System.Text.Encoding.ASCII.GetBytes(str) ??


Because your function truncates the unicode character to lower 8-bits,
whereas the ASCII encoding is defined as 7-bit, and all characters out of
the range 0..127 are converted to '?' (which is the 0x3F you have
encountered at the beginning).

It's generally not a good idea to use strings as a storage for binary data,
unless you really know what you're doing. I suggest you rewrite the
Authorization.GenerateKey() function to return byte[] instead of string.

HTH,
Stefan
 
yes, I know, but its a text based protocol.
I have to send "$Key <key>"....

©tefan ©imek said:
Dirk Reske said:
sorry....you're all right!!

but why does my function

public static byte[] ToByteArray(string str)
{
byte[] arr = new byte[str.Length];
for (int i = 0; i < arr.Length; i++)
arr = (byte)str;
return arr;
}

return another array then
System.Text.Encoding.ASCII.GetBytes(str) ??


Because your function truncates the unicode character to lower 8-bits,
whereas the ASCII encoding is defined as 7-bit, and all characters out of
the range 0..127 are converted to '?' (which is the 0x3F you have
encountered at the beginning).

It's generally not a good idea to use strings as a storage for binary
data, unless you really know what you're doing. I suggest you rewrite the
Authorization.GenerateKey() function to return byte[] instead of string.

HTH,
Stefan
 
Back
Top