Skip to content
Advertisement

Send and receive Hex String from linux Server C#

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 ?

Advertisement

Answer

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)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement