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


当前回答

In a broad sense, Socket - is just that, a socket, just like your electrical, cable or telephone socket. A point where "requisite stuff" (power, signal, information) can go out and come in from. It hides a lot of detailed stuff, which is not required for the use of the "requisite stuff". In software parlance, it provides a generic way of defining a mechanism of communication between two entities (those entities could be anything - two applications, two physically separate devices, User & Kernel space within an OS, etc)

端口是端点标识符。它区分一个端点和另一个端点。在网络级别,它将一个应用程序与另一个应用程序区分开来,以便网络堆栈可以将信息传递给适当的应用程序。

其他回答

In a broad sense, Socket - is just that, a socket, just like your electrical, cable or telephone socket. A point where "requisite stuff" (power, signal, information) can go out and come in from. It hides a lot of detailed stuff, which is not required for the use of the "requisite stuff". In software parlance, it provides a generic way of defining a mechanism of communication between two entities (those entities could be anything - two applications, two physically separate devices, User & Kernel space within an OS, etc)

端口是端点标识符。它区分一个端点和另一个端点。在网络级别,它将一个应用程序与另一个应用程序区分开来,以便网络堆栈可以将信息传递给适当的应用程序。

似乎有很多答案将socket等同于2台PC之间的连接。我认为这是完全错误的。套接字一直是一台PC上的端点,可能连接也可能不连接-当然我们都在某些时候使用侦听器或UDP套接字*。重要的部分是它是可寻址的和活跃的。向1.1.1.1:1234发送消息不太可能起作用,因为没有为该端点定义套接字。

套接字是特定于协议的-因此,TCP/IP和UDP/IP都使用* (ipaddress:port)的唯一性实现与eg不同。, IPX (Network, Node, and…嗯哼,套接字——但是一个不同的套接字是指一般的“套接字”术语。IPX套接字号相当于IP端口)。但是,它们都提供了唯一的可寻址端点。

由于IP已成为主导协议,端口(在网络术语中)已成为UDP或TCP端口号的同义词——后者是套接字地址的一部分。

UDP is connection-less - meaning no virtual circuit between the 2 endpoints is ever created. However, we still refer to UDP sockets as the endpoint. The API functions make it clear that both are just different type of sockets - SOCK_DGRAM is UDP (just sending a message) and SOCK_STREAM is TCP (creating a virtual circuit). Technically, the IP header holds the IP Address, and the protocol on top of IP (UDP or TCP) holds the port number. This makes it possible to have other protocols (eg. ICMP that have no port numbers, but do have IP addressing information).

端口表示IP网络协议的TCP和UDP传输中的通信端点。套接字是通信端点的软件抽象,通常用于这些协议的实现(套接字API)。另一种实现是XTI/TLI API。

参见:

史蒂文斯,W. R. 1998, UNIX网络编程:网络api:套接字和XTI;第一卷,普伦蒂斯大厅。 Stevens, W. R., 1994, TCP/IP图解,第1卷:协议,Addison-Wesley。

从Oracle Java教程:

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

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

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