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


当前回答

端口是网络协议用来访问连接的主机的实体。端口可以是特定于应用程序的,也可以是与特定通信媒介相关的。不同的协议使用不同的端口访问主机,如HTTP使用80端口或FTP使用23端口。您可以在应用程序中分配用户定义的端口号,但这些端口号应该大于1023。

端口打开到所需主机的连接,而套接字是网络间或进程间通信的端点。 套接字是由系统通过api(应用程序编程接口)分配的。

更微妙的区别是,当系统重新启动时,端口将出现,而套接字将被销毁。

其他回答

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地址+端口(数字地址) 它们一起标识机器上网络连接的端点。(我刚刚是不是网络基础课不及格?)

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

端口:

端口可以指物理连接点 用于外部设备,如串行、并行和USB端口。 术语端口也指某些以太网连接点 如集线器、交换机或路由器上的那些。

套接字:

套接字表示两个网络应用程序之间的单个连接。 这两个应用程序名义上运行在不同的计算机上, 但是套接字也可以用于单台计算机上的进程间通信。 应用程序可以创建多个套接字用于相互通信。 套接字是双向的,这意味着连接的任何一方都能够发送和接收数据。

首先,我认为我们应该先了解一下什么构成了从a到B的数据包。

网络的一个常见定义是使用OSI模型,该模型根据目的将网络分离为许多层。有几个重要的,我们将在这里介绍:

The data link layer. This layer is responsible for getting packets of data from one network device to another and is just above the layer that actually does the transmitting. It talks about MAC addresses and knows how to find hosts based on their MAC (hardware) address, but nothing more. The network layer is the layer that allows you to transport data across machines and over physical boundaries, such as physical devices. The network layer must essentially support an additional address based mechanism which relates somehow to the physical address; enter the Internet Protocol (IPv4). An IP address can get your packet from A to B over the internet, but knows nothing about how to traverse individual hops. This is handled by the layer above in accordance with routing information. The transport layer. This layer is responsible for defining the way information gets from A to B and any restrictions, checks or errors on that behaviour. For example, TCP adds additional information to a packet such that it is possible to deduce if packets have been lost.

TCP包含了端口的概念。这些实际上是Internet套接字(AF_INET)可以绑定到的同一IP地址上的不同数据端点。

UDP和其他传输层协议也是如此。从技术上讲,它们不需要以端口为特色,但这些端口确实为上层的多个应用程序提供了一种使用同一台计算机接收(实际上是发出)传出连接的方法。

这就把我们带到了TCP或UDP连接的解剖。每个都有一个源端口和地址,以及一个目标端口和地址。这样,在任何给定的会话中,目标应用程序都可以从源进行响应和接收。

因此,端口本质上是一种规范强制的方式,允许多个并发连接共享相同的地址。

现在,我们需要看看如何从应用程序的角度与外部世界通信。要做到这一点,你需要询问你的操作系统,因为大多数操作系统都支持伯克利套接字的方式来做事情,我们看到我们可以从应用程序创建包含端口的套接字,像这样:

int fd = socket(AF_INET, SOCK_STREAM, 0); // tcp socket
int fd = socket(AF_INET, SOCK_DGRAM, 0); // udp socket
// later we bind...

太棒了!因此,在sockaddr结构中,我们将指定端口,bam!完成工作!嗯,差不多了,除了:

int fd = socket(AF_UNIX, SOCK_STREAM, 0);

也是可能的。啊,这可真是个麻烦!

好吧,实际上并没有。我们所需要做的就是想出一些合适的定义:

internet套接字是IP地址、协议及其相关端口号的组合,服务可以在其上提供数据。tcp端口80,stackoverflow.com是一个互联网套接字。 unix套接字是一个在文件系统中表示的IPC端点,例如/var/run/database.sock。 套接字API是一种请求应用程序能够向套接字读写数据的方法。

瞧!这样就把事情整理好了。在我们的方案中,

端口是一个数字标识符,作为传输层协议的一部分,标识应该响应给定请求的服务号。

因此,端口实际上是形成互联网套接字所需的一个子集。不幸的是,“套接字”这个词的意思恰好被应用到几个不同的概念中。所以我衷心建议你为你的下一个项目命名套接字,只是为了增加混乱;)