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


当前回答

终于. .太多的人将套接字概念连接到双端点通信,主要是在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不知道什么是端口。

其他回答

这个问题已经有了理论上的答案。我想举一个实际的例子来解释这个问题,让大家对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.

简短的回答。

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

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

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.

打个比方

尽管上面已经给出了很多关于套接字的技术知识…… 我想补充我的答案,以防万一,如果有人仍然不能感觉到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分配设备。一旦分配了端口,就没有其他套接字可以连接到该端口。当套接字被关闭时,端口将被释放。

看一下TCP/IP术语。