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


当前回答

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

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

其他回答

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.

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.

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

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

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

端口是最简单的部分,它只是套接字的唯一标识符。套接字是进程可以用来建立连接和相互通信的东西。高个子杰夫有一个很好的电话比喻,但并不完美,所以我决定修正它:

IP和端口~电话号码 插座~电话设备 连接~电话 建立连接~拨打号码 流程,远程应用程序~人员 留言~语音

套接字允许在单个应用程序中两个应用程序之间进行通信 机器还是两台机器。实际上它就像门。如果门开了,就可以 是门内进程或应用程序之间的连接 在门外。

套接字有4种类型:

流套接字 数据报套接字 原始套接字 顺序数据包套接字。

套接字主要用于客户机-服务器应用程序。端口标识网络地址上的不同端点。它包含一个数值。总的来说,套接字是端口和网络地址的组合。