ratitamaldita
asked on
C# TCP SEND ASCII CHARACTERS
Windows service c#
Hi gurus, i have the next problem, my code need send a tcp string, the first 2 bytes of the string are the lenght of the string.
Example:
I have a string of length 690 = 02 b0 in hexadecimal, but the receptor receives 02 C2 B2, whereas it should receive 02 B2.
I have proven in UTF8 encoding and ASCII, but I'm not getting the desired result
Please help me to fix this issue
Thanks for all solutions
Hi gurus, i have the next problem, my code need send a tcp string, the first 2 bytes of the string are the lenght of the string.
Example:
I have a string of length 690 = 02 b0 in hexadecimal, but the receptor receives 02 C2 B2, whereas it should receive 02 B2.
I have proven in UTF8 encoding and ASCII, but I'm not getting the desired result
Please help me to fix this issue
Thanks for all solutions
// Get the string lenght
char[] lk = ToCharsBitShift(sXMLResponse.Length);
// attach the lenght to the string
sXMLResponse = lk[0].ToString() + lk[1].ToString() + sXMLResponse;
//Encode the string to bytes
Byte[] ClientResponse = Encoding.UTF8.GetBytes(sXMLResponse);
//Send the response to client
clientStream.Write(ClientResponse, 0, ClientResponse.Length);
clientStream.Flush()
public static char[] ToCharsBitShift(int x)
{
char[] chars = new char[2];
chars[1] = (char)(x & 0xFF);
chars[0] = (char)(x >> 8 & 0xFF);
return chars;
}
ASKER
hi kyleb84:
I use the next line to debug my code windows service.
EventLog.WriteEntry("DEBUG APP", "RESPONSE " + sXMLResponse);
the result of the debug is
°<?xml version="1.0" encoding="UTF-8"?>........
I use the next line to debug my code windows service.
EventLog.WriteEntry("DEBUG
the result of the debug is
°<?xml version="1.0" encoding="UTF-8"?>........
Try:
// Get the string lenght
byte[] lk = ToBytesBitShift(sXMLResponse.Length);
// DON't attach the lenght to the string
// sXMLResponse = lk[0].ToString() + lk[1].ToString() + sXMLResponse;
//Encode the string to bytes
Byte[] ClientResponse = Encoding.UTF8.GetBytes(sXMLResponse);
// send the length first
clientStream.WriteByte(lk[0]);
clientStream.WriteByte(lk[1]);
//Send the response to client
clientStream.Write(ClientResponse, 0, ClientResponse.Length);
clientStream.Flush()
public static byte[] ToBytesBitShift(int x)
{
byte[] bytes = new byte[2];
bytes[1] = (byte)(x & 0xFF);
bytes[0] = (byte)(x >> 8 & 0xFF);
return bytes;
}
Havn't tested it but it should send..
"02B0<?xml version="1.0" encoding="UTF-8"?>....."
"02B0<?xml version="1.0" encoding="UTF-8"?>....."
// Get the string lenght
char[] lk = ToCharsBitShift(sXMLResponse.Length);
// attach the lenght to the string
String size1;
String size2;
size1 = ((Integer)lk[0]).ToString("X");
if (size1.length == 1)
size1 = "0" + size;
size2 = ((Integer)lk[1]).ToString("X");
if (size2.length == 1)
size2 = "0" + size2;
sXMLResponse = size1 + size2 + sXMLResponse;
//Encode the string to bytes
Byte[] ClientResponse = Encoding.UTF8.GetBytes(sXMLResponse);
Just read the first 4 bytes in at the other end and:
int len = int.Parse(recvdata, System.Globalization.Numbe rStyles.He xNumber);
Where:
[len] will be the converted value of the hex string
[recvdata] is the data received from the TCP socket
int len = int.Parse(recvdata, System.Globalization.Numbe
Where:
[len] will be the converted value of the hex string
[recvdata] is the data received from the TCP socket
ASKER
kyleb84:
I need send ascii characters for the lenght 02 = B0=° , i donk make changes in the other side.
cheers
I need send ascii characters for the lenght 02 = B0=° , i donk make changes in the other side.
cheers
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
@ philipjonathan:
I try your code work perfect.
thanks, Cheers.
I try your code work perfect.
thanks, Cheers.
MessageBox.Show sXMLResponse
just before:
//Send the response to client
clientStream.Write(ClientR
clientStream.Flush()
And post what the first, say 8 characters are...