这是我所在组织的一位软件工程师提出的问题。我感兴趣的是最广义的定义。
当前回答
在阅读了这些优秀的向上投票的答案后,我发现对于我这个网络编程新手来说,有以下几点需要强调:
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
...
其他回答
A socket represents a single connection between two network applications. These two applications nominally run on different computers, but sockets can also be used for interprocess communication on a single computer. Applications can create multiple sockets for communicating with each other. Sockets are bidirectional, meaning that either side of the connection is capable of both sending and receiving data. Therefore a socket can be created theoretically at any level of the OSI model from 2 upwards. Programmers often use sockets in network programming, albeit indirectly. Programming libraries like Winsock hide many of the low-level details of socket programming. Sockets have been in widespread use since the early 1980s.
端口表示网络通信的端点或“通道”。端口号允许同一计算机上的不同应用程序在不相互干扰的情况下利用网络资源。端口号最常出现在网络编程中,尤其是套接字编程中。但是,有时端口号对普通用户是可见的。例如,一个人在因特网上访问的一些网站使用如下URL:
http://www.mairie-metz.fr:8080/在本例中,数字8080指Web浏览器连接到Web服务器所使用的端口号。通常,Web站点使用端口号80,该端口号不需要包含在URL中(尽管可以包含)。
在IP组网中,端口号理论上可以在0到65535之间。但是,大多数流行的网络应用程序使用范围较低的端口号(例如HTTP的80)。
注意:术语端口还指网络技术的其他几个方面。端口可以指外部设备的物理连接点,如串口、并口和USB端口。术语端口也指某些以太网连接点,例如集线器、交换机或路由器上的连接点。
ref http://compnetworking.about.com/od/basicnetworkingconcepts/l/bldef_port.htm
ref http://compnetworking.about.com/od/itinformationtechnology/l/bldef_socket.htm
它们是来自两个不同领域的术语:“端口”是TCP/IP网络的概念,“套接字”是API(编程)的东西。“套接字”是通过将端口和主机名或网络适配器组合成一个数据结构(可以用来发送或接收数据)而形成的(在代码中)。
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协议上。但是:
NO - Socket is not related to a two-endpoint communication. It's the local endpoint, which can or cannot be connected on the other side (Think about a server socket listening for incoming connection) NO - Socket it's not strictly related to TCP/IP. It is defined with a protcol, which can be TCP/IP, but can be anything else. For example you can have socket that communicates over files. You can also implement a new protocol yourself to have a communication over USB lamp which sends data by flashing: that would still be a socket from the application point of view.
关于端口概念,你在其他答案上读到的是正确的。Port通常是TCP或UDP数据包中的数字值(2字节,0-65535)。我要强调的是,TCP或UPD不一定用于IP之上。所以:
不-说端口是TCP/IP或UDP/IP的一部分是不对的。它是TCP或UDP或任何其他定义和使用它的协议的一部分。IP不知道什么是端口。
尽可能简单地说,套接字和端口之间没有物理区别,例如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,但也不一定非得如此。最基本的特征是两端都知道发生了什么,不管发生了什么。
这里的关键是,当在某个端口上接收到连接请求时,应答握手包括有关为服务特定请求者而创建的套接字的信息。后续通信通过该(私有)套接字连接进行,而不是服务继续侦听连接请求的公共端口连接。
推荐文章
- 在套接字编程中AF_INET和PF_INET的区别是什么?
- read()和recv(), send()和write()之间有什么区别?
- mysqld_safe UNIX套接字文件目录“/var/run/mysqld”不存在
- 如何解决“java.net.BindException: Address already in use: JVM_Bind”错误?
- 增加Linux中TCP/IP连接的最大数量
- 模拟慢速网络连接的网络工具
- 如何打开“谷歌计算引擎”中的特定端口(如9090)
- socket和websocket的区别?
- Android检查网络连接
- 465端口和587端口有什么区别?
- 什么是AF_INET,为什么我需要它?
- Kubernetes Service定义中targetPort和port的区别
- 停止node.js服务器的所有实例
- 现代Linux机顶盒理论上最大的TCP连接数是多少
- Linux上的网络使用top/htop