这是我所在组织的一位软件工程师提出的问题。我感兴趣的是最广义的定义。


当前回答

尽可能简单地说,套接字和端口之间没有物理区别,例如PATA和SATA之间的区别。它们只是一些读写网卡的软件。

A port is essentially a public socket, some of which are well-known/well-accepted, the usual example being 80, dedicated to HTTP. Anyone who wants to exchange traffic using a certain protocol, HTTP in this instance, canonically goes to port 80. Of course, 80 is not physically dedicated to HTTP (it's not physically anything, it's just a number, a logical value), and could be used on some particular machine for some other protocol ad libitum, as long as those attempting to connect know which protocol (which could be quite private) to use.

套接字本质上是一个私有端口,为连接方知道但其他人不一定知道的特定目的而建立。底层传输层通常是TCP或UDP,但也不一定非得如此。最基本的特征是两端都知道发生了什么,不管发生了什么。

这里的关键是,当在某个端口上接收到连接请求时,应答握手包括有关为服务特定请求者而创建的套接字的信息。后续通信通过该(私有)套接字连接进行,而不是服务继续侦听连接请求的公共端口连接。

其他回答

套接字基本上是网络通信的端点,至少由一个ip地址和一个端口组成。在Java/ c#中,套接字是双向连接一侧的高级实现。

还有Java教程中的一个(非规范的)定义。

套接字由三部分组成:

IP地址 传输协议 端口号

端口是1到65535之间的数字,表示设备中的逻辑门。 客户端和服务器之间的每个连接都需要一个惟一的套接字。

例如:

1030为端口。 (10.1.1.2, TCP,端口1030)是一个套接字。

I know that there are lot of explanations. But, there is one more easy way to understand with practical example. We all can connect to HTTP port 80, but does it mean only one user can connect to that port at a time?. The answer is obviously 'no'. Multiple users for multiple purposes can access HTTP port 80 but they still get proper response they are waiting for, from the server, can't they?. Now think about it for a minute, how?. Yes you are correct, its IP address that uniquely identifies different users who contacts for different purposes. If you would have read the previous answers before reaching here, you would know that IP address is a part of information that socket consists. Think about it, is it possible to have a communication without sockets?. The answer is 'Yes' but you cannot run more than one application in a port but we know that we are not a 'Dump' switch that runs on just hardware.

在阅读了这些优秀的向上投票的答案后,我发现对于我这个网络编程新手来说,有以下几点需要强调:

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
...

从Oracle Java教程:

套接字是网络上运行的两个程序之间双向通信链路的一个端点。套接字与端口号绑定,以便TCP层可以识别数据要发送到的应用程序。