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


当前回答

这个问题已经有了理论上的答案。我想举一个实际的例子来解释这个问题,让大家对Socket和Port有一个更清晰的理解。

我在这里找到的

This example will walk you thru the process of connecting to a website, such as Wiley. You would open your web browser (like Mozilla Firefox) and type www.wiley.com into the address bar. Your web browser uses a Domain Name System (DNS) server to look up the name www.wiley.com to identify its IP address is. For this example, the address is 192.0.2.100. Firefox makes a connection to the 192.0.2.100 address and to the port where the application layer web server is operating. Firefox knows what port to expect because it is a well-known port . The well-known port for a web server is TCP port 80. The destination socket that Firefox attempts to connect is written as socket:port, or in this example, 192.0.2.100:80. This is the server side of the connect, but the server needs to know where to send the web page you want to view in Mozilla Firefox, so you have a socket for the client side of the connection also. The client side connection is made up of your IP address, such as 192.168.1.25, and a randomly chosen dynamic port number. The socket associated with Firefox looks like 192.168.1.25:49175. Because web servers operate on TCP port 80, both of these sockets are TCP sockets, whereas if you were connecting to a server operating on a UDP port, both the server and client sockets would be UDP sockets.

其他回答

套接字是一种特殊类型的文件句柄,进程使用它从操作系统请求网络服务。 套接字地址是三元组: {protocol, local-address, local-process},其中本地进程由端口号标识。

在TCP/IP套件中,例如:

{tcp, 193.44.234.3, 12345}

会话是两个进程之间的通信链接,从而描述了两者之间的关联。 关联是一个5元组,它完全指定了组成连接的两个进程: {protocol, local-address, local-process, foreign-address, foreign-process}

在TCP/IP套件中,例如:

{tcp, 193.44.234.3, 1500, 193.44.234.5, 21}

可能是一个有效的关联。

半关联是: {protocol, local-address, local-process}

or

{protocol, foreign-address, foreign-process}

它们指定连接的每一半。

半关联也称为套接字或传输地址。也就是说,套接字是可以在网络中命名和寻址的通信端点。 套接字接口是通信协议的几个应用程序编程接口(api)之一。它被设计为一个通用的通信编程接口,最初由4.2BSD UNIX系统引入。虽然还没有标准化,但已经成为事实上的行业标准。

套接字= IP地址+端口(数字地址) 它们一起标识机器上网络连接的端点。(我刚刚是不是网络基础课不及格?)

Socket is an abstraction provided by kernel to user applications for data I/O. A socket type is defined by the protocol it's handling, an IPC communication etc. So if somebody creates a TCP socket he can do manipulations like reading data to socket and writing data to it by simple methods and the lower level protocol handling like TCP conversions and forwarding packets to lower level network protocols is done by the particular socket implementation in the kernel. The advantage is that user need not worry about handling protocol specific nitigrities and should just read and write data to socket like a normal buffer. Same is true in case of IPC, user just reads and writes data to socket and kernel handles all lower level details based on the type of socket created.

端口和IP一起就像给套接字提供一个地址,虽然不是必需的,但它有助于网络通信。

从Oracle Java教程:

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

端口是网络协议用来访问连接的主机的实体。端口可以是特定于应用程序的,也可以是与特定通信媒介相关的。不同的协议使用不同的端口访问主机,如HTTP使用80端口或FTP使用23端口。您可以在应用程序中分配用户定义的端口号,但这些端口号应该大于1023。

端口打开到所需主机的连接,而套接字是网络间或进程间通信的端点。 套接字是由系统通过api(应用程序编程接口)分配的。

更微妙的区别是,当系统重新启动时,端口将出现,而套接字将被销毁。