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;
}
0xABand receive literaly"0xAB"or what? Because .. if you think about it (0 == 0x00 == nulland' ' == 32 == 0x20) so everything is "hex"string str = Console.ReadLine();, then gets the bytesbyte[] 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.