这是我所在组织的一位软件工程师提出的问题。我感兴趣的是最广义的定义。
当前回答
相对的TCP/IP术语,我认为这是隐含的问题。通俗地说:
PORT类似于特定邮政编码中特定房屋的电话号码。城镇的邮政编码可以被认为是城镇和城镇中所有房屋的IP地址。
另一方面,SOCKET更像是一对房屋之间的电话之间的通话。这些呼叫可以在同一城镇的房屋之间建立,也可以在不同城镇的两所房屋之间建立。这对手机之间建立的临时通道就是SOCKET。
其他回答
从Oracle Java教程:
套接字是网络上运行的两个程序之间双向通信链路的一个端点。套接字与端口号绑定,以便TCP层可以识别数据要发送到的应用程序。
An application consists of pair of processes which communicate over the network (client-server pair). These processes send and receive messages, into and from the network through a software interface called socket. Considering the analogy presented in the book "Computer Networking: Top Down Approach". There is a house that wants to communicate with other house. Here, house is analogous to a process, and door to a socket. Sending process assumes that there is a infrastructure on the other side of the door that will transport the data to the destination. Once the message is arrived on the other side, it passes through receiver's door (socket) into the house (process). This illustration from the same book can help you: Sockets are part of transport layer, which provides logical communication to applications. This means that from application's point of view both hosts are directly connected to each other, even though there are numerous routers and/or switches between them. Thus a socket is not a connection itself, it's the end point of the connection. Transport layer protocols are implemented only on hosts, and not on intermediate routers. Ports provide means of internal addressing to a machine. The primary purpose it to allow multiple processes to send and receive data over the network without interfering with other processes (their data). All sockets are provided with a port number. When a segment arrives to a host, the transport layer examines the destination port number of the segment. It then forwards the segment to the corresponding socket. This job of delivering the data in a transport layer segment to the correct socket is called de-multiplexing. The segment's data is then forwarded to the process attached to the socket.
打个比方
尽管上面已经给出了很多关于套接字的技术知识…… 我想补充我的答案,以防万一,如果有人仍然不能感觉到ip,端口和套接字之间的区别
考虑一个服务器S,
假设X、Y、Z需要服务器S提供的服务(比如聊天服务)
then
IP地址告诉——>谁?是X,Y,Z想要联系的聊天服务器'S'吗
好的,你有"谁是服务员"
但假设服务器S也为其他人提供一些其他服务,比如S为A、B、C提供存储服务
then
端口告诉——>哪个?你(X,Y,Z)需要的服务,即聊天服务,而不是存储服务
好吧. .,你让服务器知道“聊天服务”是你想要的,而不是存储
but
您是三个人,服务器可能希望以不同的方式识别所有三个人
这是插座
socket告诉。>是哪个?特定的连接
也就是说,
socket 1用于X人
Y的socket 2
Z的插座3
套接字是一种数据I/O机制。端口是通信协议的契约概念。套接字可以没有端口而存在。一个端口可以没有特定的套接字而存在(例如,如果同一个端口上有几个活动的套接字,这可能是某些协议允许的)。
端口用于确定接收端应该将数据包路由到哪个套接字,在许多协议中,但它并不总是必需的,接收套接字的选择可以通过其他方式完成——端口完全是网络子系统中协议处理程序使用的工具。例如,如果协议不使用端口,数据包可以到所有侦听套接字或任何套接字。
首先,我认为我们应该先了解一下什么构成了从a到B的数据包。
网络的一个常见定义是使用OSI模型,该模型根据目的将网络分离为许多层。有几个重要的,我们将在这里介绍:
The data link layer. This layer is responsible for getting packets of data from one network device to another and is just above the layer that actually does the transmitting. It talks about MAC addresses and knows how to find hosts based on their MAC (hardware) address, but nothing more. The network layer is the layer that allows you to transport data across machines and over physical boundaries, such as physical devices. The network layer must essentially support an additional address based mechanism which relates somehow to the physical address; enter the Internet Protocol (IPv4). An IP address can get your packet from A to B over the internet, but knows nothing about how to traverse individual hops. This is handled by the layer above in accordance with routing information. The transport layer. This layer is responsible for defining the way information gets from A to B and any restrictions, checks or errors on that behaviour. For example, TCP adds additional information to a packet such that it is possible to deduce if packets have been lost.
TCP包含了端口的概念。这些实际上是Internet套接字(AF_INET)可以绑定到的同一IP地址上的不同数据端点。
UDP和其他传输层协议也是如此。从技术上讲,它们不需要以端口为特色,但这些端口确实为上层的多个应用程序提供了一种使用同一台计算机接收(实际上是发出)传出连接的方法。
这就把我们带到了TCP或UDP连接的解剖。每个都有一个源端口和地址,以及一个目标端口和地址。这样,在任何给定的会话中,目标应用程序都可以从源进行响应和接收。
因此,端口本质上是一种规范强制的方式,允许多个并发连接共享相同的地址。
现在,我们需要看看如何从应用程序的角度与外部世界通信。要做到这一点,你需要询问你的操作系统,因为大多数操作系统都支持伯克利套接字的方式来做事情,我们看到我们可以从应用程序创建包含端口的套接字,像这样:
int fd = socket(AF_INET, SOCK_STREAM, 0); // tcp socket
int fd = socket(AF_INET, SOCK_DGRAM, 0); // udp socket
// later we bind...
太棒了!因此,在sockaddr结构中,我们将指定端口,bam!完成工作!嗯,差不多了,除了:
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
也是可能的。啊,这可真是个麻烦!
好吧,实际上并没有。我们所需要做的就是想出一些合适的定义:
internet套接字是IP地址、协议及其相关端口号的组合,服务可以在其上提供数据。tcp端口80,stackoverflow.com是一个互联网套接字。 unix套接字是一个在文件系统中表示的IPC端点,例如/var/run/database.sock。 套接字API是一种请求应用程序能够向套接字读写数据的方法。
瞧!这样就把事情整理好了。在我们的方案中,
端口是一个数字标识符,作为传输层协议的一部分,标识应该响应给定请求的服务号。
因此,端口实际上是形成互联网套接字所需的一个子集。不幸的是,“套接字”这个词的意思恰好被应用到几个不同的概念中。所以我衷心建议你为你的下一个项目命名套接字,只是为了增加混乱;)
推荐文章
- 在SSH会话中查找客户端的IP地址
- 模拟慢速互联网连接
- 127.0.0.1和localhost之间的区别是什么
- 为什么SCTP不常用/不为人所知
- 无法启动IIS Express Web服务器,注册URL失败,访问被拒绝
- 我可以在/etc/hosts中映射主机名*和端口*吗?
- 如何修改Play执行run命令时使用的默认端口(9000)?
- 连接网络共享时,如何提供用户名和密码
- 如何找到可用的端口?
- 如何通过windows命令行关闭TCP和UDP端口
- UDP和TCP比起来快多少?
- ssh -L转发多个端口
- 一个干净、轻量级的Python扭曲的替代品?
- 套接字的连接超时和读超时之间的区别是什么?
- 将主机端口转发到docker容器