[英]Java - communication through sockets
Let's say, within a program, there are capabilities to connect to a socket and server(which would be my own computer - localhost) and has all the handling for communicating to the server and back (receiving and sending messages like a chat service). 比方说,在一个程序中,有能力连接到插座和服务器(这是我自己的电脑 - 本地主机),并拥有所有的通信服务器和背面(接收和发送消息像聊天服务)的处理。 If i send this program as a .jar file to two other users their respective machines, as the users executes the program, it would connect to this socket and server. 如果我这个节目作为一个.jar文件发送给其他两个用户各自的机器,用户执行程序,它会连接到这个插槽和服务器。 If all was set up correctly (correctly writes and reads from the socket inputstream and outputstreams) and displays the messages on a GUI chat box, would the two users be able to communicate with this chat service? 如果一切都设置正确(正确地写入,并从插座InputStream和outputstreams读取),并显示在GUI聊天框中的消息,将在两个用户能够使用此聊天服务沟通?
Yes, but the main barrier that would prevent users from doing this is a little something called Network Address Translation (NAT). 是的,但是阻止用户执行此操作的主要障碍是网络地址转换 (NAT)。
In short, the vast majority of computers connected to the Internet do not have their own, unique, public IPv4 address (and the vast majority do not have IPv6 addresses either, because ISPs are dragging their feet in rolling out IPv6, but that's another matter entirely). 简而言之,连接到Internet的绝大多数计算机都没有自己的唯一公共IPv4地址(而且绝大多数也没有IPv6地址,因为ISP拖延了推出IPv6的步伐,但这是另一回事完全)。
Dedicated servers often do have (usually multiple) unique IPv4 addresses, but significant numbers of IP addresses are usually only available to hosting companies that operate their own Autonomous System -- and even then, purchasing IPs these days is very expensive, because they're a valuable and rare commodity. 专用服务器通常确实具有(通常是多个)唯一的IPv4地址,但是通常只有运行自己的自治系统的托管公司才能使用大量的IP地址-即使那样,如今购买IP的价格也非常昂贵,因为它们一个有价值的和稀缺的商品。
Anyway, two random users on some sort of home Internet connection (like cable, DSL, or fiber) almost certainly have a router or modem of some sort that performs NAT, giving each user device (computer, tablet, etc.) their own private LAN IP, instead of giving each device a unique public IP. 无论如何,在某种家庭Internet连接(例如电缆,DSL或光纤)上的两个随机用户几乎可以肯定具有执行NAT的某种路由器或调制解调器 ,从而为每个用户设备(计算机,平板电脑等)提供了自己的专用设备。 LAN IP,而不是为每个设备提供唯一的公用IP。
This means that inbound connections from the public Internet are, by default, ignored by the NAT gateway. 这意味着默认情况下,NAT网关会忽略来自公共Internet的入站连接。 So if you try to open a socket to your friend's public IP, unless that gateway (modem/router) has been configured correctly, it will just silently ignore your connection attempt, and you'll get a timeout or "connection refused" type message. 因此,如果您尝试打开朋友公共IP的套接字,除非已正确配置该网关(调制解调器/路由器),否则它将无提示地忽略您的连接尝试,并且您将收到超时或“连接被拒绝”类型的消息。
There are a few techniques for getting around this, but most of them either require much more complicated application network code, or require the user to mess around in their router configuration pages (which casual users usually aren't willing to do). 有几种技术可以解决此问题,但是其中大多数要么需要更复杂的应用程序网络代码,要么需要用户在路由器配置页面中打乱(休闲用户通常不愿意这样做)。
You'd need to adopt one of these techniques if your clients are using NAT. 你需要采用这些技术,如果您的客户端使用NAT之一。 If one of your clients has a static IP directly connected to their computer and can accept incoming connections directly (without NAT), your program concept would work without modification, as long as the user behind the NAT initiates the connection to the one with a static IP. 如果您的一个客户端具有直接连接到其计算机的静态IP并可以直接接受传入连接(不使用NAT),则只要NAT背后的用户使用静态IP 发起 到该客户端的连接,您的程序概念就无需修改即可工作IP。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.