Link to home
Create AccountLog in
Avatar of ratitamaldita
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
// 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;
 }

Open in new window

Avatar of kyleb84
kyleb84
Flag of Australia image

Can you debug the code and put in:

MessageBox.Show sXMLResponse

just before:

//Send the response to client
clientStream.Write(ClientResponse, 0, ClientResponse.Length);
clientStream.Flush()

And post what the first, say 8 characters are...
Avatar of ratitamaldita
ratitamaldita

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"?>........
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;
 }

Open in new window

Havn't tested it but it should send..


"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);

Open in new window

Just read the first 4 bytes in at the other end and:

int len = int.Parse(recvdata, System.Globalization.NumberStyles.HexNumber);

Where:
[len] will be the converted value of the hex string
[recvdata] is the data received from the TCP socket

kyleb84:

I need send ascii characters for the lenght 02 = B0=° , i donk make changes in the other side.


cheers
ASKER CERTIFIED SOLUTION
Avatar of philipjonathan
philipjonathan
Flag of New Zealand image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
@ philipjonathan:

I try your code work perfect.

thanks, Cheers.