【问题标题】:How to use TCPListener (or other method) to bind to device on the network如何使用 TCPListener(或其他方法)绑定到网络上的设备
【发布时间】:2010-12-08 04:30:18
【问题描述】:

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

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

“本地地址”是指使用TCPListener的服务器地址吗?

我还能如何建立这种联系。该设备在我的内部网络(我的防火墙一侧)内。

【问题讨论】:

  • 使用 TcpClient 连接到您的服务器。

标签: c# client-server tcpclient tcplistener


【解决方案1】:

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 test.net.user@gmail.com\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();
    }

【讨论】:

  • 这是一个如何在 MSDN 上使用 TcpClient 的示例,非常简单。 msdn.microsoft.com/en-us/library/…
  • 感谢 cmets/answers。我仍在努力。直流
  • 我正在按照 msdn 教程使用 TcpClient。但这也使用 TcpListener ,它只在端口上侦听任何地址。但我的代码从未执行过“TcpClient client = this.tcpListener.AcceptTcpClient();”因此,无论出于何种原因,客户端永远不会连接到服务器。有任何想法吗?我知道我在正确的端口和地址上。我已经 ping 了仪表并使用了 OEM 软件,所以我知道仪表有一些响应。 ....DC
  • 好吧,我还是不明白你为什么要使用 TcpListener,你是做服务器还是连接服务器?我已经更新了我的帖子,以包含一个非常粗略的控制台应用程序示例,该应用程序连接到 gmail 并发送/接收数据。从您的原始帖子看来,您正在尝试连接到远程服务器。请详细说明您正在尝试做什么。
  • 谢谢。在家中与 Gmail 的连接工作正常,但在我的工作网络中,我收到一个异常,“主机主动拒绝连接。”我会继续努力的。是的,我正在尝试从电能表(千瓦时)发送命令和接收数据。所以我正在尝试连接到服务器。感谢您的耐心等待。
猜你喜欢
  • 2020-08-04
  • 2022-11-06
  • 2014-11-01
  • 1970-01-01
  • 2015-04-23
  • 1970-01-01
  • 2014-12-20
  • 2020-10-23
  • 2011-09-06
相关资源
最近更新 更多