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


当前回答

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

其他回答

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

套接字由三部分组成:

IP地址 传输协议 端口号

端口是1到65535之间的数字,表示设备中的逻辑门。 客户端和服务器之间的每个连接都需要一个惟一的套接字。

例如:

1030为端口。 (10.1.1.2, TCP,端口1030)是一个套接字。

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.

这些是基本的网络概念,所以我将以一种简单而全面的方式详细解释它们。

插座就像电话(即端到端通信设备) IP就像你的电话号码(即你的插座地址) 端口就像您想要与之交谈的人(即您想从该地址订购的服务) 插座可以是客户端也可以是服务器端(例如,在公司中,客户支持的电话是服务器,但你家里的电话主要是客户端)

因此,网络中的套接字是绑定到一对(ip,端口)=(地址,服务)的虚拟通信设备。

注意:

A machine, a computer, a host, a mobile, or a PC can have multiple addresses , multiple open ports, and thus multiple sockets. Like in an office you can have multiple telephones with multiple telephone numbers and multiple people to talk to. Existence of an open/active port necessitate that you must have a socket bound to it, because it is the socket that makes the port accessible. However, you may have unused ports for the time being. Also note, in a server socket you can bind it to (a port, a specific address of a machine) or to (a port, all addresses of a machine) as in the telephone you may connect many telephone lines (telephone numbers) to a telephone or one specific telephone line to a telephone and still you can reach a person through all these telephone lines or through a specific telephone line. You can not associate (bind) a socket with two ports as in the telephone usually you can not always have two people using the same telephone at the same time . Advanced: on the same machine you cannot have two sockets with same type (client, or server) and same port and ip. However, if you are a client you can open two connections, with two sockets, to a server because the local port in each of these client's sockets is different)

希望它能消除你的疑虑

端口表示IP网络协议的TCP和UDP传输中的通信端点。套接字是通信端点的软件抽象,通常用于这些协议的实现(套接字API)。另一种实现是XTI/TLI API。

参见:

史蒂文斯,W. R. 1998, UNIX网络编程:网络api:套接字和XTI;第一卷,普伦蒂斯大厅。 Stevens, W. R., 1994, TCP/IP图解,第1卷:协议,Addison-Wesley。