致谢没有成功.Teltonika FM5300

时间:2015-12-02 19:47:38

标签: c# sockets tcp gps

以下是我的代码示例。它不起作用。我以正确的形式{0x01}发送了确认,但设备始终只返回IMEI。有人能解决这个问题吗?

    static void Main(string[] args)
    {
        TcpListener list = new TcpListener(new IPEndPoint(IPAddress.Any, 2065));
        TcpClient client;

        Console.WriteLine("Listening... \n");

        list.Start(1);
        list.Server.NoDelay = true;

        while (true)
        {
            Console.WriteLine("Waiting for client...\n");
            client = list.AcceptTcpClient();
            Console.WriteLine("Client connected ");
            byte[] imei = new byte[8192];

            NetworkStream ns = client.GetStream();
            if (ns.CanRead)
            {
                ns.Read(imei, 0, (int)client.ReceiveBufferSize);
            }

            Console.WriteLine(Encoding.ASCII.GetString(imei, 0, imei.Length));
            byte[] ack = new byte[1] {0x01};
            if (ns.CanWrite)
            {
                ns.Write(ack, 0, ack.Length);
            }
            client.Close();
        }

    }

1 个答案:

答案 0 :(得分:0)

像罗恩说的那样,你一遍又一遍地发送同样的东西。收到IMEI后,您的代码会发送正确的响应并断开连接。所以接下来的连接从开始就开始了,IMEI ......来自设备的数据来自同一个连接,所以......为什么要断开连接? ;)                      - mlask