0

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;
}
4
  • You want to send what? You mean that you want to send 0xAB and receive literaly "0xAB" or what? Because .. if you think about it ( 0 == 0x00 == null and ' ' == 32 == 0x20 ) so everything is "hex" Commented Dec 12, 2016 at 13:13
  • When you say "could not send hex code", could you clarify what you mean? Did you get an error messages, if so what were they? Commented Dec 12, 2016 at 13:13
  • I notice the code prompts for input string str = Console.ReadLine();, then gets the bytes 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. Commented Dec 12, 2016 at 13:22
  • Hi,Actually i'm trying to porting C++ code to C#.Let me write sample c++ code to the questions maybe this helps. Commented Dec 13, 2016 at 6:23

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Yes.I'm writing something but i want to do that autmatically.Yes i meant to send var data = new byte[] { 0xCF, 0xC4 }; and read some byte after that and send some different byte as well i wrote this in c++ i updated quetions with code. thanks in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.