Documentation

The Java™ Tutorials
Hide TOC
Networking Basics网络基础
Trail: Custom Networking
Lesson: Overview of Networking网络概述

Networking Basics网络基础

Computers running on the Internet communicate to each other using either the Transmission Control Protocol (TCP) or the User Datagram Protocol (UDP), as this diagram illustrates:在Internet上运行的计算机使用传输控制协议(TCP)或用户数据报协议(UDP)相互通信,如下图所示:

Example of network communication.

When you write Java programs that communicate over the network, you are programming at the application layer. 当您编写通过网络进行通信的Java程序时,您是在应用层编程。Typically, you don't need to concern yourself with the TCP and UDP layers. 通常,您不需要关心TCP和UDP层。Instead, you can use the classes in the java.net package. 相反,您可以使用java.net包中的类。These classes provide system-independent network communication. 这些类提供独立于系统的网络通信。However, to decide which Java classes your programs should use, you do need to understand how TCP and UDP differ.然而,要决定程序应该使用哪些Java类,您确实需要了解TCP和UDP的区别。

TCP

When two applications want to communicate to each other reliably, they establish a connection and send data back and forth over that connection. 当两个应用程序希望彼此可靠地通信时,它们会建立连接并通过该连接来回发送数据。This is analogous to making a telephone call. 这类似于打电话。If you want to speak to Aunt Beatrice in Kentucky, a connection is established when you dial her phone number and she answers. 如果你想和肯塔基州的比阿特丽斯阿姨通话,当你拨她的电话号码,她接电话时,就会建立连接。You send data back and forth over the connection by speaking to one another over the phone lines. 您通过电话线彼此通话,通过连接来回发送数据。Like the phone company, TCP guarantees that data sent from one end of the connection actually gets to the other end and in the same order it was sent. 与电话公司一样,TCP保证从连接的一端发送的数据实际到达另一端,并且发送顺序与发送顺序相同。Otherwise, an error is reported.否则,将报告错误。

TCP provides a point-to-point channel for applications that require reliable communications. TCP为需要可靠通信的应用程序提供点对点通道。The Hypertext Transfer Protocol (HTTP), File Transfer Protocol (FTP), and Telnet are all examples of applications that require a reliable communication channel. 超文本传输协议(HTTP)、文件传输协议(FTP)和Telnet都是需要可靠通信通道的应用程序的示例。The order in which the data is sent and received over the network is critical to the success of these applications. 通过网络发送和接收数据的顺序对于这些应用程序的成功至关重要。When HTTP is used to read from a URL, the data must be received in the order in which it was sent. 当使用HTTP从URL读取数据时,必须按照发送顺序接收数据。Otherwise, you end up with a jumbled HTML file, a corrupt zip file, or some other invalid information.否则,您将得到一个混乱的HTML文件、一个损坏的zip文件或一些其他无效信息。


Definition:定义: 

TCP (Transmission Control Protocol) is a connection-based protocol that provides a reliable flow of data between two computers.TCP传输控制协议)是一种基于连接的协议,可在两台计算机之间提供可靠的数据流。


UDP

The UDP protocol provides for communication that is not guaranteed between two applications on the network. UDP协议提供网络上两个应用程序之间无法保证的通信。UDP is not connection-based like TCP. UDP不像TCP那样基于连接。Rather, it sends independent packets of data, called datagrams, from one application to another. 相反,它从一个应用程序向另一个应用程序发送独立的数据包,称为数据报Sending datagrams is much like sending a letter through the postal service: The order of delivery is not important and is not guaranteed, and each message is independent of any other.发送数据报很像通过邮政服务发送信件:递送顺序不重要,也不保证,每条消息都是独立的。


Definition:定义: 

UDP (User Datagram Protocol用户数据报协议) is a protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival. 是一种协议,它将称为数据报的独立数据包从一台计算机发送到另一台计算机,而不保证到达。UDP is not connection-based like TCP.UDP不像TCP那样基于连接。


For many applications, the guarantee of reliability is critical to the success of the transfer of information from one end of the connection to the other. 对于许多应用来说,可靠性的保证对于成功地将信息从连接的一端传输到另一端至关重要。However, other forms of communication don't require such strict standards. 然而,其他形式的交流不需要如此严格的标准。In fact, they may be slowed down by the extra overhead or the reliable connection may invalidate the service altogether.事实上,额外的开销可能会降低它们的速度,或者可靠的连接可能会使服务完全失效。

Consider, for example, a clock server that sends the current time to its client when requested to do so. 例如,考虑一个时钟服务器,它在客户机请求时将当前时间发送给客户机。If the client misses a packet, it doesn't really make sense to resend it because the time will be incorrect when the client receives it on the second try. 如果客户端错过了一个数据包,那么重新发送它是没有意义的,因为客户端在第二次尝试时收到数据包的时间不正确。If the client makes two requests and receives packets from the server out of order, it doesn't really matter because the client can figure out that the packets are out of order and make another request. 如果客户端发出两个请求,并且从服务器接收到的数据包出现故障,这其实并不重要,因为客户端可以发现数据包出现故障,然后发出另一个请求。The reliability of TCP is unnecessary in this instance because it causes performance degradation and may hinder the usefulness of the service.在这种情况下,TCP的可靠性是不必要的,因为它会导致性能下降,并可能会妨碍服务的有用性。

Another example of a service that doesn't need the guarantee of a reliable channel is the ping command. 另一个不需要可靠通道保证的服务示例是ping命令。The purpose of the ping command is to test the communication between two programs over the network. ping命令的目的是测试网络上两个程序之间的通信。In fact, ping needs to know about dropped or out-of-order packets to determine how good or bad the connection is. 事实上,ping需要知道丢失或无序的数据包,以确定连接的好坏。A reliable channel would invalidate this service altogether.一个可靠的渠道将使这项服务完全失效。

The UDP protocol provides for communication that is not guaranteed between two applications on the network. UDP协议提供网络上两个应用程序之间无法保证的通信。UDP is not connection-based like TCP. Rather, it sends independent packets of data from one application to another. UDP不像TCP那样基于连接。相反,它从一个应用程序向另一个应用程序发送独立的数据包。Sending datagrams is much like sending a letter through the mail service: The order of delivery is not important and is not guaranteed, and each message is independent of any others.发送数据报很像通过邮件服务发送信件:发送顺序并不重要,也不保证,每条消息都独立于其他消息。


Note: 

Many firewalls and routers have been configured not to allow UDP packets. 许多防火墙和路由器已配置为不允许UDP数据包。If you're having trouble connecting to a service outside your firewall, or if clients are having trouble connecting to your service, ask your system administrator if UDP is permitted.如果您无法连接到防火墙外的服务,或者客户端无法连接到您的服务,请询问系统管理员是否允许UDP。


Understanding Ports了解端口

Generally speaking, a computer has a single physical connection to the network. 一般来说,计算机与网络只有一个物理连接。All data destined for a particular computer arrives through that connection. 所有发送到特定计算机的数据都通过该连接到达。However, the data may be intended for different applications running on the computer. 但是,这些数据可能用于计算机上运行的不同应用程序。So how does the computer know to which application to forward the data? 那么,计算机如何知道将数据转发到哪个应用程序呢?Through the use of ports.通过使用端口

Data transmitted over the Internet is accompanied by addressing information that identifies the computer and the port for which it is destined. 通过互联网传输的数据伴随着地址信息,这些信息可以识别计算机及其目的地的端口。The computer is identified by its 32-bit IP address, which IP uses to deliver data to the right computer on the network. 计算机由其32位IP地址标识,IP使用该地址将数据传送到网络上的正确计算机。Ports are identified by a 16-bit number, which TCP and UDP use to deliver the data to the right application.端口由16位数字标识,TCP和UDP使用该数字将数据传送到正确的应用程序。

In connection-based communication such as TCP, a server application binds a socket to a specific port number. 在基于连接的通信(如TCP)中,服务器应用程序将套接字绑定到特定的端口号。This has the effect of registering the server with the system to receive all data destined for that port. 这样做的效果是向系统注册服务器,以接收发送到该端口的所有数据。A client can then rendezvous with the server at the server's port, as illustrated here:然后,客户机可以在服务器端口与服务器会合,如下所示:

A client communicating to a server through its port

Definition: 

The TCP and UDP protocols use ports to map incoming data to a particular process running on a computer.TCP和UDP协议使用端口将传入数据映射到计算机上运行的特定进程。


In datagram-based communication such as UDP, the datagram packet contains the port number of its destination and UDP routes the packet to the appropriate application, as illustrated in this figure:在基于数据报的通信(如UDP)中,数据报数据包包含其目的地的端口号,UDP将数据包路由到适当的应用程序,如下图所示:

Routing the packet to the appropriate application.

Port numbers range from 0 to 65,535 because ports are represented by 16-bit numbers. 端口号的范围从0到65535,因为端口由16位数字表示。The port numbers ranging from 0 - 1023 are restricted; they are reserved for use by well-known services such as HTTP and FTP and other system services. 0-1023之间的端口号受到限制;它们保留供HTTP和FTP等知名服务以及其他系统服务使用。These ports are called well-known ports. 这些端口称为知名端口Your applications should not attempt to bind to them.应用程序不应尝试绑定到它们。

Networking Classes in the JDKJDK中的网络类

Through the classes in java.net, Java programs can use TCP or UDP to communicate over the Internet. 通过java.net中的类,Java程序可以使用TCP或UDP在Internet上进行通信。The URL, URLConnection, Socket, and ServerSocket classes all use TCP to communicate over the network. URLURLConnectionSocketServerSocket类都使用TCP通过网络进行通信。The DatagramPacket, DatagramSocket, and MulticastSocket classes are for use with UDP.DatagramPacketDatagramSocketMulticastSocket类用于UDP。


Previous page: What You May Already Know About Networking in Java
Next page: Working with URLs