我有一个关于套接字/客户端的问题......
我刚刚用 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);
发布评论
评论(3)
您只需要知道服务器的 IP 地址,并在客户端代码中指定即可。
您可以通过在命令提示符下输入
ipconfig /all
来获取您的 IP。请注意,这只会为您提供与本地网络的连接。如果您尝试通过 Internet 执行此操作,则需要使用可查找 WAN(广域网)IP 地址的服务。您可以通过谷歌搜索如何做到这一点,因为没有“标准”服务可以做到这一点。
如果您有路由器,则需要将端口转发到运行服务的计算机。查找网络地址转换,并查看路由器的文档,或致电技术支持。或者谷歌“我如何转发端口?”。
设置好网络并了解所有连接信息后,假设您正在使用
TcpListener
:IpAddress.Any
。指定您喜欢的任何尚未使用的端口号 (8888
)。IPAddress.Parse("127.0.0.1")
和8888
替换为服务器的端口和地址。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
:TcpListener
withIpAddress.Any
. Specify any port number you like, that isn't already in use (8888
).IPAddress.Parse("127.0.0.1")
and8888
with the port and address of the server.OverMars 的解决方案并不好,因为像 ipchicken 这样的第三方网站会给你你的 WAN IP。本地连接将无法工作。如果您需要更多信息,请查找 NAT(网络地址转换)。
如果您想要连接一台单独的机器,只需绑定到“任何”地址即可。
请注意,“任意”会转换为地址“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.
Note that "Any" translates to the address "0.0.0.0".
127.0.0.1
是“此计算机”或运行应用程序的设备的内部地址。现代网络上的每台计算机都将具有 127.0.0.1 和至少 1 个其他 IP 地址。要查找另一台 Windows 计算机的 IP 地址,您可以在命令提示符下使用 ipconfig。您将得到如下内容:
Windows IP 配置
在本例中,10.0.0.2 是您可以用来从其他计算机连接到它的 IP 地址。像这样:
Windows 计算机还将有一个名称,例如
JimsPC
或JimsPC.abc.com
,也可以在TcpClient
构造函数中使用或BeginConnect
、Connect
方法,如下所示。或者
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
In this case the 10.0.0.2 is the IP address you can use to connect to it from other computers. Like so:
A Windows computer will also have a name such as
JimsPC
orJimsPC.abc.com
that can also be used in theTcpClient
constructor orBeginConnect
,Connect
methods like so.or