这是我所在组织的一位软件工程师提出的问题。我感兴趣的是最广义的定义。
当前回答
端口表示IP网络协议的TCP和UDP传输中的通信端点。套接字是通信端点的软件抽象,通常用于这些协议的实现(套接字API)。另一种实现是XTI/TLI API。
参见:
史蒂文斯,W. R. 1998, UNIX网络编程:网络api:套接字和XTI;第一卷,普伦蒂斯大厅。 Stevens, W. R., 1994, TCP/IP图解,第1卷:协议,Addison-Wesley。
其他回答
套接字是一种数据I/O机制。端口是通信协议的契约概念。套接字可以没有端口而存在。一个端口可以没有特定的套接字而存在(例如,如果同一个端口上有几个活动的套接字,这可能是某些协议允许的)。
端口用于确定接收端应该将数据包路由到哪个套接字,在许多协议中,但它并不总是必需的,接收套接字的选择可以通过其他方式完成——端口完全是网络子系统中协议处理程序使用的工具。例如,如果协议不使用端口,数据包可以到所有侦听套接字或任何套接字。
套接字由三部分组成:
IP地址 传输协议 端口号
端口是1到65535之间的数字,表示设备中的逻辑门。 客户端和服务器之间的每个连接都需要一个惟一的套接字。
例如:
1030为端口。 (10.1.1.2, TCP,端口1030)是一个套接字。
A socket is a communication endpoint. A socket is not directly related to the TCP/IP protocol family, it can be used with any protocol your system supports. The C socket API expects you to first get a blank socket object from the system that you can then either bind to a local socket address (to directly retrieve incoming traffic for connection-less protocols or to accept incoming connection requests for connection-oriented protocols) or that you can connect to a remote socket address (for either kind of protocol). You can even do both if you want to control both, the local socket address a socket is bound to and the remote socket address a socket is connected to. For connection-less protocols connecting a socket is even optional but if you don't do that, you'll have to also pass the destination address with every packet you want to send over the socket as how else would the socket know where to send this data to? Advantage is that you can use a single socket to send packets to different socket addresses. Once you have your socket configured and maybe even connected, consider it to be a bi-directional communication pipe. You can use it to pass data to some destination and some destination can use it to pass data back to you. What you write to a socket is send out and what has been received is available for reading.
Ports on the other hand are something that only certain protocols of the TCP/IP protocol stack have. TCP and UDP packets have ports. A port is just a simple number. The combination of source port and destination port identify a communication channel between two hosts. E.g. you may have a server that shall be both, a simple HTTP server and a simple FTP server. If now a packet arrives for the address of that server, how would it know if that is a packet for the HTTP or the FTP server? Well, it will know so as the HTTP server will run on port 80 and the FTP server on port 21, so if the packet arrives with a destination port 80, it is for the HTTP server and not for the FTP server. Also the packet has a source port since without such a source port, a server could only have one connection to one IP address at a time. The source port makes it possible for a server to distinguish otherwise identical connections: they all have the same destination port, e.g. port 80, the same destination IP (the IP of the server), and the same source IP, as they all come from the same client, but as they have different source ports, the server can distinguish them from each other. And when the server sends back replies, it will do so to the port the request came from, that way the client can also distinguish different replies it receives from the same server.
A socket represents a single connection between two network applications. These two applications nominally run on different computers, but sockets can also be used for interprocess communication on a single computer. Applications can create multiple sockets for communicating with each other. Sockets are bidirectional, meaning that either side of the connection is capable of both sending and receiving data. Therefore a socket can be created theoretically at any level of the OSI model from 2 upwards. Programmers often use sockets in network programming, albeit indirectly. Programming libraries like Winsock hide many of the low-level details of socket programming. Sockets have been in widespread use since the early 1980s.
端口表示网络通信的端点或“通道”。端口号允许同一计算机上的不同应用程序在不相互干扰的情况下利用网络资源。端口号最常出现在网络编程中,尤其是套接字编程中。但是,有时端口号对普通用户是可见的。例如,一个人在因特网上访问的一些网站使用如下URL:
http://www.mairie-metz.fr:8080/在本例中,数字8080指Web浏览器连接到Web服务器所使用的端口号。通常,Web站点使用端口号80,该端口号不需要包含在URL中(尽管可以包含)。
在IP组网中,端口号理论上可以在0到65535之间。但是,大多数流行的网络应用程序使用范围较低的端口号(例如HTTP的80)。
注意:术语端口还指网络技术的其他几个方面。端口可以指外部设备的物理连接点,如串口、并口和USB端口。术语端口也指某些以太网连接点,例如集线器、交换机或路由器上的连接点。
ref http://compnetworking.about.com/od/basicnetworkingconcepts/l/bldef_port.htm
ref http://compnetworking.about.com/od/itinformationtechnology/l/bldef_socket.htm
在阅读了这些优秀的向上投票的答案后,我发现对于我这个网络编程新手来说,有以下几点需要强调:
TCP-IP连接是连接一个地址:端口组合和另一个地址:端口组合的双向路径。因此,每当您打开从本地计算机到远程服务器上的端口的连接(例如www.google.com:80)时,您也将计算机上的一个新端口号与该连接关联起来,以允许服务器将内容发回给您(例如127.0.0.1:65234)。使用netstat查看你机器的连接是很有帮助的:
> netstat -nWp tcp (on OS X)
Active Internet connections
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp4 0 0 192.168.0.6.49871 17.172.232.57.5223 ESTABLISHED
...
推荐文章
- 在套接字编程中AF_INET和PF_INET的区别是什么?
- read()和recv(), send()和write()之间有什么区别?
- mysqld_safe UNIX套接字文件目录“/var/run/mysqld”不存在
- 如何解决“java.net.BindException: Address already in use: JVM_Bind”错误?
- 增加Linux中TCP/IP连接的最大数量
- 模拟慢速网络连接的网络工具
- 如何打开“谷歌计算引擎”中的特定端口(如9090)
- socket和websocket的区别?
- Android检查网络连接
- 465端口和587端口有什么区别?
- 什么是AF_INET,为什么我需要它?
- Kubernetes Service定义中targetPort和port的区别
- 停止node.js服务器的所有实例
- 现代Linux机顶盒理论上最大的TCP连接数是多少
- Linux上的网络使用top/htop