简体   繁体   English

Java-通过套接字进行通信

[英]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). 有几种技术可以解决此问题,但是其中大多数要么需要更复杂的应用程序网络代码,要么需要用户在路由器配置页面中打乱(休闲用户通常不愿意这样做)。

  • Port forwarding : Users must manually configure their router to "forward" public IP ports to a specific private IP, thus allowing the connection attempt on the public IP to pass through the Network Address Translation table to the private LAN. 端口转发 :用户必须手动配置其路由器,以将公共IP端口“转发”到特定的私有IP,从而允许对公共IP的连接尝试通过 “网络地址转换”表传递到私有LAN。 There is no general-purpose way of automating this without user intervention due to the differences between router/gateway devices. 由于路由器/网关设备之间的差异,没有通用方法可以自动执行此操作而无需用户干预。
  • NAT Traversal , which requires one of a variety of NAT-T protocols to be implemented in your program, and requires the explicit support of the desired NAT-T protocol for the routers involved (yours and your friend's). NAT遍历 ,它需要在程序中实现多种NAT-T协议之一, 并且需要所涉及的路由器(您和您的朋友的)明确支持所需的NAT-T协议。 There are loads of different NAT-T protocols available; 有许多不同的NAT-T协议可用。 it'd be up to you to determine which one to use, based on compatibility, availability, etc. 您需要根据兼容性,可用性等来决定使用哪个。
  • Using an interposing server as an indirect way of routing packets between the two: In this case, you create a server on the public Internet that does have a unique IP (or has ports forwarded to it), and have each of your two client computers initiate an outbound connection to that server. 使用中介服务器作为间接在两者之间路由数据包的方式:在这种情况下,您在公用Internet上创建一个服务器,该服务器确实具有唯一的IP(或具有转发到其的端口),并且拥有两台客户端计算机中的每台启动到该服务器的出站连接。 The server would accept connections from both clients, then process the packets and pass them along, like talking to someone through a middleman. 该服务器将接受来自客户端的连接,然后再处理数据包,并通过他们,想通过中间人与人交谈。 This doesn't require any special configuration on the clients, as long as they can make outbound connections on the desired ports (by default, most can, except in restricted enterprise environments where your ports may be limited to a few like 80 and 443.) 这不需要在客户端上进行任何特殊配置,只要它们可以在所需的端口上进行出站连接(默认情况下,大多数可以,除非在受限的企业环境中,您的端口可能仅限于80和443之类的少数端口)。 )

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.

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