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


当前回答

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

套接字有4种类型:

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

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

其他回答

相对的TCP/IP术语,我认为这是隐含的问题。通俗地说:

PORT类似于特定邮政编码中特定房屋的电话号码。城镇的邮政编码可以被认为是城镇和城镇中所有房屋的IP地址。

另一方面,SOCKET更像是一对房屋之间的电话之间的通话。这些呼叫可以在同一城镇的房屋之间建立,也可以在不同城镇的两所房屋之间建立。这对手机之间建立的临时通道就是SOCKET。

一个连接套接字(fd)用于本地地址+本地端口+对端地址+对端端口。通过套接字抽象处理recv/发送数据。 一个监听套接字(fd)用于本地地址+本地监听端口。进程可以通过套接字接受新的连接。

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

套接字有4种类型:

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

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

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.

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.