设备每秒发送事件,从 192.168.101.4 到 192.168.101.2:4002。 我需要监听并解析这些事件,但监听部分已经失败。
有人可以解释一下,如何在紧凑框架的有限选项下做到这一点吗?
最佳答案
我自己解决了这个问题,但感谢 PaulH 提供的有用教程。
对于有同样问题的程序员:
/// <param name="port">Port to use</param>
/// <param name="size">count of bytes to return</param>
/// <returns>a byteArray with received data</returns>
public static byte[] readEvent(int port, int size)
{
byte[] bytes = new byte[size];
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[1];
TcpListener listener;
try
{
listener = new TcpListener(ipAddress, port);
listener.Start();
TcpClient tcpClient = listener.AcceptTcpClient();
NetworkStream stream = tcpClient.GetStream();
stream.Read(bytes, 0, bytes.Length);
listener.Stop();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return bytes;
}
关于tcp - 在win CE中监听tcp事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20122854/