c# - TCP IP 服务器确认并再次接收数据

标签 c# sockets tcp gps ip

我用 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);
        }

最佳答案

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

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

您知道 Poll 命令是否返回吗?可能会引发错误而您错过了那个错误。检查一下。

关于c# - TCP IP 服务器确认并再次接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17270891/

相关文章:

c# - 文件夹权限 : Full control granted to all users

c# - .ASPX 上的自定义用户控件和友好的属性项集合(类似于 ListBox 和 ListItems,但带有 L​​ist<Class>)

c# - 'Microsoft.ACE.OLEDB.12.0'错误无解决方案

c - 套接字服务器内存使用率上升,变得无响应

java - 程序卡在 ObjectInputStream 构造函数处

http - 如何在 .NET 中创建 HTTP 请求监听器 Windows 服务

linux - 用监听套接字 fork

c# - 将应用程序部署到 IIS(MVC3、EF 4.3)后分页出现意外结果

android - 套接字输入流和 Unicode

python - TCP 服务器/客户端 : [Errno 32] Broken pipe