Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在win CE中收听tcp事件_Tcp_Compact Framework_Listener_Windows Ce - Fatal编程技术网

在win CE中收听tcp事件

在win CE中收听tcp事件,tcp,compact-framework,listener,windows-ce,Tcp,Compact Framework,Listener,Windows Ce,设备每秒发送一次事件,从192.168.101.4到192.168.101.2:4002。 我需要监听和解析这些事件,但监听部分已经失败了 有人能解释一下,如何使用compact framework的有限选项来实现这一点吗?我自己解决了这个问题,但感谢PaulH提供的有用教程 对于有相同问题的程序员: /// <param name="port">Port to use</param> /// <param name="size">count

设备每秒发送一次事件,从192.168.101.4到192.168.101.2:4002。 我需要监听和解析这些事件,但监听部分已经失败了


有人能解释一下,如何使用compact framework的有限选项来实现这一点吗?

我自己解决了这个问题,但感谢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;
    }
///要使用的端口
///要返回的字节数
///具有接收数据的字节数组
公共静态字节[]读取事件(int端口,int大小)
{
字节[]字节=新字节[大小];
IPHostEntry ipHostInfo=Dns.GetHostEntry(Dns.GetHostName());
IPAddress IPAddress=ipHostInfo.AddressList[1];
听者;
尝试
{
侦听器=新的TcpListener(IP地址,端口);
listener.Start();
TcpClient TcpClient=listener.AcceptTcpClient();
NetworkStream=tcpClient.GetStream();
读取(字节,0,字节,长度);
listener.Stop();
}
捕获(例外e)
{
控制台写入线(e.Message);
}
返回字节;
}

您能告诉我们您尝试了什么吗?我成功地使用了一个连接的套接字和函数“socket.receive()”,但只有在发送了一个包之后才可以直接使用。您能给出一个您正在使用的代码的最小示例,并指出它失败的地方以及您遇到的错误吗?在不了解您的具体问题的情况下,我所能做的就是向您介绍一个通用套接字教程: