[the java tutorials]All About Sockets

来源:互联网 发布:诡异失踪知乎 编辑:程序博客网 时间:2024/05/10 19:26

Lesson: All About Sockets

URLs and URLConnections provide a relatively high-level mechanism for accessing resources on the Internet. 

URLs和URLConnections提供一个相对高等级机制给在Internet上面访问资源.

Sometimes your programs require lower-level network communication, 

有些时候你的程序请求低等级的网络连接,

for example, when you want to write a client-server application.

例如,当你要写一个客户端/服务器程序.

In client-server applications, the server provides some service, 

在客户端/服务器程序中,服务器提供一些服务,

such as processing database queries or sending out current stock prices. 

例如访问数据库查询或者发送当前的股票价格.

The client uses the service provided by the server, 

客户端使用的服务假如是由服务器提供,

either displaying database query results to the user or making stock purchase recommendations to an investor. 

显示数据库查询结果给用户或者做一个股票收购建议给投资者.

The communication that occurs between the client and the server must be reliable. 

客户端与服务器之间链接必须是可靠地.

That is, no data can be dropped and it must arrive on the client side in the same order in which the server sent it.

也就是说,不能丢掉任何数据而且它必须以服务器发送的相同的顺序到达客户端.

TCP provides a reliable, point-to-point communication channel that client-server applications on the Internet use to 

TCP提供一个可靠地,点对点的链接通道,它可以使client和server应用程序在Interner互相连接.

communicate with each other. 

To communicate over TCP, a client program and a server program establish a connection to one another. 

通过TCP的链接,客户端程序与一个服务器程序建立一个链接.

Each program binds a socket to its end of the connection. 

每一个程序绑定一个socket给它的链接末端.

To communicate, the client and the server each reads from and writes to the socket bound to the connection.

就是这样交流,客户端和服务器每次从绑定到这个链接的socket读和写.

What Is a Socket?

什么是Socket?

A socket is one end-point of a two-way communication link between two programs running on the network. 

一个socket是在运行在网络上两个程序之间之间交流链接双向通道的末端.

Socket classes are used to represent the connection between a client program and a server program. 

Socket类们被用来表现客户端和服务器之间的链接.

The java.net package provides two classes--Socket and ServerSocket--that implement the client side of the connection and the server side of the connection, respectively.

java.net包提供2个类Socket和ServerSocket分别实现客户端的连接和服务器端的链接.

Reading from and Writing to a Socket

读/写一个Socket.

This page contains a small example that illustrates how a client program can read from and write to a socket.

这个页面包好一个小例子它阐述了一个客户端程序读写socket.

Writing a Client/Server Pair

写一对客户端和服务器.

The previous page showed an example of how to write a client program that interacts with an existing server via a Socket object. 

之前的页面展示一个怎么写一个通过Socket对象与一个存在的服务器交流的客户端程序实例。

This page shows you how to write a program that implements the other side of the connection--a server program.

这个页面展示给你如何去写一个服务器程序.

文章来源:

http://docs.oracle.com/javase/tutorial/networking/sockets/

原创粉丝点击