简体   繁体   中英

How can I send hex-code via TCP in C# language

I'm new to C# programming and I'm trying to write TCP client that will send hex code to the server via hex code read some metadata from server side.I managed to connect with the server but could not send hex code.Could you please look what I'm missing here.

using System;
using System.IO;
using System.Text;
using System.Net.Sockets;


public class clnt
{

public static void Main()
{

    try
    {
        TcpClient tcpclnt = new TcpClient();
        Console.WriteLine("Connecting...");

        tcpclnt.Connect("192.168.80.128", 557);
        // use the ip address as in the server program

        Console.WriteLine("Connected!");
        Console.WriteLine("Sending Hex Code...");


        string str = Console.ReadLine();
        Stream stm = tcpclnt.GetStream();


        ASCIIEncoding asen = new ASCIIEncoding();
        byte[] ba = asen.GetBytes(str);
        var data = new byte[] { 0xCF, 0xC4 };
        Console.WriteLine("Transmitting");

        stm.Write(ba, 0, ba.Length);

        byte[] bb = new byte[100];
        int k = stm.Read(bb, 0, 100);

        for (int i = 0; i < k; i++)
            Console.Write(Convert.ToChar(bb[i]));

        tcpclnt.Close();
    }

    catch (Exception e)
    {
        Console.WriteLine("Opps " + e.StackTrace);
    }
}

}


//C++ code
int main()
{
    client.Initialize("192.168.80.128", 557);

    std::this_thread::sleep_for(std::chrono::seconds(2));

    std::string xid = U8("NASA_Arnold_AFB_Airshow");

    std::vector<std::uint8_t> command = {0xCF, 0xC4};

    command.push_back(static_cast<std::uint8_t>(xid.length()));

    command.insert(command.end(), xid.begin(), xid.end());

    std::cout << command.size() << std::endl;

    client.Correspond(command, 10);

    std::this_thread::sleep_for(std::chrono::seconds(3));

    command.clear();

    command = {0xC8, 0xC3};

    std::cout << client.Response().size() << std::endl;

    std::vector<std::uint8_t> idhandle(client.Response().begin() + 2, client.Response().end());

    command.insert(command.end(), idhandle.begin(), idhandle.end());

    client.Correspond(command, 3);

    std::this_thread::sleep_for(std::chrono::seconds(3));

    for(auto element : client.Response())
        std::cout << static_cast<int>(element) << std::endl;

    std::uint8_t size = client.Response().back();

    std::cout << "size of xid:" << static_cast<int>(size) << std::endl;

    client.ReadN(size);

    std::this_thread::sleep_for(std::chrono::seconds(3));

    std::cout << std::string(client.Response().begin(), client.Response().end()) << std::endl;

    command.clear();

    command = {0xC8, 0x4A};

    command.insert(command.end(), idhandle.begin(), idhandle.end());

    std::cout << idhandle.size() << std::endl;

    for(auto element : idhandle)
        std::cout << static_cast<int>(element) << std::endl;

    client.Correspond(command, 97);

    std::this_thread::sleep_for(std::chrono::seconds(3));


    std::cout << std::string(client.Response().begin(), client.Response().end()) << std::endl;


    std::getchar();

    return 0;
}

If I understand correctly, your specific bytes are in array data . But you don't use it in your code.
It seems that you missed something like stm.Write(data, 0, data.Length) to send bytes 0xCF and 0xC4

Your code prompts for input:

string str = Console.ReadLine();

then gets the bytes from this input:

byte[] ba = asen.GetBytes(str);

and finally sends these bytes to the stream:

stm.Write(ba, 0, ba.Length);

Are you typing anything in at the prompt? If not, then there won't be any bytes to send. If instead you meant to send var data = new byte[] { 0xCF, 0xC4 }; then you need to use

stm.Write(data, 0, data.Length);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM