消息未收到TCP

时间:2014-02-21 08:49:16

标签: c# .net tcpclient tcplistener ip-camera

我的IP-Camera配置为向我的计算机发送警报消息。

摄像机的IPAddress:10.251.51.1
我的电脑的IPAddress:10.251.51.136

可以将摄像机配置为将消息发送到计算机上的任何端口。

因此,我将警报目的地设置为我的电脑的IP地址:10.251.51.136 port 3487

现在,在我的电脑上应该怎样做以获取相机发送的信息?

我尝试从TCPListener写一个MSDN,代码如下:

                Int32 port = 3487;
                IPAddress localAddr = IPAddress.Parse("10.251.51.136");

                // TcpListener server = new TcpListener(port);
                server = new TcpListener(localAddr, port);
                server.Start();

                // Start listening for client requests.

                // Buffer for reading data
                Byte[] bytes = new Byte[256];
                String data = null;

                // Enter the listening loop.
                while (true)
                {
                    Console.Write("Waiting for a connection... ");

                    // Perform a blocking call to accept requests.
                    // You could also user server.AcceptSocket() here.
                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("Connected!");

                server.Start();
                    data = null;

                    // Get a stream object for reading and writing
                    NetworkStream stream = client.GetStream();

                    int i;

                    // Loop to receive all the data sent by the client.
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        // Translate data bytes to a ASCII string.
                        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        Console.WriteLine("Received: {0}", data);

                        // Process the data sent by the client.
                        data = data.ToUpper();

                        byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                        // Send back a response.
                        stream.Write(msg, 0, msg.Length);
                        Console.WriteLine("Sent: {0}", data);
                    }

                    // Shutdown and end connection
                    client.Close();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                // Stop listening for new clients.
                server.Stop();
            }

但这似乎不起作用。

或者我的整个方法有缺陷吗?我应该为上面的方案实施TCPListenerTCPServer吗?

任何指针为什么?

0 个答案:

没有答案