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


当前回答

单个端口可以有一个或多个连接不同外部IP的插座,如多个电源插座。

  TCP    192.168.100.2:9001     155.94.246.179:39255   ESTABLISHED     1312
  TCP    192.168.100.2:9001     171.25.193.9:61832     ESTABLISHED     1312
  TCP    192.168.100.2:9001     178.62.199.226:37912   ESTABLISHED     1312
  TCP    192.168.100.2:9001     188.193.64.150:40900   ESTABLISHED     1312
  TCP    192.168.100.2:9001     198.23.194.149:43970   ESTABLISHED     1312
  TCP    192.168.100.2:9001     198.49.73.11:38842     ESTABLISHED     1312

其他回答

似乎有很多答案将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).

它们是来自两个不同领域的术语:“端口”是TCP/IP网络的概念,“套接字”是API(编程)的东西。“套接字”是通过将端口和主机名或网络适配器组合成一个数据结构(可以用来发送或接收数据)而形成的(在代码中)。

简短的回答。

端口可以被描述为主机中的内部地址,用于标识程序或进程。

套接字可以被描述为一个编程接口,允许一个程序在internet上或本地与其他程序或进程通信。

套接字地址是一个IP地址和端口号

123.132.213.231         # IP address
               :1234    # port number
123.132.213.231:1234    # socket address

当两个套接字绑定在一起时,就发生了连接。

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)

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