简体   繁体   English

如何在客户端程序中指定服务器IP地址?

[英]How do I specify Server Ip address in my Client program?

I'm currently working with TCP/IP Sockets, My client console program has to connect with server which is my PC, If the destination in the client program is specified as a local host it works fine, what I need to get done with is to connecting the client with my server through internet, what I did is looked up for my IP address on http://www.whatismyip.com/ and tried but it didn't work as I think it's a Network Interface IP address, then I altered destination IP address in client program specifically to the address of my computer which I want it to be a server, but that didn't work also. 我当前正在使用TCP / IP套接字,我的客户端控制台程序必须与服务器即我的PC连接,如果客户端程序中的目标被指定为本地主机,则可以正常工作,我需要做的是要通过互联网将客户端与服务器连接起来,我在http://www.whatismyip.com/上查找了我的IP地址,并尝试了一下,但是由于我认为它是网络接口IP地址而无法正常工作,然后我将客户端程序中的目标IP地址专门更改为我希望它作为服务器的计算机的地址,但这也没有用。 Here's my code. 这是我的代码。

Ip = (IPAddress.Parse("192.168.1.4"));

MyClient.Connect(Ip,6000);
GetStream = MyClient.GetStream();
Console.WriteLine("CONNECTED TO SERVER");
Read = new BinaryReader(GetStream);
Write = new BinaryWriter(GetStream);

There 2 things (at least) that you should be aware of: 您至少应该注意两件事:

1. To access your computer from Internet (from the public address, the one you get with whatismyip.com) you need to open the port (6000) at the router, and tell the router to what IP it should forward the incoming connection. 1.要从Internet(从公共地址(通过whatismyip.com获得的地址)中访问Internet)访问计算机,您需要打开路由器的端口(6000),并告诉路由器应该转发传入连接的IP地址。 You could specified specifics ports or put a DMZ host where all the incoming connections will be routed to that host/PC. 您可以指定特定的端口,也可以放置DMZ主机,将所有传入的连接路由到该主机/ PC。 Read your router manual to see how that is done. 阅读路由器手册以了解操作方法。

2. You cannot access your public IP from the inner side of the router (intranet), if you want to connect to your public IP you need to be in another network. 2.您不能从路由器(内部网)的内部访问公用IP,如果要连接到公用IP,则需要位于另一个网络中。

If you have a dynamic IP (is the default) every time the router is powered off and on then, most probably, that IP would change, you need to investigate thru whatismyip.com in order to know what IP has been assigned. 如果您每次关闭路由器电源后都具有动态IP(默认值),那么该IP很可能会更改,那么您需要通过whatismyip.com进行调查,以了解已分配了哪个IP。 You could connect to dyndns.org and ask for a host name myhost.mydomain.com (.es, .fr, etc), and in the router tell the DDNS (Dynamic DNS) to update that host every time the IP changes. 您可以连接到dyndns.org并询问主机名myhost.mydomain.com (.es,.fr等),然后在路由器中告诉DDNS(动态DNS)在IP每次更改时更新该主机。 In your client program you then connect to MyClient.Connect("myhost.mydomain.com", 6000); 然后,在客户端程序中,连接到MyClient.Connect("myhost.mydomain.com", 6000);

Hope I explain myself well enough, anyway, if you have any question let me know. 希望我能很好地解释自己,如果您有任何问题让我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我的服务器知道远程客户端的IP地址,如何发送消息到远程客户端? - If my server knows a remote client's IP address, how do I send a message to the remote client? 如果服务器和客户端位于同一域中,如何获取客户端的外部IP地址? - How do I get the external IP address of a client if the server and the client are on the same domain? 如何指定数据库连接的传出IP地址? - How do I specify the outgoing IP address of a database connection? 如何从服务器获取我的Corba客户端的IP地址 - How do I get the IP address of my Corba client from the sever C#/ winforms IP地址问题中的客户端/服务器聊天程序 - client/server chat program in C#/winforms IP address issue 当我的tcp客户端使用ip地址连接到服务器但与“ localhost”一起使用时,出现异常 - I am getting an exception when my tcp client connects to the server with ip address but works with “localhost” 如何在.net套接字程序中手动设置客户端的IP地址 - How to set IP address of client manually in .net socket program 如何将带有数据的客户端 IP 地址从服务器发送到另一个客户端? - How can I send clients IP address with data from server to another client? 如何自动为我的服务器程序获取IP? - How to automatically get IP to my server program? ASP.NET Core 3.1 - 如何获取客户的 IP 地址? - ASP.NET Core 3.1 - How do I get a client's IP Address?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM