Отправить сообщение ASCII - PullRequest
0 голосов
/ 03 февраля 2020

У меня есть машина "Sysmex XP-300", которая отправляет данные через TCP, мне нужно перехватить эти данные и ответить ASCII, чтобы убедиться, что они были получены. Проблема в том, что каждый раз, когда машина подключена, код будет отвечать «подключен» без остановки. Я также должен ответить с ASCII, что моя машина, кажется, не получает. Вот мой код:

class Program
{
    static string ack = char.ConvertFromUtf32(6);
    byte[] byData = System.Text.Encoding.ASCII.GetBytes(ack);
    const int PORT_NO = 3001;
    const string SERVER_IP = "127.0.0.1";

    static void Main(string[] args)
    {
        TcpServer tcpServer = new TcpServer(SERVER_IP, 3001);
        tcpServer.Connected += new TcpServer.TcpServerEventDlgt(OnConnected);
        tcpServer.StartListening();
        Console.WriteLine("Press ENTER to exit the server.");
        Console.ReadLine();
        tcpServer.StopListening();
    }

    static void OnConnected(object sender, TcpServerEventArgs e)
    {
        // Handle the connection here by starting your own worker thread
        // that handles communicating with the client.
        Console.WriteLine("Connected.");

        IPAddress localAdd = IPAddress.Parse(SERVER_IP);
        TcpListener listener = new TcpListener(localAdd, PORT_NO);

        TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
        NetworkStream nwStream = client.GetStream();
        byte[] bytesToSend = System.Text.Encoding.ASCII.GetBytes(ack);// ASCIIEncoding.ASCII.GetBytes(textToSend);

        //---send the text---
        Console.WriteLine("Sending : " );
        nwStream.Write(bytesToSend, 0, bytesToSend.Length);

    }
}
...