【问题标题】:Displaying the IP address of the local machine in order to connect outside the network?显示本地机器的 IP 地址以便连接到网络外部?
【发布时间】:2017-02-07 19:09:54
【问题描述】:

我正在尝试制作一个可以在大学和家庭之间使用的简单服务器。服务器工作正常,客户端可以连接。但我希望能够从本地网络外部进行连接。我正在尝试找到一种方法来显示连接到它所需的机器的 IP 地址:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace SocketProgramming
{
    class Program
    {

    private static byte[] _buffer = new byte[1024];
    private static Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    private static List<Socket> _clientSockets = new List<Socket>();
    private static List<string> _nicknames = new List<string>();

    static void Main(string[] args)
    {

        SetupServer();
        Console.ReadLine();

    }

    private static void SetupServer()
    {

        Console.Write("Enter a port number to establish the server on: ");
        int portNum = Int32.Parse(Console.ReadLine());
        _serverSocket.Bind(new IPEndPoint(IPAddress.Any, portNum));
        _serverSocket.Listen(5);

        _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);

    }
}
}

如何获取本地机器的IP地址?我希望能够在任何机器上运行此代码,因此在代码中使用静态 IP 将无济于事。

【问题讨论】:

  • Get local IP address的可能重复
  • 我猜您的“服务器”将在您的家庭网络中,而您的客户端将在该网络之外运行。在这种情况下,您想知道服务器的 IP 地址,这将是您家庭网络的公共 IP 地址。此值不是静态的,除非您使用某些服务 ($$) 为您的家庭获取静态 IP 地址。
  • @ThomasWeller 那人修好了。谢谢老兄!

标签: c# tcp server ip


【解决方案1】:

这个post 在尝试解决同样的问题时对我非常有帮助。我的实现代码最终使用了this answer,并稍作修改以准确指定我想要的协议类型。

string localIP;

        try
        {
            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.IP))
            {
                socket.Connect("8.8.8.8", 65530);
                IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
                localIP = endPoint.Address.ToString();
            }
        }
        catch (Exception ex)
        {
            localIP = String.Empty;
        }

        return localIP;

【讨论】:

    【解决方案2】:

    值得注意的是,一台机器可能有多个 IP 地址。您可以使用 dos 或 powershell cmd 行中的“ipconfig”命令获取快速列表。你可以像这样在 c# 中做类似的事情:

            foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces()) {
                Console.WriteLine("Name: " + netInterface.Name);
                Console.WriteLine("Description: " + netInterface.Description);
                Console.WriteLine("Addresses: ");
                IPInterfaceProperties ipProps = netInterface.GetIPProperties();
                foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses) {
                    Console.WriteLine(" " + addr.Address.ToString());
                    }
                Console.WriteLine("");
                }
    

    见:Get All IP Addresses On Machine

    此外,如果尝试访问远程计算机,您可能会遇到防火墙/路由器问题。如果您的主机 IP 是非路由地址(如 10.*.*.* 或 192.168.*.* 等),您将无法从该网络外部访问它。

    【讨论】:

      猜你喜欢
      • 2016-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      相关资源
      最近更新 更多