-2

I'm developing an developing a little software in c# that connect to a Linux server. the software send a HEX String and receive a HEX String Back. here is my code

static void Main(string[] args)
    {
        TcpClient tcpclient = new TcpClient();

        tcpclient.Connect("192.168.0.100",9010);

        string msg = Console.ReadLine();

        Stream stream = tcpclient.GetStream();
        ASCIIEncoding ascii = new ASCIIEncoding();
        byte[] enviar = ascii.GetBytes(msg);
        stream.Write(enviar, 0, enviar.Length);
        byte[] bit = new byte[255];
        int i = stream.Read(bit, 0,255);
        for (int a = 0; a < i; a++ ) {
            Console.Write(Convert.ToString(bit[0]));
        }
        tcpclient.Close();


    }

an example of a string is 0x30 0x30 0x30 0x30 0x30 0x30 0x30 0x34 0x03 0x30 0x30 0x30 and the response is this 30 30 30 30 30 30 33 33 60 60 60 60 FF FF FF FF FF FF FF FF FF FF FF FF FF FF 01 the deal is that a need to send a string to the server but don't get response, need to have a response like this

realterm

can you give a hand with the code please ?

  • 1
    You're not writing a hex string, you're writing a byte array. If you want to write a hex string like you say, you need to do something like `string.Join(" ", enviar.Select(b => string.Format("0x{0:X2}", b)));` That being said, writing a byte array makes way more sense to me from an efficiency standpoint than writing a hex string like you describe. – itsme86 Mar 02 '17 at 20:29
  • Back up a bit. You need a message plan. See this [explanation](http://stackoverflow.com/a/8028242/2226988). – Tom Blodget Mar 02 '17 at 23:32

1 Answers1

0

Not sure a) if it's a typo, and b) how you are determining you didn't get a reply
That said: IF you are saying it by the console output, examine this line:

Console.Write(Convert.ToString(bit[0]));

I believe it should be:

Console.Write(Convert.ToString(bit[a]));

(also - naming a byte "bit" is a bit misleading - no pun intended)

John
  • 182
  • 7