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


当前回答

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

套接字有4种类型:

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

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

其他回答

套接字地址是一个IP地址和端口号

123.132.213.231         # IP address
               :1234    # port number
123.132.213.231:1234    # socket address

当两个套接字绑定在一起时,就发生了连接。

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

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.

套接字是一种特殊类型的文件句柄,进程使用它从操作系统请求网络服务。 套接字地址是三元组: {protocol, local-address, local-process},其中本地进程由端口号标识。

在TCP/IP套件中,例如:

{tcp, 193.44.234.3, 12345}

会话是两个进程之间的通信链接,从而描述了两者之间的关联。 关联是一个5元组,它完全指定了组成连接的两个进程: {protocol, local-address, local-process, foreign-address, foreign-process}

在TCP/IP套件中,例如:

{tcp, 193.44.234.3, 1500, 193.44.234.5, 21}

可能是一个有效的关联。

半关联是: {protocol, local-address, local-process}

or

{protocol, foreign-address, foreign-process}

它们指定连接的每一半。

半关联也称为套接字或传输地址。也就是说,套接字是可以在网络中命名和寻址的通信端点。 套接字接口是通信协议的几个应用程序编程接口(api)之一。它被设计为一个通用的通信编程接口,最初由4.2BSD UNIX系统引入。虽然还没有标准化,但已经成为事实上的行业标准。

套接字基本上是网络通信的端点,至少由一个ip地址和一个端口组成。在Java/ c#中,套接字是双向连接一侧的高级实现。

还有Java教程中的一个(非规范的)定义。