gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:54:45 24 4
gpt4 key购买 nike

我用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com