如何使用 TCPListener(或其他方法)绑定到网络上的设备

发布于 2024-08-08 11:22:39 字数 573 浏览 5 评论 0原文

我有一个网络地址为 192.168.xxx.xxx 端口 xxxx 的设备。这是我的网络上的有效地址。我尝试使用 TCPListener 建立服务器连接,但收到错误 “错误...... System.Net.Sockets.SocketException:请求的地址在其上下文中无效 在 System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress 套接字地址) 在 System.Net.Sockets.Socket.Bind(EndPoint localEP) 在 System.Net.Sockets.TcpListener.Start(Int32 积压) 在 System.Net.Sockets.TcpListener.Start() 在 C:.....\Server.cs:line 49" 中的 WinMarkTest.Server.Main() 处

当我使用 myListener.Start() 方法时,

。 “本地地址”是否意味着使用 TCPListener 的服务器的地址?

我还能如何建立此连接?该设备位于我的内部网络(防火墙的我这边)内。

I have a device with a network address of 192.168.xxx.xxx port xxxx. This is a valid address on my network. I have tried to use TCPListener to make the connection for the server but receive the error
"Error..... System.Net.Sockets.SocketException: The requested address is not valid in its context
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress
socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at System.Net.Sockets.TcpListener.Start()
at WinMarkTest.Server.Main() in C:.....\Server.cs:line 49"

when I use the myListener.Start() method.

Does the "local address" mean the address of the server to use TCPListener?

How else can I make this connection. The device is within my internal network (my side of the firewall).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聊慰 2024-08-15 11:22:39

TcpListener 将在您的服务器上运行,等待与特定端口的连接。 TcpClient 将用于建立到 192.168.xxx.xxx:xxxx 地址的连接。因此,当您执行 Listener.Start 时,您正在侦听在侦听器运行的地址和端口上与侦听器建立的连接。本地地址确实意味着您正在侦听要建立的连接的地址。

如果您想连接到远程 ip:port 那么您应该尝试 TcpClient。一个简单的测试是看看您是否可以连接到 smtp 服务器或类似的服务器。

编辑:-- 包括一个非常粗略的示例,用于连接 pop.google.com 并发送/接收数据。

static void Main(string[] args)
    {
        Stream networkStream = null;
        string hostName = "pop.gmail.com";
        int port = 995;
        TcpClient client = new TcpClient();
        MemoryStream dataStream = new MemoryStream();
        try
        {

            client.SendTimeout = 15000;
            client.ReceiveTimeout = 15000;
            client.Connect(hostName, port);
            networkStream = new SslStream(client.GetStream(), true);
            ((SslStream)networkStream).AuthenticateAsClient(hostName);
            const int ChunkSize = 256;
            int bytesRead = 0;
            const int BufferSize = 1024;
            byte[] buffer = new byte[BufferSize];

            //CONNECT SHOULD GET BANNER
            string messageReceived;
            using (dataStream = new MemoryStream())
            {
                do
                {
                    bytesRead = networkStream.Read(buffer, 0, ChunkSize);
                    dataStream.Write(buffer, 0, bytesRead);
                    messageReceived = Encoding.UTF8.GetString(dataStream.ToArray());
                } while (!messageReceived.EndsWith(Environment.NewLine));

                Console.WriteLine("Response:{0}", Encoding.UTF8.GetString(dataStream.ToArray()));
            }

            buffer = Encoding.UTF8.GetBytes("USER [email protected]\r\n");

            networkStream.Write(buffer, 0, buffer.Length);

            buffer = new byte[BufferSize];
            using (dataStream = new MemoryStream())
            {
                do
                {
                    bytesRead = networkStream.Read(buffer, 0, ChunkSize);
                    dataStream.Write(buffer, 0, bytesRead);
                    messageReceived = Encoding.UTF8.GetString(dataStream.ToArray());
                } while (!messageReceived.EndsWith(Environment.NewLine));

                Console.WriteLine("Response:{0}", Encoding.UTF8.GetString(dataStream.ToArray()));
            }
        }
        catch (Exception e)
        {
            Console.Write(e);
        }
        finally
        {
            if (networkStream != null)
            {
                networkStream.Dispose();
                networkStream = null;
            }
            if (client != null)
            {
                if (client.Connected)
                {
                    client.Client.Disconnect(false);
                }
                client.Close();
                client = null;
            }
        }

        Console.ReadKey();
    }

TcpListener would be running on your server waiting for connections to a particular port. A TcpClient would be used to make a connection to the 192.168.xxx.xxx:xxxx address. So when you do a Listener.Start you are listening for connections to be made to your listener on the address and port where the listener is running. The local address does mean the address address that you are listening for connections to be made on.

If you want to make a connection to a remote ip:port then you should try the TcpClient. An easy test would be to see if you could connect to an smtp server or something like that.

EDIT: -- Included a very crude example to connect and send/receive data to pop.google.com.

static void Main(string[] args)
    {
        Stream networkStream = null;
        string hostName = "pop.gmail.com";
        int port = 995;
        TcpClient client = new TcpClient();
        MemoryStream dataStream = new MemoryStream();
        try
        {

            client.SendTimeout = 15000;
            client.ReceiveTimeout = 15000;
            client.Connect(hostName, port);
            networkStream = new SslStream(client.GetStream(), true);
            ((SslStream)networkStream).AuthenticateAsClient(hostName);
            const int ChunkSize = 256;
            int bytesRead = 0;
            const int BufferSize = 1024;
            byte[] buffer = new byte[BufferSize];

            //CONNECT SHOULD GET BANNER
            string messageReceived;
            using (dataStream = new MemoryStream())
            {
                do
                {
                    bytesRead = networkStream.Read(buffer, 0, ChunkSize);
                    dataStream.Write(buffer, 0, bytesRead);
                    messageReceived = Encoding.UTF8.GetString(dataStream.ToArray());
                } while (!messageReceived.EndsWith(Environment.NewLine));

                Console.WriteLine("Response:{0}", Encoding.UTF8.GetString(dataStream.ToArray()));
            }

            buffer = Encoding.UTF8.GetBytes("USER [email protected]\r\n");

            networkStream.Write(buffer, 0, buffer.Length);

            buffer = new byte[BufferSize];
            using (dataStream = new MemoryStream())
            {
                do
                {
                    bytesRead = networkStream.Read(buffer, 0, ChunkSize);
                    dataStream.Write(buffer, 0, bytesRead);
                    messageReceived = Encoding.UTF8.GetString(dataStream.ToArray());
                } while (!messageReceived.EndsWith(Environment.NewLine));

                Console.WriteLine("Response:{0}", Encoding.UTF8.GetString(dataStream.ToArray()));
            }
        }
        catch (Exception e)
        {
            Console.Write(e);
        }
        finally
        {
            if (networkStream != null)
            {
                networkStream.Dispose();
                networkStream = null;
            }
            if (client != null)
            {
                if (client.Connected)
                {
                    client.Client.Disconnect(false);
                }
                client.Close();
                client = null;
            }
        }

        Console.ReadKey();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文