简体   繁体   English

TCP服务器IP客户端连接问题

[英]TCP Server IP Client Connection Issue

OK so I've just started messing with TCP using c#, and I've successfully set up a server that i can send a 'Hello World' Message to, anyway I've been doing this locally (because both laptops are connected to the same router) i just use the 192.168 number to connect. 好的,所以我刚开始使用c#弄乱TCP,并且我已经成功设置了一个服务器,可以向其发送“ Hello World”消息,无论如何,我一直在本地进行此操作(因为两台笔记本电脑都已连接到同一路由器),我只是使用192.168号码进行连接。 but The whole purpose of it is to work over the internet, and the routers ip address is obviously the same for both computers, if i type the routers IP address it doesn't work, and if i type the 192.168 number that definitely won't work over the internet... So what IP do i use, or what is a better solution? 但是它的全部目的是通过互联网工作,并且路由器的IP地址对于两台计算机来说显然是相同的,如果我输入路由器的IP地址不起作用,并且如果我输入的192.168号码肯定不会的话,不能通过Internet工作...那么我要使用什么IP,或者什么是更好的解决方案?

here's the line of code if it matters 这是重要的代码行

 var client = ScsClientFactory.CreateClient(new ScsTcpEndPoint("192.168.1.142", 10085));

Where 192.168.1.142 is the local ip of the laptop with the server started on it 其中192.168.1.142是笔记本电脑的本地IP,服务器已启动

and 10085 is the port. 10085是端口。

You have to configure your router to forward any incoming connection to the port 10085 to you local IP adress. 您必须将路由器配置为将到端口10085的任何传入连接转发到本地IP地址。 Then anyone will be able to connect using your external IP adress. 这样,任何人都可以使用您的外部IP地址进行连接。

Each router has it's own configuration system so you have to search "port forwading" and your router model in google. 每个路由器都有其自己的配置系统,因此您必须在Google中搜索“端口转发”和路由器型号。

It depends on what your trying to acheive i guess. 这取决于您想达到的目标。 If for example your making a chat application. 例如,如果您正在创建聊天应用程序。 The client (behind the router) lets say its local IP is 192.168.1.111 and router IP is 80.120.78.100. 客户端(在路由器后面)可以说其本地IP为192.168.1.111,路由器IP为80.120.78.100。 The client would connect to the server. 客户端将连接到服务器。

Once that connection is made it doesn't matter about sending back to the client because the connection is already open between client and server so the server would just use the same connection. 建立连接后,发送回客户端就没有关系,因为客户端和服务器之间的连接已经打开,因此服务器将使用相同的连接。 The router figures out where to "route" the packet, stuff which generally you dont need to know about. 路由器会找出将数据包“路由”到的位置,通常您不需要了解这些内容。

If however your server is the one looking for clients, then thats different. 但是,如果您的服务器是寻找客户端的服务器,那就不一样了。

I'll try to explain a little about networks, but you'll have to search about it . 我将尝试解释一些有关网络的信息,但是您必须进行搜索

Basically , understand an IP mask , such as 192.168.1.0 as one network. 基本上 ,将一个IP掩码 (例如192.168.1.0理解为一个网络。 Router's role is to connect different networks, that's why a router typically has 2 ports, WAN (wide) and LAN (local). 路由器的作用是连接不同的网络,这就是为什么路由器通常具有WAN(宽)和LAN(本地)两个端口的原因。

With this concept, you can see the internet as one big network made from the connection of various ISPs. 有了这个概念,您就可以将Internet视为由各种ISP连接而成的一个大型网络。 Each ISP has a router to its network, and another one that gives you an internet connection. 每个ISP都有一个通往其网络的路由器,而另一个则为您提供Internet连接。 Finally, you have a router at your home. 最后,您在家中有一台路由器。 So, from this, you can understand that there are 3 networks connected: your home, your ISP and the internet. 因此,从中您可以了解到有3个网络连接在一起:您的家庭,您的ISP和Internet。

In order for you to be able to connect to a computer at my home, I have to make this computer available from the internet, I have to publish it. 为了使您能够连接到我家里的计算机,我必须从互联网上使这台计算机可用,我必须将其发布 I do this by setting up a NAT (network area translation) at my router. 我是通过在路由器上设置NAT (网络区域转换)来实现的。 This NAT says "anything that comes from the internet on port 12345, forward to 192.168.1.10 (my server) at port 80". 此NAT表示“任何来自Internet的端口12345,都将转发到端口80的192.168.1.10(我的服务器)”。

This is an extremely simple explanation, ok? 这是一个非常简单的解释,好吗?


Now, let's take a big step back. 现在,让我们退后一步。 If you have another computer available on your network, you can test if your program is working with a much simpler approach. 如果网络上有另一台计算机可用,则可以使用更简单的方法测试程序是否正常运行。

  1. Connect both computers to you LAN , so they will acquire similar IP addresses. 将两台计算机都连接到您的LAN ,这样它们将获得相似的IP地址。 Let's pretend they are 192.168.1.10 and 192.168.1.20 假设它们是192.168.1.10192.168.1.20
  2. Run the server at 10 and disable all kind of firewalls (Windows and third party) 10运行服务器并禁用所有类型的防火墙(Windows和第三方)
  3. Run the client at 20 and try to connect to the server 20运行客户端,然后尝试连接到服务器

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM