环回/本地主机问题

发布于 2024-12-05 20:48:17 字数 326 浏览 0 评论 0 原文

我有一个关于套接字/客户端的问题......

我刚刚用 C# 编写了一个客户端服务器程序。我想知道如何连接到具有不同 IP 地址的计算机。例如,如果我想在两台不同的机器上分别运行客户端和服务器,环回(或使用本地主机)将不允许这样做......

不太熟悉网络,任何帮助将不胜感激......这里是我在客户端处理环回的代码:

TcpClient client = new TcpClient();

IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);

i have a question about sockets/clients....

I just finished writing a client server program in C#. I was wondering, how do you connect to computers that have a different IP address. For instance, if i want to run a client and server seperately on two different machines, loopback (or using the localhost) won't allow for this....

Not too familiar with networking, any help would be greatly appreciated.. here is my code on the client side which deals with loopback:

TcpClient client = new TcpClient();

IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);

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

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

发布评论

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

评论(3

幼儿园老大 2024-12-12 20:48:17

您只需要知道服务器的 IP 地址,并在客户端代码中指定即可。

您可以通过在命令提示符下输入 ipconfig /all 来获取您的 IP。请注意,这只会为您提供与本地网络的连接。

如果您尝试通过 Internet 执行此操作,则需要使用可查找 WAN(广域网)IP 地址的服务。您可以通过谷歌搜索如何做到这一点,因为没有“标准”服务可以做到这一点。

如果您有路由器,则需要将端口转发到运行服务的计算机。查找网络地址转换,并查看路由器的文档,或致电技术支持。或者谷歌“我如何转发端口?”。

设置好网络并了解所有连接信息后,假设您正在使用 TcpListener

You just need to know the IP address of the server, and specify that in the client side code.

You can get your IP by typing ipconfig /all on the command prompt. Note that this will only give you the connection to the local network.

If you're trying to do this over the Internet, you'll need to use a service that finds your WAN (wide-area network) IP address. You can google for how to do that, as there is no "standard" service to do this.

If you have a router, you'll need to forward a port to the machine your service is running on. Look up Network Address Translation, and check out the documentation for your router, or call tech support. Or google "how do I forward ports?".

Once you have your network set up, and know all your connection info, and assuming you are using TcpListener:

  • On the server side, simply set up your TcpListener with IpAddress.Any. Specify any port number you like, that isn't already in use (8888).
  • On the client side, connect to the server's IP address. Replace IPAddress.Parse("127.0.0.1") and 8888 with the port and address of the server.
撩起发的微风 2024-12-12 20:48:17

OverMars 的解决方案并不好,因为像 ipchicken 这样的第三方网站会给你你的 WAN IP。本地连接将无法工作。如果您需要更多信息,请查找 NAT(网络地址转换)。

如果您想要连接一台单独的机器,只需绑定到“任何”地址即可。

TcpClient client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, 8888);

请注意,“任意”会转换为地址“0.0.0.0”。

OverMars' solution is not good because 3rd party websites like ipchicken will give you your WAN IP. Local connections will not work. Look up NAT(network address translation) if you need more info.

Just bind to the "Any" address if you want a seperate machine to connect.

TcpClient client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, 8888);

Note that "Any" translates to the address "0.0.0.0".

萌辣 2024-12-12 20:48:17

127.0.0.1 是“此计算机”或运行应用程序的设备的内部地址。现代网络上的每台计算机都将具有 127.0.0.1 和至少 1 个其他 IP 地址。

要查找另一台 Windows 计算机的 IP 地址,您可以在命令提示符下使用 ipconfig。您将得到如下内容:

Windows IP 配置

Ethernet adapter Local Area Connection:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 10.0.0.2
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 10.0.0.1

在本例中,10.0.0.2 是您可以用来从其他计算机连接到它的 IP 地址。像这样:

TcpClient client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("10.0.0.2"), 8888);  
client.Connect(serverEndPoint);

Windows 计算机还将有一个名称,例如 JimsPCJimsPC.abc.com,也可以在 TcpClient 构造函数中使用或 BeginConnectConnect 方法,如下所示。

TcpClient client = new TcpClient("JimsPC", 8888);

或者

TcpClient client = new TcpClient();
client.Connect("JimsPC", 8888);

127.0.0.1 is an internal address to "this computer" or device where the application is running. Each computer will have 127.0.0.1 and at least 1 other IP address on a modern network.

To find out the IP address of another Windows computer you can use ipconfig from the command prompt. You will get something like this:

Windows IP Configuration

Ethernet adapter Local Area Connection:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 10.0.0.2
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 10.0.0.1

In this case the 10.0.0.2 is the IP address you can use to connect to it from other computers. Like so:

TcpClient client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("10.0.0.2"), 8888);  
client.Connect(serverEndPoint);

A Windows computer will also have a name such as JimsPC or JimsPC.abc.com that can also be used in the TcpClient constructor or BeginConnect, Connect methods like so.

TcpClient client = new TcpClient("JimsPC", 8888);

or

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