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

Editors IDEsC#Networking Protocols

Avatar of undefined
Last Comment
ratitamaldita

8/22/2022 - Mon
kyleb84

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...
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"?>........
philipjonathan

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
kyleb84

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

kyleb84

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

ratitamaldita

ASKER
kyleb84:

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


cheers
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
philipjonathan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ratitamaldita

ASKER
@ philipjonathan:

I try your code work perfect.

thanks, Cheers.