【问题标题】:TCP IP Server with acknowledgement and receive data againTCP IP Server 确认并再次接收数据
【发布时间】:2013-06-20 16:32:32
【问题描述】:

我用 C# 开发了 TCP/IP 服务器,监听端口,并且 GPS 设备正在将数据发送到服务器。

设备最初将IMEI号发送给服务器,服务器以01确认。设备收到确认后,向服务器发送一个新的数据包。

我可以通过 TCP 服务器获取 IMEI 号码,在发送确认后,我无法从客户端接收新的数据包数据。我也在下面包括我的代码。请让我知道我哪里错了。

我已经使用 hercules tcp 服务器工具进行了测试,下一个数据包正在成功接收,但从我的应用程序中它无法正常工作。

           try
        {
            IPAddress ipAdress = IPAddress.Parse(ConfigurationManager.AppSettings["Server"].ToString());
            // You can use local IP as far as you use the 
            //same in client

            // Initializes the Listener
            TcpListener myList = new TcpListener(ipAdress, int.Parse(ConfigurationManager.AppSettings["Port"].ToString()));

            for (; ; )
            { // Run forever, accepting and servicing connections
                //Start Listeneting at the specified port

                myList.Start();

                Console.WriteLine("Server running - Port: 8000");
                Console.WriteLine("Local end point:" + myList.LocalEndpoint);
                Console.WriteLine("Waiting for connections...");

                Socket socket = myList.AcceptSocket();
                // When accepted
                Console.WriteLine("Connection accepted from " + socket.RemoteEndPoint);

                byte[] b = new byte[1000];
                int k = socket.Receive(b);


                Console.WriteLine("echoed {0} bytes.", k);
                Console.WriteLine("Reading IMEI....");

                string hexStr = ConvertAsciiToHex(Encoding.ASCII.GetString(b, 0, b.Length));
                File.WriteAllText(@"C:\ClientFiles\testIMEI", hexStr);


                ASCIIEncoding asen = new ASCIIEncoding();
                string response = "01";
                Console.WriteLine("Ackowledgement Data - " + response);
                //
                int sentBytes = socket.Send(asen.GetBytes(response));
                Console.WriteLine("Acknowledgement data sent!\n");

                int count = 0;

                while (socket.Poll(-1, SelectMode.SelectRead))
                {
                    b = new byte[1000];
                    k = socket.Receive(b);
                    if (k > 0)
                    {
                        count++;
                        Console.WriteLine("Reading Client Data - Count - " + count.ToString() + " and lenght " + k.ToString());
                        hexStr = ConvertAsciiToHex(Encoding.ASCII.GetString(b, 0, b.Length));
                        File.WriteAllText(@"C:\ClientFiles\testData" + count.ToString(), hexStr);
                    }
                }

                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }


            myList.Stop();
        }
        catch (Exception ex)
        {
            File.WriteAllText(@"C:\ClientFiles\Error.txt", ex.Message + "****" + ex.InnerException);
        }

【问题讨论】:

  • 请逐步调试调试器并告诉我们它卡在哪里。
  • 感谢Zicore回复,发送确认后下一个包为空
  • 你正在尝试接收 1000 个字节,可能它已经在那个缓冲区中了。
  • 这1000字节是一个新对象,我下次用它来接收任何新数据包。
  • int k = socket.Receive(b); 接收套接字当前接收到的具有定义的最大值的所有字节。你的 1000 个字节。所以可能第二个数据包已经在那个缓冲区中了。

标签: c# sockets tcp gps ip


【解决方案1】:

我在程序中没有看到任何错误,除了这些 cmets:

 // Don't use the b.Length in this command:
 string hexStr = ConvertAsciiToHex(Encoding.ASCII.GetString(b, 0, b.Length));
 // use k instead, the numbers actually read, not the length of the buffer.
 string hexStr = ConvertAsciiToHex(Encoding.ASCII.GetString(b, 0, k)); 

 while (socket.Poll(-1, SelectMode.SelectRead))
 {
     // b has been allocated, don't need this
     // b = new byte[1000];
     k = socket.Receive(b);
     if (k > 0)
     {
         count++;
         Console.WriteLine("Reading Client Data - Count - " + count.ToString() + " and lenght " + k.ToString();
        // use k not Length
        hexStr = ConvertAsciiToHex(Encoding.ASCII.GetString(b, 0, k /*b.Length*/));
        File.WriteAllText(@"C:\ClientFiles\testData" + count.ToString(), hexStr);
     }
}

你知道轮询命令是否会返回吗?可能是它引发了一个错误,而你错过了那个错误。看看吧。

【讨论】:

  • 我试过上面的,结果是一样的。基本上,我想知道在向客户端发送确认后如何接收数据。
  • 根据GPS数据接收文档,确认应为01,为二进制流。如何处理这个。有什么想法吗?
  • 但是,轮询命令会返回吗?您确定客户端正在发送 GPS 数据吗?我在您的代码中没有看到任何问题,它应该可以工作,您必须调试代码并查看 Poll 命令是否返回,如果没有返回则客户端没有发送任何数据。
  • 哦,我明白了,01 作为二进制流,您是否尝试仅发送 2 个字节 0x00 和 0x01 而不是 ascii '01'?实际上是二进制的 48 和 49。
猜你喜欢
  • 2011-09-13
  • 2012-08-09
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多