[翻译]What Is a Socket?

What Is a Socket?

什么是 Socket(套接字)?

Normally, a server runs on a specific computer and has a socket that is bound to a specific port number. 

通常,一个服务器运行在一个具体的计算机然后有一个套接字被绑定到一个具体的端口号.

The server just waits, listening to the socket for a client to make a connection request.

这个服务器光等待,监听这个为一个客户端建立一个链接的socket.

On the client-side: The client knows the hostname of the machine on which the server is running and the port 

在客户端:这个客户端知道正在运行服务器机器的主机名和服务器正在监听的端口号.

number on which the server is listening. 

To make a connection request, the client tries to rendezvous with the server on the server's machine and port.

去建立一个链接请求,这个客户端尝试去和服务器会和在服务机器和端口上。

The client also needs to identify itself to the server so it binds to a local port number that it will use during this connection. 

客户端也需要标记它自己给服务器,所以它绑定一个本地的端口号将会在这个链接中使用.

This is usually assigned by the system.

这个通常通过系统分配.

A client's connection request

If everything goes well, the server accepts the connection. 

如果每件事都顺利,这个服务器获得这个链接.

Upon acceptance, the server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client. 

受理,服务器获得一个新的套接字绑定到这个同样的本地端口然后也有远程的端点设置到这个地址和客户单的端口.

It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.

它需要一个新的socket这样它可以继续监听这个原始socket为链接请求与此同时维护已经链接的客户端.

The connection is made

On the client side, if the connection is accepted, 

在这个客户端,如果链接被接受,

a socket is successfully created and the client can use the socket to communicate with the server.

一个socket成功的创建然后这个client能使用socket与服务器交流.

The client and server can now communicate by writing to or reading from their sockets.

这个客户端/服务器可以通过写和读它们的socket进行交流.


Definition: 
定义:

socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent.


An endpoint is a combination of an IP address and a port number. 

一个终端是一个ip地址和端口号的组合.

Every TCP connection can be uniquely identified by its two endpoints. 

每一个tcp连接可以被唯一的标记通过它的两个终端.

That way you can have multiple connections between your host and the server.

你可以有多个连接在你的主机和服务器之间.

The java.net package in the Java platform provides a class, Socket, that implements one side of a two-way connection between your Java program and another program on the network. 

这个java.net包在java平台提供一个class,Socket,它实现在网络上一个java程序与另外一个java程序之间的连接的一端.

The Socket class sits on top of a platform-dependent implementation, 

Soket类放置在平台依赖实现的上面,

hiding the details of any particular system from your Java program. 

从你的java程序中隐藏任何特殊的系统的细节.

By using the java.net.Socket class instead of relying on native code, 

通过使用java.net.Socket类取代依赖本地的代码,

your Java programs can communicate over the network in a platform-independent fashion.

你的java程序可以在流行的依赖平台上进行网落传输.

Additionally, java.net includes the ServerSocket class, 

此外,java.net包含这个ServerSocket类,

which implements a socket that servers can use to listen for and accept connections to clients. 

它实现服务器可以去监听和获得客户端连接的套接字.

This lesson shows you how to use the Socket and ServerSocket classes.

这借口展示你如何使用这个Socket和ServerSocket类.

If you are trying to connect to the Web, 

如果尝试去连接web,

the URL class and related classes (URLConnection,URLEncoder) are probably more appropriate than the socket classes. 

这个URL类和相关的类(URLConnection,URLEncoder)可能比socket类更为适合.

In fact, URLs are a relatively high-level connection to the Web and use sockets as part of the 

underlying implementation. See Working with URLs for information about connecting to the Web via URLs.

实际上,URL是一个相对高等级的链接对于Web和使用socket作为底层实现一部分.看Working with URLs得到更多关于通过URLs链接到web。

原文:http://docs.oracle.com/javase/tutorial/networking/sockets/definition.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值