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


当前回答

A socket represents a single connection between two network applications. These two applications nominally run on different computers, but sockets can also be used for interprocess communication on a single computer. Applications can create multiple sockets for communicating with each other. Sockets are bidirectional, meaning that either side of the connection is capable of both sending and receiving data. Therefore a socket can be created theoretically at any level of the OSI model from 2 upwards. Programmers often use sockets in network programming, albeit indirectly. Programming libraries like Winsock hide many of the low-level details of socket programming. Sockets have been in widespread use since the early 1980s.

端口表示网络通信的端点或“通道”。端口号允许同一计算机上的不同应用程序在不相互干扰的情况下利用网络资源。端口号最常出现在网络编程中,尤其是套接字编程中。但是,有时端口号对普通用户是可见的。例如,一个人在因特网上访问的一些网站使用如下URL:

http://www.mairie-metz.fr:8080/在本例中,数字8080指Web浏览器连接到Web服务器所使用的端口号。通常,Web站点使用端口号80,该端口号不需要包含在URL中(尽管可以包含)。

在IP组网中,端口号理论上可以在0到65535之间。但是,大多数流行的网络应用程序使用范围较低的端口号(例如HTTP的80)。

注意:术语端口还指网络技术的其他几个方面。端口可以指外部设备的物理连接点,如串口、并口和USB端口。术语端口也指某些以太网连接点,例如集线器、交换机或路由器上的连接点。

ref http://compnetworking.about.com/od/basicnetworkingconcepts/l/bldef_port.htm

ref http://compnetworking.about.com/od/itinformationtechnology/l/bldef_socket.htm

其他回答

Socket is an abstraction provided by kernel to user applications for data I/O. A socket type is defined by the protocol it's handling, an IPC communication etc. So if somebody creates a TCP socket he can do manipulations like reading data to socket and writing data to it by simple methods and the lower level protocol handling like TCP conversions and forwarding packets to lower level network protocols is done by the particular socket implementation in the kernel. The advantage is that user need not worry about handling protocol specific nitigrities and should just read and write data to socket like a normal buffer. Same is true in case of IPC, user just reads and writes data to socket and kernel handles all lower level details based on the type of socket created.

端口和IP一起就像给套接字提供一个地址,虽然不是必需的,但它有助于网络通信。

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

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

In a broad sense, Socket - is just that, a socket, just like your electrical, cable or telephone socket. A point where "requisite stuff" (power, signal, information) can go out and come in from. It hides a lot of detailed stuff, which is not required for the use of the "requisite stuff". In software parlance, it provides a generic way of defining a mechanism of communication between two entities (those entities could be anything - two applications, two physically separate devices, User & Kernel space within an OS, etc)

端口是端点标识符。它区分一个端点和另一个端点。在网络级别,它将一个应用程序与另一个应用程序区分开来,以便网络堆栈可以将信息传递给适当的应用程序。

从Oracle Java教程:

套接字是网络上运行的两个程序之间双向通信链路的一个端点。套接字与端口号绑定,以便TCP层可以识别数据要发送到的应用程序。

Socket是软件抽象的网络端点,用作应用程序的接口。在Java、c#中,它用对象表示,在Linux、Unix中,它是一个文件。

Port只是一个套接字的属性,如果你想建立一个通信,你必须指定。要从套接字接收数据包,必须将其绑定到特定的本地端口和网卡(具有本地IP地址)或所有网卡(在绑定调用中指定INADDR_ANY)。要发送数据包,必须指定远端套接字的端口和IP。