Unix Sockets 简明教程
Unix Socket - Ports and Services
当客户端进程想连接服务器时,客户端必须有一种方法来识别它想要连接的服务器。如果客户端知道驻留服务器的主机的 32 位互联网地址,它就可以联系该主机。但是客户端如何识别该主机上正在运行的特定服务器进程?
When a client process wants to a connect a server, the client must have a way of identifying the server that it wants to connect. If the client knows the 32-bit Internet address of the host on which the server resides, it can contact that host. But how does the client identify the particular server process running on that host?
为了解决在主机上识别特定服务器进程的问题,TCP 和 UDP 都定义了一组众所周知端口。
To resolve the problem of identifying a particular server process running on a host, both TCP and UDP have defined a group of well-known ports.
就我们的目的而言,端口将被定义为 1024 到 65535 之间的整数。这是因为小于 1024 的所有端口号都被认为是众所周知的——例如,Telnet 使用端口 23,http 使用 80,ftp 使用 21,依此类推。
For our purpose, a port will be defined as an integer number between 1024 and 65535. This is because all port numbers smaller than 1024 are considered well-known — for example, telnet uses port 23, http uses 80, ftp uses 21, and so on.
网络服务的端口分配可以在文件 /etc/services 中找到。如果您要编写自己的服务器,则必须小心为服务器分配一个端口。您应该确保此端口未分配给任何其他服务器。
The port assignments to network services can be found in the file /etc/services. If you are writing your own server then care must be taken to assign a port to your server. You should make sure that this port should not be assigned to any other server.
通常做法是分配大于 5000 的任何端口号。但有许多组织编写的服务器具有大于 5000 的端口号。例如,Yahoo Messenger 在 5050 上运行,SIP 服务器在 5060 上运行,等等。
Normally it is a practice to assign any port number more than 5000. But there are many organizations who have written servers having port numbers more than 5000. For example, Yahoo Messenger runs on 5050, SIP Server runs on 5060, etc.
Example Ports and Services
以下是一份服务和关联端口的小列表。您可以在 IANA - TCP/IP Port Assignments 找到最更新的互联网端口和关联服务列表。
Here is a small list of services and associated ports. You can find the most updated list of internet ports and associated service at IANA - TCP/IP Port Assignments.
Service |
Port Number |
Service Description |
echo |
7 |
UDP/TCP sends back what it receives. |
discard |
9 |
UDP/TCP throws away input. |
daytime |
13 |
UDP/TCP returns ASCII time. |
chargen |
19 |
UDP/TCP returns characters. |
ftp |
21 |
TCP file transfer. |
telnet |
23 |
TCP remote login. |
smtp |
25 |
TCP email. |
daytime |
37 |
UDP/TCP returns binary time. |
tftp |
69 |
UDP trivial file transfer. |
finger |
79 |
TCP info on users. |
http |
80 |
TCP World Wide Web. |
login |
513 |
TCP remote login. |
who |
513 |
UDP different info on users. |
Xserver |
6000 |
TCP X windows (N.B. >1023). |
Port and Service Functions
Unix 提供下列函数用于从 /etc/services 文件中获取服务名称。
Unix provides the following functions to fetch service name from the /etc/services file.
-
struct servent *getservbyname(char *name, char *proto) − This call takes service name and protocol name, and returns the corresponding port number for that service.
-
struct servent *getservbyport(int port, char *proto) − This call takes port number and protocol name, and returns the corresponding service name.
每个函数的返回值都是一个指向具有下列形式的结构的指针 −
The return value for each function is a pointer to a structure with the following form −
struct servent {
char *s_name;
char **s_aliases;
int s_port;
char *s_proto;
};
下面是成员字段的说明 −
Here is the description of the member fields −
Attribute |
Values |
Description |
s_name |
http |
It is the official name of the service. For example, SMTP, FTP POP3, etc. |
s_aliases |
ALIAS |
It holds the list of service aliases. Most of the time, it will be set to NULL. |
s_port |
80 |
It will have the associated port number. For example, for HTTP, it will be 80. |
s_proto |
TCP UDP |
It is set to the protocol used. Internet services are provided using either TCP or UDP. |