端口和套接字之间的区别是什么?

时间:2021-08-28 18:11:09

This was a question raised by one of the software engineers in my organisation. I'm interested in the broadest definition.

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

33 个解决方案

#1


762  

Summary

A TCP socket is an endpoint instance defined by an IP address and a port in the context of either a particular TCP connection or the listening state.

TCP套接字是一个端点实例,它是由一个IP地址和一个端口在特定的TCP连接或监听状态下定义的。

A port is a virtualisation identifier defining a service endpoint (as distinct from a service instance endpoint aka session identifier).

端口是定义服务端点的虚拟化标识符(与服务实例端点aka会话标识符不同)。

A TCP socket is not a connection, it is the endpoint of a specific connection.

TCP套接字不是连接,它是特定连接的端点。

There can be concurrent connections to a service endpoint, because a connection is identified by both its local and remote endpoints, allowing traffic to be routed to a specific service instance.

可以并发连接到服务端点,因为它的本地和远程端点都标识了连接,从而允许将通信路由到特定的服务实例。

There can only be one listener socket for a given address/port combination.

对于给定的地址/端口组合,只能有一个侦听器套接字。

Exposition

This was an interesting question that forced me to re-examine a number of things I thought I knew inside out. You'd think a name like "socket" would be self-explanatory: it was obviously chosen to evoke imagery of the endpoint into which you plug a network cable, there being strong functional parallels. Nevertheless, in network parlance the word "socket" carries so much baggage that a careful re-examination is necessary.

这是一个很有趣的问题,迫使我重新审视了一些我认为自己知道的事情。你可能会认为像“套接字”这样的名字是不言自明的:显然,它是被选择来唤醒你插入网络电缆的端点的图像,有强大的功能相似之处。然而,在网络用语中,“插座”这个词承载了太多的包袱,需要仔细的重新检查。

In the broadest possible sense, a port is a point of ingress or egress. Although not used in a networking context, the French word porte literally means door or gateway, further emphasising the fact that ports are transportation endpoints whether you ship data or big steel containers.

在最广泛的意义上,港口是入口或出口的一个点。虽然没有在网络环境中使用,但法语单词porte字面上的意思是门或网关,它进一步强调了一个事实,即港口是运输端点,无论你是船舶数据还是大型钢容器。

For the purpose of this discussion I will limit consideration to the context of TCP-IP networks. The OSI model is all very well but has never been completely implemented, much less widely deployed in high-traffic high-stress conditions.

为了这次讨论的目的,我将限制对TCP-IP网络上下文的考虑。OSI模型非常好,但是从来没有完全实现过,在高流量的高压力条件下部署得更少。

The combination of an IP address and a port is strictly known as an endpoint and is sometimes called a socket. This usage originates with RFC793, the original TCP specification.

IP地址和端口的组合被严格称为端点,有时被称为套接字。这个用法源于RFC793,最初的TCP规范。

A TCP connection is defined by two endpoints aka sockets.

TCP连接由两个端点(即套接字)定义。

An endpoint (socket) is defined by the combination of a network address and a port identifier. Note that address/port does not completely identify a socket (more on this later).

端点(套接字)由网络地址和端口标识符的组合来定义。注意,地址/端口不能完全识别套接字(稍后将对此进行详细说明)。

The purpose of ports is to differentiate multiple endpoints on a given network address. You could say that a port is a virtualised endpoint. This virtualisation makes multiple concurrent connections on a single network interface possible.

端口的目的是在给定的网络地址上区分多个端点。您可以说一个端口是一个虚拟端点。这种虚拟化可以在单个网络接口上实现多个并发连接。

It is the socket pair (the 4-tuple consisting of the client IP address, client port number, server IP address, and server port number) that specifies the two endpoints that uniquely identifies each TCP connection in an internet. (TCP-IP Illustrated Volume 1, W. Richard Stevens)

它是套接字对(由客户端IP地址、客户端端口号、服务器IP地址和服务器端口号组成的4元组),它指定在internet中唯一标识每个TCP连接的两个端点。(TCP-IP插图卷1,W. Richard Stevens)

In most C-derived languages, TCP connections are established and manipulated using methods on an instance of a Socket class. Although it is common to operate on a higher level of abstraction, typically an instance of a NetworkStream class, this generally exposes a reference to a socket object. To the coder this socket object appears to represent the connection because the connection is created and manipulated using methods of the socket object.

在大多数c派生语言中,TCP连接是通过在套接字类的实例上使用方法来建立和操作的。尽管在更高级别的抽象(通常是NetworkStream类的实例)上操作是很常见的,但这通常会暴露对套接字对象的引用。对于编码器,这个套接字对象似乎表示连接,因为连接是用套接字对象的方法创建和操作的。

In C#, to establish a TCP connection (to an existing listener) first you create a TcpClient. If you don't specify an endpoint to the TcpClient constructor it uses defaults - one way or another the local endpoint is defined. Then you invoke the Connect method on the instance you've created. This method requires a parameter describing the other endpoint.

在c#中,首先要创建一个TCP连接(对于现有的侦听器),然后创建一个TcpClient。如果您没有为TcpClient构造函数指定一个端点,那么它将使用缺省值—一个或另一个方法定义本地端点。然后,在创建的实例上调用Connect方法。此方法需要一个描述其他端点的参数。

All this is a bit confusing and leads you to believe that a socket is a connection, which is bollocks. I was labouring under this misapprehension until Richard Dorman asked the question.

所有这些都有点混乱,让您相信一个套接字是一个连接,即bollocks。我一直在为这种误解而苦恼,直到理查德·多曼问了这个问题。

Having done a lot of reading and thinking, I'm now convinced that it would make a lot more sense to have a class TcpConnection with a constructor that takes two arguments, LocalEndpoint and RemoteEndpoint. You could probably support a single argument RemoteEndpoint when defaults are acceptable for the local endpoint. This is ambiguous on multihomed computers, but the ambiguity can be resolved using the routing table by selecting the interface with the shortest route to the remote endpoint.

在做了大量的阅读和思考之后,我现在确信,拥有一个带有两个参数、LocalEndpoint和RemoteEndpoint的构造函数的类TcpConnection会更有意义。当本地端点可以接受缺省值时,您可能会支持一个参数RemoteEndpoint。这在多主机计算机上是不明确的,但是可以使用路由表通过选择最短路由到远程端点的接口来解决歧义。

Clarity would be enhanced in other respects, too. A socket is not identified by the combination of IP address and port:

透明度也会在其他方面得到加强。IP地址和端口的组合不能识别套接字:

[...]TCP demultiplexes incoming segments using all four values that comprise the local and foreign addresses: destination IP address, destination port number, source IP address, and source port number. TCP cannot determine which process gets an incoming segment by looking at the destination port only. Also, the only one of the [various] endpoints at [a given port number] that will receive incoming connection requests is the one in the listen state. (p255, TCP-IP Illustrated Volume 1, W. Richard Stevens)

[…]TCP demultiplexes传入段使用包含本地和外部地址的所有四个值:目标IP地址、目的端口号、源IP地址和源端口号。通过只查看目标端口,TCP无法确定哪个进程获得传入段。另外,在[给定端口号]中,将接收传入连接请求的[各种]端点中唯一的一个是侦听状态中的一个。(p255, TCP-IP插图卷1,W. Richard Stevens)

As you can see, it is not just possible but quite likely for a network service to have numerous sockets with the same address/port, but only one listener socket on a particular address/port combination. Typical library implementations present a socket class, an instance of which is used to create and manage a connection. This is extremely unfortunate, since it causes confusion and has lead to widespread conflation of the two concepts.

正如您所看到的,网络服务不仅有可能,而且很可能有多个具有相同地址/端口的套接字,而只有一个特定地址/端口组合的侦听器套接字。典型的库实现提供了一个socket类,一个用于创建和管理连接的实例。这是极其不幸的,因为它引起混乱,并导致了这两个概念的广泛合并。

Hagrawal doesn't believe me (see comments) so here's a real sample. I connected a web browser to http://dilbert.com and then ran netstat -an -p tcp. The last six lines of the output contain two examples of the fact that address and port are not enough to uniquely identify a socket. There are two distinct connections between 192.168.1.3 (my workstation) and 54.252.92.236:80

Hagrawal不相信我(见注释),所以这是一个真实的例子。我将一个web浏览器连接到http://dilbert.com,然后运行netstat -an -p tcp。输出的最后6行包含两个示例,说明地址和端口不足以唯一标识套接字。在192.168.1.3(我的工作站)和54.252.92.236:80之间有两个明显的连接。

  TCP    192.168.1.3:63240      54.252.94.236:80       SYN_SENT
  TCP    192.168.1.3:63241      54.252.94.236:80       SYN_SENT
  TCP    192.168.1.3:63242      207.38.110.62:80       SYN_SENT
  TCP    192.168.1.3:63243      207.38.110.62:80       SYN_SENT
  TCP    192.168.1.3:64161      65.54.225.168:443      ESTABLISHED

Since a socket is the endpoint of a connection, there are two sockets with the address/port combination 207.38.110.62:80 and two more with the address/port combination 54.252.94.236:80.

由于套接字是连接的端点,所以有两个带地址/端口组合的套接字:207.38.110.62:80,还有两个带地址/端口组合的套接字:54.252.94.236 . 80。

I think Hagrawal's misunderstanding arises from my very careful use of the word "identifies". I mean "completely, unambiguously and uniquely identifies". In the above sample there are two endpoints with the address/port combination 54.252.94.236:80. If all you have is address and port, you don't have enough information to tell these sockets apart. It's not enough information to identify a socket.

我认为Hagrawal的误解来自于我非常谨慎地使用“识别”这个词。我的意思是“完全、明确、唯一地识别”。在上面的示例中,有两个端点,地址/端口组合为54.252.94.236:80。如果您所拥有的只是地址和端口,那么您就没有足够的信息来区分这些套接字。没有足够的信息来识别套接字。

Addendum

Paragraph two of section 2.7 of RFC793 says

RFC793第2.7节第2段说。

A connection is fully specified by the pair of sockets at the ends. A local socket may participate in many connections to different foreign sockets.

连接由两端的套接字完全指定。本地套接字可以参与许多连接到不同的外国套接字。

This definition of socket is not helpful from a programming perspective because it is not the same as a socket object, which is the endpoint of a particular connection. To a programmer, and most of this question's audience are programmers, this is a vital functional difference.

从编程的角度来看,这个套接字的定义并不是很有用,因为它与套接字对象不同,后者是特定连接的端点。对于程序员来说,这个问题的大部分听众都是程序员,这是一个至关重要的功能差异。

References

  1. TCP-IP Illustrated Volume 1 The Protocols, W. Richard Stevens, 1994 Addison Wesley

    《协议》第1卷《议定书》,W. Richard Stevens, 1994年Addison Wesley。

  2. RFC793, Information Sciences Institute, University of Southern California for DARPA

    RFC793,南加利福尼亚大学信息科学研究所。

  3. RFC147, The Definition of a Socket, Joel M. Winett, Lincoln Laboratory

    RFC147,一个插座的定义,Joel M. Winett,林肯实验室。

#2


144  

A socket consists of three things:

套接字由三件事组成:

  1. An IP address
  2. 一个IP地址
  3. A transport protocol
  4. 传输协议
  5. A port number
  6. 端口号

A port is a number between 1 and 65535 inclusive that signifies a logical gate in a device. Every connection between a client and server requires a unique socket.

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

For example:

例如:

  • 1030 is a port.
  • 1030是一个港口。
  • (10.1.1.2 , TCP , port 1030) is a socket.
  • (10.1.1.2,TCP,端口1030)是一个套接字。

#3


82  

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.

套接字表示两个网络应用程序之间的单个连接。这两个应用程序名义上运行在不同的计算机上,但是套接字也可以用于单个计算机上的进程间通信。应用程序可以创建多个套接字来进行通信。套接字是双向的,这意味着连接的任何一端都能够发送和接收数据。因此,从理论上可以在OSI模型的任何级别上从2向上创建套接字。程序员经常在网络编程中使用套接字,尽管是间接的。像Winsock这样的编程库隐藏了许多套接字编程的底层细节。自上世纪80年代初以来,插座就被广泛使用。

A port represents an endpoint or "channel" for network communications. Port numbers allow different applications on the same computer to utilize network resources without interfering with each other. Port numbers most commonly appear in network programming, particularly socket programming. Sometimes, though, port numbers are made visible to the casual user. For example, some Web sites a person visits on the Internet use a URL like the following:

端口代表网络通信的端点或“通道”。端口号允许不同的应用程序在同一台计算机上使用网络资源而不互相干扰。端口号最常出现在网络编程中,尤其是套接字编程。不过,有时对临时用户可见端口号。例如,一些网站在互联网*问的人使用的网址如下:

http://www.mairie-metz.fr:8080/ In this example, the number 8080 refers to the port number used by the Web browser to connect to the Web server. Normally, a Web site uses port number 80 and this number need not be included with the URL (although it can be).

在本例中,数字8080是指Web浏览器用于连接到Web服务器的端口号。通常,Web站点使用端口80,而这个数字不需要包含在URL中(尽管可以是)。

In IP networking, port numbers can theoretically range from 0 to 65535. Most popular network applications, though, use port numbers at the low end of the range (such as 80 for HTTP).

在IP网络中,端口号在理论上可以从0到65535。不过,大多数流行的网络应用程序使用的端口号都是低端的(比如HTTP的80)。

Note: The term port also refers to several other aspects of network technology. A port can refer to a physical connection point for peripheral devices such as serial, parallel, and USB ports. The term port also refers to certain Ethernet connection points, such as those on a hub, switch, or router.

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

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

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

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

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

#4


40  

Firsty, I think we should start with a little understanding of what constitutes getting a packet from A to B.

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

A common definition for a network is the use of the OSI Model which separates a network out into a number of layers according to purpose. There are a few important ones, which we'll cover here:

网络的一个常见定义是使用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.
  • 数据链路层。这一层负责将数据包从一个网络设备发送到另一个网络设备,并在实际执行传输的层之上。它谈到了MAC地址,知道如何根据MAC(硬件)地址找到主机,但没有更多。
  • 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.
  • 网络层是允许您跨机器和物理边界(例如物理设备)传输数据的层。网络层本质上必须支持一个附加的基于地址的机制,它与物理地址有关;输入Internet协议(IPv4)。IP地址可以通过internet从A到B获得数据包,但是不知道如何遍历单个的hops。这是由上面的层按照路由信息处理的。
  • 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.
  • 传输层。这一层负责定义信息从A到B的方式,以及对该行为的任何限制、检查或错误。例如,TCP将附加信息添加到包中,这样就可以推断包是否丢失了。

TCP contains, amongst other things, the concept of ports. These are effectively different data endpoints on the same IP address to which an Internet Socket (AF_INET) can bind.

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

As it happens, so too does UDP, and other transport layer protocols. They don't technically need to feature ports, but these ports do provide a way for multiple applications in the layers above to use the same computer to receive (and indeed make) outgoing connections.

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

Which brings us to the anatomy of a TCP or UDP connection. Each features a source port and address, and a target port and address. This is so that in any given session, the target application can respond, as well as receive, from the source.

这就引出了一个TCP或UDP连接的剖析。每个特性都有一个源端口和地址,以及一个目标端口和地址。这是这样的,在任何给定的会话中,目标应用程序都可以从源中响应,也可以接收。

So ports are essentially a specification-mandated way of allowing multiple concurrent connections sharing the same address.

因此,端口本质上是一种特定的授权方式,允许多个并发连接共享同一个地址。

Now, we need to take a look at how you communicate from an application point of view to the outside world. To do this, you need to kindly ask your operating system and since most OSes support the Berkeley Sockets way of doing things, we see we can create sockets involving ports from an application like this:

现在,我们需要了解一下如何从应用程序的角度与外部世界进行通信。要做到这一点,您需要友好地询问您的操作系统,因为大多数OSes都支持Berkeley套接字的方法,我们看到我们可以在这样的应用程序中创建包含端口的套接字:

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

Great! So in the sockaddr structures, we'll specify our port and bam! Job done! Well, almost, except:

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

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

is also possible. Urgh, that's thrown a spanner in the works!

也是可能的。呃,那是一个扳手在工作!

Ok, well actually it hasn't. All we need to do is come up with some appropriate definitions:

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

  • An internet socket is the combination of an IP address, a protocol and its associated port number on which a service may provide data. So tcp port 80, *.com is an internet socket.
  • internet套接字是一个IP地址、一个协议和一个服务可以提供数据的相关端口号的组合。所以tcp端口80,*.com是一个网络插座。
  • An unix socket is an IPC endpoint represented in the file system, e.g. /var/run/database.sock.
  • unix套接字是在文件系统中表示的IPC端点,例如/var/run/database.sock。
  • A socket API is a method of requesting an application be able to read and write data to a socket.
  • 套接字API是一种请求应用程序能够读写数据到套接字的方法。

Voila! That tidies things up. So in our scheme then,

瞧!将地事情。所以在我们的计划中,

  • A port is a numeric identifier which, as part of a transport layer protocol, identifies the service number which should respond to the given request.
  • 端口是一个数字标识符,作为传输层协议的一部分,它标识应该响应给定请求的服务编号。

So really a port is a subset of the requirements for forming an internet socket. Unfortunately, it just so happens that the meaning of the word socket has been applied to several different ideas. So I heartily advise you name your next project socket, just to add to the confusion ;)

因此,真正的端口是形成internet套接字的需求的子集。不幸的是,“套接字”的含义已经被应用到几个不同的概念上。所以我衷心地建议你说出下一个项目的名称,只是为了增加混乱。

#5


37  

With some analogy

用一些比喻

Although a lot technical stuff is already given above for sockets... I would like to add my answer, just in case , if somebody still could not feel the difference between ip, port and sockets

虽然有很多技术上的东西已经给了插座……我想添加我的答案,以防万一有人仍然感觉不到ip、端口和套接字之间的区别。

Consider a server S,

考虑一个服务器,

and say person X,Y,Z need a service (say chat service) from that server S

并且说person X,Y,Z需要一个服务(比如聊天服务)。

then

然后

IP address tells --> who? is that chat server 'S' with whom X,Y,Z want to contact

IP地址告诉——>谁?是聊天服务器的“与谁X,Y,Z想联系?”

okay, you got "who is the server"

好的,你得到了"谁是服务器"

but suppose that server 'S' is also providing some other services to other people,say 'S' provides storage services to person A,B,C

但是假设服务器'S'也为其他人提供一些其他服务,比如'S'提供存储服务给A,B,C。

then

然后

port tells ---> which? service you (X,Y,Z) need i.e. chat service and not that storage service

港告诉- - - >哪些吗?服务您(X,Y,Z)需要的是聊天服务,而不是存储服务。

okay.., you Server came to know that 'chat service' is what you want and not the storage

好吧. .你的服务器知道“聊天服务”是你想要的,而不是存储。

but

you are three and the server might want to identify all the three differently

您是三个,服务器可能想要识别所有三个不同的。

there comes the socket

有套接字

now socket tells--> which one? particular connection

socket告诉我们,>是哪个?特定的连接

that is , say ,

也就是说,

socket 1 for person X

插座1给人X。

socket 2 for person Y

插座2给人Y。

and socket 3 for person Z

和插座3给人Z。

I hope it helps someone who was still confused :)

我希望它能帮助那些仍然困惑的人:

#6


22  

There seems to be a lot of answers equating socket with the connection between 2 PC's..which I think is absolutely incorrect. A socket has always been the endpoint on 1 PC, that may or may not be connected - surely we've all used listener or UDP sockets* at some point. The important part is that it's addressable and active. Sending a message to 1.1.1.1:1234 is not likely to work, as there is no socket defined for that endpoint.

似乎有很多答案将套接字与两台PC的连接连接起来。我认为这是绝对错误的。一个套接字一直是1个PC上的端点,它可能是连接的,也可能不是连接的——在某个点上,我们肯定都使用了侦听器或UDP套接字。重要的是它是可寻址和活跃的。将消息发送到1.1.1.1:1234是不太可能的,因为没有为该端点定义套接字。

Sockets are protocol specific - so the implementation of uniqueness that both TCP/IP and UDP/IP uses* (ipaddress:port), is different than eg., IPX (Network, Node, and...ahem, socket - but a different socket than is meant by the general "socket" term. IPX socket numbers are equivalent to IP ports). But, they all offer a unique addressable endpoint.

套接字是特定于协议的,所以TCP/IP和UDP/IP使用* (ipaddress:port)的惟一性的实现不同于eg。,IPX(网络,节点,和…嗯,套接字——但是一个不同的套接字,而不是一般的“套接字”术语。IPX套接字数字相当于IP端口。但是,它们都提供了一个独特的可寻址端点。

Since IP has become the dominant protocol, a port (in networking terms) has become synonomous with either a UDP or TCP port number - which is a portion of the socket address.

由于IP已经成为主导协议,一个端口(在网络术语中)与UDP或TCP端口号(这是套接字地址的一部分)是同步的。

  • UDP is connection-less - meaning no virtual circuit between the 2 endpoints is ever created. However, we still refer to UDP sockets as the endpoint. The API functions make it clear that both are just different type of sockets - SOCK_DGRAM is UDP (just sending a message) and SOCK_STREAM is TCP (creating a virtual circuit).

    UDP是无连接的,这意味着在两个端点之间没有创建任何虚拟电路。然而,我们仍然将UDP套接字作为端点。API函数清楚地表明,它们都是不同类型的套接字——SOCK_DGRAM是UDP(只发送一条消息),而SOCK_STREAM是TCP(创建一个虚拟电路)。

  • Technically, the IP header holds the IP Address, and the protocol on top of IP (UDP or TCP) holds the port number. This makes it possible to have other protocols (eg. ICMP that have no port numbers, but do have IP addressing information).

    技术上,IP报头保存IP地址,IP地址(UDP或TCP)上的协议保留端口号。这使得有其他的协议成为可能。没有端口号的ICMP,但有IP寻址信息。

#7


21  

Short brief answer.

简短的答案。

A port can be described as an internal address within a host that identifies a program or process.

一个端口可以被描述为一个主机的内部地址,它标识一个程序或进程。

A socket can be described as a programming interface allowing a program to communicate with other programs or processes, on the internet, or locally.

一个套接字可以被描述为一个编程接口,允许一个程序与其他程序或进程,在因特网上或本地进行通信。

#8


20  

A socket = IP Address + a port (numeric address)
Together they identify an end-point for a network connection on a machine. (Did I just flunk network 101?)

一个套接字= IP地址+一个端口(数字地址),它们一起确定一个机器上的网络连接的端点。(我只是不及格吗?)

#9


19  

Generally you will get a lot of theoretical but one of the easiest ways to differentiate these two concepts is as follows:

一般来说,你会得到很多理论但最简单的区分这两个概念的方法是:

In order to get a service you need a service number. This service number is called a port. Simple as that.

为了获得服务,您需要一个服务编号。这个服务号码被称为端口。就这么简单。

For example, the HTTP as a service is running on port 80.

例如,HTTP作为服务在端口80上运行。

Now, many people can request the service and a connection from client- server has established. There will be a lot of connections. Each connection represent a client. In order to maintain each of the connection, the server creates socket per connection to maintain it's client.

现在,许多人可以请求服务和从客户端服务器建立连接。会有很多的联系。每个连接代表一个客户端。为了维护每个连接,服务器为维护它的客户端创建每个连接的套接字。

Hope this helps.Thank you.

希望这个有帮助。谢谢你!

#10


14  

They are terms from two different domains: 'port' is a concept from TCP/IP networking, 'socket' is an API (programming) thing. A 'socket' is made (in code) by taking a port and a hostname or network adapter and combining them into a data structure that you can use to send or receive data.

它们是来自两个不同领域的术语:“端口”是一个来自TCP/IP网络的概念,“socket”是一个API(编程)的东西。“套接字”是通过取一个端口和一个主机名或网络适配器,并将它们组合成一个可以用来发送或接收数据的数据结构来实现的(在代码中)。

#11


10  

After reading the excellent up-voted answers, I found that the following point needed emphasis for me, a newcomer to network programming:

在阅读了优秀的上选答案后,我发现我需要强调以下几点:网络编程的新手:

TCP-IP connections are bi-directional pathways connecting one address:port combination with another address:port combination. Therefore, whenever you open a connection from your local machine to a port on a remote server (say www.google.com:80), you are also associating a new port number on your machine with the connection, to allow the server to send things back to you, (e.g. 127.0.0.1:65234). It can be helpful to use netstat to look at your machine's connections:

TCP-IP连接是连接一个地址的双向通道:端口组合与另一个地址:端口组合。因此,当您从本地机器打开到远程服务器上的端口的连接时(比如www.google.com:80),您也会将一个新的端口号与连接在您的机器上,以便服务器将其发回给您(例如:127.0.0.1:65234)。使用netstat查看您的机器的连接是很有帮助的:

> netstat -nWp tcp (on OS X)
Active Internet connections
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)    
tcp4       0      0  192.168.0.6.49871      17.172.232.57.5223     ESTABLISHED
...

#12


9  

A socket is a special type of file handle which is used by a process to request network services from the operating system. A socket address is the triple: {protocol, local-address, local-process} where the local process is identified by a port number.

套接字是一种特殊类型的文件句柄,用于从操作系统请求网络服务。套接字地址是三重:{协议、本地地址、本地进程},本地进程由端口号标识。

In the TCP/IP suite, for example:

在TCP/IP套件中,例如:

{tcp, 193.44.234.3, 12345}

{ tcp 193.44.234.3 12345 }

A conversation is the communication link between two processes thus depicting an association between two. An association is the 5-tuple that completely specifies the two processes that comprise a connection: {protocol, local-address, local-process, foreign-address, foreign-process}

会话是两个过程之间的通信链路,因此描述了两者之间的关联。一个关联是5元组,它完全指定包含连接的两个进程:{协议、本地地址、本地进程、外地址、外进程}

In the TCP/IP suite, for example:

在TCP/IP套件中,例如:

{tcp, 193.44.234.3, 1500, 193.44.234.5, 21}

{tcp, 193.44.234.3, 1500, 193.44.234.5, 21}

could be a valid association.

可以是一个有效的关联。

A half-association is either: {protocol, local-address, local-process}

半关联是:{协议,本地地址,本地进程}

or

{protocol, foreign-address, foreign-process}

{协议,国外住址,foreign-process }

which specify each half of a connection.

它指定连接的每一半。

The half-association is also called a socket or a transport address. That is, a socket is an end point for communication that can be named and addressed in a network. The socket interface is one of several application programming interfaces (APIs) to the communication protocols. Designed to be a generic communication programming interface, it was first introduced by the 4.2BSD UNIX system. Although it has not been standardized, it has become a de facto industry standard.

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

#13


6  

A socket address is an IP address & port number

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

A connection occurs when 2 sockets are bound together.

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

#14


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.

应用程序由对网络(客户机-服务器对)进行通信的两个进程组成。这些进程通过一个称为套接字的软件接口发送和接收消息。考虑到书中所提到的“计算机网络:自上而下的方法”。有一所房子想与其他房子沟通。在这里,house类似于一个过程,而门则是一个套接字。发送过程假定在门的另一端有一个基础设施,将数据传输到目的地。一旦信息到达另一边,它就通过接收器的门(插座)进入房子(过程)。来自同一本书的插图可以帮助您:套接字是传输层的一部分,它为应用程序提供逻辑通信。这意味着,从应用程序的角度来看,两个主机都是直接连接的,即使它们之间有许多路由器和/或交换机。因此,套接字本身不是连接本身,而是连接的端点。传输层协议只在主机上实现,而不是在中间路由器上实现。端口提供了机器内部寻址的方法。其主要目的是允许多个进程通过网络发送和接收数据,而不会干扰其他进程(它们的数据)。所有的套接字都有一个端口号。当一个段到达主机时,传输层检查该区段的目的端口号。然后将该部分转发到相应的套接字。将传输层段中的数据传输到正确的套接字的工作称为去多路复用。然后将该段的数据转发给连接到套接字的进程。

#15


4  

from Oracle Java Tutorial:

从甲骨文Java教程:

A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to.

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

#16


4  

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.

套接字是一个通信端点。套接字与TCP/IP协议家族没有直接关系,它可以与您的系统支持的任何协议一起使用。C的套接字API希望你先从系统获得一个空白的套接字对象,然后,您可以绑定到本地套接字地址(直接检索的传入流量无连接协议或接受传入的连接请求为面向连接协议),或者您可以连接到一个远程套接字地址(协议)。您甚至可以同时进行这两种操作,如果您想要同时控制它们,那么本地套接字地址将绑定到一个套接字,而远程套接字地址则连接到一个套接字。连接一个套接字的无连接协议甚至是可选的,但是如果您不这样做,您还必须将您想要发送的每个包的目标地址传递给这个套接字,就像套接字知道将这些数据发送到哪里一样?优点是您可以使用单个套接字将数据包发送到不同的套接字地址。一旦您配置好了套接字,甚至连接了它,请将其视为双向通信管道。您可以使用它将数据传递到某个目的地,并且某些目的地可以使用它将数据传递给您。你给套接字写的东西被发送出去,收到的是可以阅读的。

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, always the same server address and the same source IP, as they all come from the same client, but as they have a 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.

另一方面,端口只是TCP/IP协议栈的某些协议。TCP和UDP包都有端口。一个端口就是一个简单的数字。源端口和目的端口的组合标识了两个主机之间的通信通道。例如,您可能有一个服务器,一个简单的HTTP服务器和一个简单的FTP服务器。如果现在有一个数据包到达该服务器的地址,它如何知道这是HTTP或FTP服务器的数据包?它将会知道,因为HTTP服务器将在端口80上运行,端口21上的FTP服务器将运行,所以如果数据包到达目的端口80,它是为HTTP服务器而不是为FTP服务器。另外,包有一个源端口,因为没有这样的源端口,服务器只能一次连接一个IP地址。服务器的源端口可以区分否则相同的连接:他们都有相同的目的港,如端口80,同样的目的地IP,总是相同的服务器地址和相同的源IP,因为他们都来自同一个客户,但他们有一个不同的源端口,服务器可以区分彼此。当服务器发送回回复时,它会对请求来自的端口这样做,这样客户端也可以区分收到的不同的回复。

#17


3  

Relative TCP/IP terminology which is what I assume is implied by the question. In layman's terms:

相对的TCP/IP术语,这是我假设的问题。通俗的说:

A PORT is like the telephone number of a particular house in a particular zip code. The ZIP code of the town could be thought of as the IP address of the town and all the houses in that town.

端口就像特定邮政编码中的某一特定房屋的电话号码。城镇的邮政编码可以被认为是城镇的IP地址和镇上所有的房子。

A SOCKET on the other hand is more like an established phone call between telephones of a pair of houses talking to each other. Those calls can be established between houses in the same town or two houses in different towns. It's that temporary established pathway between the pair of phones talking to each other that is the SOCKET.

另一方面,套接字更像是一对相互交谈的房子之间的电话。这些电话可以在同一城镇的房屋和不同城镇的两幢房子之间建立。这是一种临时搭建的通道,在两部手机之间相互交谈,这就是插座。

#18


3  

A socket is a structure in your software. It's more-or-less a file; it has operations like read and write. It isn't a physical thing; it's a way for your software to refer to physical things.

套接字是软件中的一个结构。这是或多或少一个文件;它有读和写操作。它不是物质的;这是一种让你的软件引用物理事物的方法。

A port is a device-like thing. Each host has one or more networks (those are physical); a host has an address on each network. Each address can have thousands of ports.

端口是设备一样的东西。每个主机都有一个或多个网络(这些网络是物理的);主机在每个网络上都有一个地址。每个地址可以有数千个端口。

One socket only may be using a port at an address. The socket allocates the port approximately like allocating a device for file system I/O. Once the port is allocated, no other socket can connect to that port. The port will be freed when the socket is closed.

一个套接字只能在地址上使用端口。套接字分配端口近似于为文件系统I/O分配一个设备。一旦端口被分配,没有其他的套接字可以连接到那个端口。当套接字关闭时,端口将被释放。

Take a look at TCP/IP Terminology.

看看TCP/IP的术语。

#19


3  

The port was the easiest part, it is just a unique identifier for a socket. A socket is something processes can use to establish connections and to communicate with each other. Tall Jeff had a great telephone analogy which was not perfect, so I decided to fix it:

端口是最简单的部分,它只是套接字的唯一标识符。套接字是一些进程可以用来建立连接和相互通信的过程。高大的杰夫有一个很好的电话类比,但并不完美,所以我决定改正它:

  • ip and port ~ phone number
  • ip和端口~电话号码。
  • socket ~ phone device
  • 套接字~电话设备
  • connection ~ phone call
  • 电话联系~
  • establishing connection ~ calling a number
  • 建立连接~调用一个数字。
  • processes, remote applications ~ people
  • 进程,远程应用程序~人。
  • messages ~ speech
  • 消息~演讲

#20


2  

A socket is a data I/O mechanism. A port is a contractual concept of a communication protocol. A socket can exist without a port. A port can exist witout a specific socket (e.g. if several sockets are active on the same port, which may be allowed for some protocols).

套接字是一个数据I/O机制。端口是通信协议的契约概念。套接字可以在没有端口的情况下存在。一个端口可以存在一个特定的套接字(例如,如果多个套接字在同一个端口上是活动的,这可能被允许用于某些协议)。

A port is used to determine which socket the receiver should route the packet to, with many protocols, but it is not always required and the receiving socket selection can be done by other means - a port is entirely a tool used by the protocol handler in the network subsystem. e.g. if a protocol does not use a port, packets can go to all listening sockets or any socket.

端口是用来确定哪些套接字接收方应将数据包路由到,许多协议,但它并不总是需要和接受套接字选择可以通过其他方式——一个端口协议处理器使用的完全是一个工具在网络子系统。如果一个协议不使用一个端口,数据包可以进入所有监听套接字或任何套接字。

#21


2  

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)

从广义上讲,插座——就是插座,就像你的电线、电缆或电话插座一样。“必要的东西”(权力、信号、信息)可以从这里出去。它隐藏了大量的细节,这并不需要使用“必要的东西”。在软件术语中,它提供了一种通用的方式来定义两个实体之间的通信机制(这些实体可以是任何东西——两个应用程序、两个物理独立的设备、一个操作系统中的用户和内核空间等等)。

A Port is an endpoint discriminator. It differentiates one endpoint from another. At networking level, it differentiates one application from another, so that the networking stack can pass on information to the appropriate application.

端口是一个端点鉴别器。它将一个端点与另一个端点区分开来。在网络级别上,它将一个应用程序与另一个应用程序区分开来,这样网络堆栈就可以将信息传递给适当的应用程序。

#22


2  

Already theoretical answers have been given to this question. I would like to give a practical example to this question, which will clear your understanding about Socket and Port.

这个问题已经得到了理论的答案。我想举一个实际的例子来说明这个问题,这将使你对插座和端口的理解更加清晰。

I found it here

我发现在这里

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.

这个例子将引导您通过连接到一个网站的过程,例如Wiley。你可以打开你的浏览器(比如Mozilla Firefox),然后在地址栏中输入www.wiley.com。您的web浏览器使用域名系统(DNS)服务器查找名称www.wiley.com,以确定其IP地址。在本例中,地址是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.

Firefox将连接到192.0.2.100地址,并连接到应用程序层web服务器正在运行的端口。Firefox知道应该期待什么端口,因为它是一个众所周知的端口。web服务器的著名端口是TCP端口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.

Firefox尝试连接的目标套接字被写入套接字:端口,或者在这个例子中,192.0.2.100:80。这是连接的服务器端,但是服务器需要知道如何将您想要查看的web页面发送到Mozilla Firefox中,因此您也可以在连接的客户端有一个套接字。

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.

客户端连接由您的IP地址组成,如192.168.1.25和随机选择的动态端口号。与Firefox相关的套接字看起来像192.168.1.25:49175。因为web服务器在TCP端口80上操作,所以这两个套接字都是TCP套接字,而如果连接到在UDP端口上运行的服务器,服务器和客户端套接字都是UDP套接字。

#23


2  

Socket is an abstraction provided by kernel to user applications for data I/O. A socket type is defined by the protocol its 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.

套接字是内核向用户应用程序提供的数据I/O的抽象。套接字类型定义的协议处理、IPC沟通等等。如果有人创建了一个TCP套接字他可以做操作喜欢阅读和写数据套接字数据的简单方法和低层协议TCP转换和转发数据包处理底层网络协议是通过特定的套接字实现的内核。好处是用户不必担心处理协议特定的nitigrities,而应该像普通缓冲区那样读写数据。在IPC的情况下也是如此,用户只是读写数据到套接字,而内核根据所创建的套接字的类型处理所有低级的细节。

Port together with IP is like providing an address to the socket, though its not necessary, but it helps in network communications.

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

#24


2  

Port and socket can be compared to the Bank Branch.

端口和套接字可以与银行分支进行比较。

The building number of the "Bank" is analogous to IP address . Bank has got different sections like :1) SAVINGS ACCOUNT DEPARTMENT 2) PERSONAL LOAN DEPARTMENT 3) HOME LOAN DEPARTMENT 4) GRIEVANCE DEPARTMENT .

“银行”的建筑编号类似于IP地址。银行有不同的部门:1)储蓄账户2)个人贷款部3)住房贷款部门4)申诉部。

so 1(SAVINGS ACCOUNT DEPARTMENT) ,2 ( PERSONAL LOAN DEPARTMENT) ,3(HOME LOAN DEPARTMENT) and 4(GRIEVANCE DEPARTMENT) are Ports.

所以1(储蓄账户部门),2(个人贷款部门),3(住房贷款部门)和4(申诉部)是港口。

Now let us say you go to open a savings account , you go to Bank(IP address) then you go to "SAVINGS ACCOUNT DEPARTMENT" (Port number 1) then you meet one of the employee working under "SAVINGS ACCOUNT DEPARTMENT" let us call him as SAVINGACCOUNT_EMPLOYEE1 for opening account .

现在,我们假设你去开一个储蓄账户,你去银行(IP地址),然后去“储蓄账户部门”(Port number 1),然后你会遇到一个在“储蓄账户部门”下工作的员工,让我们把他叫做SAVINGACCOUNT_EMPLOYEE1。

SAVINGACCOUNT_EMPLOYEE1 is your socket descriptor , so there may be SAVINGACCOUNT_EMPLOYEE1 to SAVINGACCOUNT_EMPLOYEEN these are all socket descriptors.

SAVINGACCOUNT_EMPLOYEE1是您的套接字描述符,因此可能会有SAVINGACCOUNT_EMPLOYEE1来SAVINGACCOUNT_EMPLOYEEN这些都是套接字描述符。

Likewise other departments will be having employess working under them and they are analogous to socket.

同样的,其他部门也会有员工在他们手下工作,而他们也类似于插座。

Hope this helps !!

希望这有助于! !

#25


1  

A socket is basically an endpoint for network communication, consisting of at least an IP-address and a port. In Java/C# a socket is a higher level implementation of one side of a two-way connection.

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

Also, a definition in the Java documentation.

还有,Java文档中的定义。

#26


1  

Port:

端口:

A port can refer to a physical connection point for peripheral devices such as serial, parallel, and USB ports. The term port also refers to certain Ethernet connection points, s uch as those on a hub, switch, or router.

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

Socket:

套接字:

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.

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

#27


1  

A single port can have one or more sockets connected with different external IP's like a multiple electrical outlet.

单个端口可以有一个或多个与不同外部IP相连的套接字,就像一个多电源插座。

  TCP    192.168.100.2:9001     155.94.246.179:39255   ESTABLISHED     1312
  TCP    192.168.100.2:9001     171.25.193.9:61832     ESTABLISHED     1312
  TCP    192.168.100.2:9001     178.62.199.226:37912   ESTABLISHED     1312
  TCP    192.168.100.2:9001     188.193.64.150:40900   ESTABLISHED     1312
  TCP    192.168.100.2:9001     198.23.194.149:43970   ESTABLISHED     1312
  TCP    192.168.100.2:9001     198.49.73.11:38842     ESTABLISHED     1312

#28


0  

A port denotes a communication endpoint in the TCP and UDP transports for the IP network protocol. A socket is a software abstraction for a communication endpoint commonly used in implementations of these protocols (socket API). An alternative implementation is the XTI/TLI API.

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

See also:

参见:

Stevens, W. R. 1998, UNIX Network Programming: Networking APIs: Sockets and XTI; Volume 1, Prentice Hall.
Stevens, W. R., 1994, TCP/IP Illustrated, Volume 1: The Protocols, Addison-Wesley.

1998年,《UNIX网络编程:网络api: socket和XTI》;卷1,普伦蒂斯霍尔。史蒂文斯,w·R。1994年,TCP/IP说明,第1卷:协议,Addison-Wesley。

#29


0  

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.

我知道有很多解释。但是,有一个更简单的方法可以用实例来理解。我们都可以连接到HTTP端口80,但是它是否意味着只有一个用户可以一次连接到这个端口?答案显然是“不”。多个用途的多个用户可以访问HTTP端口80,但是他们仍然可以从服务器得到正确的响应,不是吗?现在想一下,怎么样?。是的,您是正确的,它的IP地址唯一地标识不同的用户,他们联系的目的不同。如果您在到达这里之前已经阅读了前面的答案,您就会知道IP地址是套接字包含的信息的一部分。想想看,没有插座的通信是可能的吗?答案是肯定的,但是你不能在一个端口上运行多个应用程序,但是我们知道我们不是一个只在硬件上运行的“转储”开关。

#30


-1  

A port is an entity that is used by networking protocols to attain access to connected hosts. Ports could be application-specific or related to a certain communication medium. Different protocols use different ports to access the hosts, like HTTP uses port 80 or FTP uses port 23. You can assign user-defined port numbers in your application, but they should be above 1023.

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

Ports open up the connection to the required host while sockets are an endpoint in an inter-network or an inter-process communication. Sockets are assigned by APIs(Application Programming Interface) by the system.

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

A more subtle difference can be made saying that, when a system is rebooted ports will be present while the sockets will be destroyed.

更细微的差别可以这样说:当一个系统被重新引导时,端口将会出现,而套接字将被销毁。

#1


762  

Summary

A TCP socket is an endpoint instance defined by an IP address and a port in the context of either a particular TCP connection or the listening state.

TCP套接字是一个端点实例,它是由一个IP地址和一个端口在特定的TCP连接或监听状态下定义的。

A port is a virtualisation identifier defining a service endpoint (as distinct from a service instance endpoint aka session identifier).

端口是定义服务端点的虚拟化标识符(与服务实例端点aka会话标识符不同)。

A TCP socket is not a connection, it is the endpoint of a specific connection.

TCP套接字不是连接,它是特定连接的端点。

There can be concurrent connections to a service endpoint, because a connection is identified by both its local and remote endpoints, allowing traffic to be routed to a specific service instance.

可以并发连接到服务端点,因为它的本地和远程端点都标识了连接,从而允许将通信路由到特定的服务实例。

There can only be one listener socket for a given address/port combination.

对于给定的地址/端口组合,只能有一个侦听器套接字。

Exposition

This was an interesting question that forced me to re-examine a number of things I thought I knew inside out. You'd think a name like "socket" would be self-explanatory: it was obviously chosen to evoke imagery of the endpoint into which you plug a network cable, there being strong functional parallels. Nevertheless, in network parlance the word "socket" carries so much baggage that a careful re-examination is necessary.

这是一个很有趣的问题,迫使我重新审视了一些我认为自己知道的事情。你可能会认为像“套接字”这样的名字是不言自明的:显然,它是被选择来唤醒你插入网络电缆的端点的图像,有强大的功能相似之处。然而,在网络用语中,“插座”这个词承载了太多的包袱,需要仔细的重新检查。

In the broadest possible sense, a port is a point of ingress or egress. Although not used in a networking context, the French word porte literally means door or gateway, further emphasising the fact that ports are transportation endpoints whether you ship data or big steel containers.

在最广泛的意义上,港口是入口或出口的一个点。虽然没有在网络环境中使用,但法语单词porte字面上的意思是门或网关,它进一步强调了一个事实,即港口是运输端点,无论你是船舶数据还是大型钢容器。

For the purpose of this discussion I will limit consideration to the context of TCP-IP networks. The OSI model is all very well but has never been completely implemented, much less widely deployed in high-traffic high-stress conditions.

为了这次讨论的目的,我将限制对TCP-IP网络上下文的考虑。OSI模型非常好,但是从来没有完全实现过,在高流量的高压力条件下部署得更少。

The combination of an IP address and a port is strictly known as an endpoint and is sometimes called a socket. This usage originates with RFC793, the original TCP specification.

IP地址和端口的组合被严格称为端点,有时被称为套接字。这个用法源于RFC793,最初的TCP规范。

A TCP connection is defined by two endpoints aka sockets.

TCP连接由两个端点(即套接字)定义。

An endpoint (socket) is defined by the combination of a network address and a port identifier. Note that address/port does not completely identify a socket (more on this later).

端点(套接字)由网络地址和端口标识符的组合来定义。注意,地址/端口不能完全识别套接字(稍后将对此进行详细说明)。

The purpose of ports is to differentiate multiple endpoints on a given network address. You could say that a port is a virtualised endpoint. This virtualisation makes multiple concurrent connections on a single network interface possible.

端口的目的是在给定的网络地址上区分多个端点。您可以说一个端口是一个虚拟端点。这种虚拟化可以在单个网络接口上实现多个并发连接。

It is the socket pair (the 4-tuple consisting of the client IP address, client port number, server IP address, and server port number) that specifies the two endpoints that uniquely identifies each TCP connection in an internet. (TCP-IP Illustrated Volume 1, W. Richard Stevens)

它是套接字对(由客户端IP地址、客户端端口号、服务器IP地址和服务器端口号组成的4元组),它指定在internet中唯一标识每个TCP连接的两个端点。(TCP-IP插图卷1,W. Richard Stevens)

In most C-derived languages, TCP connections are established and manipulated using methods on an instance of a Socket class. Although it is common to operate on a higher level of abstraction, typically an instance of a NetworkStream class, this generally exposes a reference to a socket object. To the coder this socket object appears to represent the connection because the connection is created and manipulated using methods of the socket object.

在大多数c派生语言中,TCP连接是通过在套接字类的实例上使用方法来建立和操作的。尽管在更高级别的抽象(通常是NetworkStream类的实例)上操作是很常见的,但这通常会暴露对套接字对象的引用。对于编码器,这个套接字对象似乎表示连接,因为连接是用套接字对象的方法创建和操作的。

In C#, to establish a TCP connection (to an existing listener) first you create a TcpClient. If you don't specify an endpoint to the TcpClient constructor it uses defaults - one way or another the local endpoint is defined. Then you invoke the Connect method on the instance you've created. This method requires a parameter describing the other endpoint.

在c#中,首先要创建一个TCP连接(对于现有的侦听器),然后创建一个TcpClient。如果您没有为TcpClient构造函数指定一个端点,那么它将使用缺省值—一个或另一个方法定义本地端点。然后,在创建的实例上调用Connect方法。此方法需要一个描述其他端点的参数。

All this is a bit confusing and leads you to believe that a socket is a connection, which is bollocks. I was labouring under this misapprehension until Richard Dorman asked the question.

所有这些都有点混乱,让您相信一个套接字是一个连接,即bollocks。我一直在为这种误解而苦恼,直到理查德·多曼问了这个问题。

Having done a lot of reading and thinking, I'm now convinced that it would make a lot more sense to have a class TcpConnection with a constructor that takes two arguments, LocalEndpoint and RemoteEndpoint. You could probably support a single argument RemoteEndpoint when defaults are acceptable for the local endpoint. This is ambiguous on multihomed computers, but the ambiguity can be resolved using the routing table by selecting the interface with the shortest route to the remote endpoint.

在做了大量的阅读和思考之后,我现在确信,拥有一个带有两个参数、LocalEndpoint和RemoteEndpoint的构造函数的类TcpConnection会更有意义。当本地端点可以接受缺省值时,您可能会支持一个参数RemoteEndpoint。这在多主机计算机上是不明确的,但是可以使用路由表通过选择最短路由到远程端点的接口来解决歧义。

Clarity would be enhanced in other respects, too. A socket is not identified by the combination of IP address and port:

透明度也会在其他方面得到加强。IP地址和端口的组合不能识别套接字:

[...]TCP demultiplexes incoming segments using all four values that comprise the local and foreign addresses: destination IP address, destination port number, source IP address, and source port number. TCP cannot determine which process gets an incoming segment by looking at the destination port only. Also, the only one of the [various] endpoints at [a given port number] that will receive incoming connection requests is the one in the listen state. (p255, TCP-IP Illustrated Volume 1, W. Richard Stevens)

[…]TCP demultiplexes传入段使用包含本地和外部地址的所有四个值:目标IP地址、目的端口号、源IP地址和源端口号。通过只查看目标端口,TCP无法确定哪个进程获得传入段。另外,在[给定端口号]中,将接收传入连接请求的[各种]端点中唯一的一个是侦听状态中的一个。(p255, TCP-IP插图卷1,W. Richard Stevens)

As you can see, it is not just possible but quite likely for a network service to have numerous sockets with the same address/port, but only one listener socket on a particular address/port combination. Typical library implementations present a socket class, an instance of which is used to create and manage a connection. This is extremely unfortunate, since it causes confusion and has lead to widespread conflation of the two concepts.

正如您所看到的,网络服务不仅有可能,而且很可能有多个具有相同地址/端口的套接字,而只有一个特定地址/端口组合的侦听器套接字。典型的库实现提供了一个socket类,一个用于创建和管理连接的实例。这是极其不幸的,因为它引起混乱,并导致了这两个概念的广泛合并。

Hagrawal doesn't believe me (see comments) so here's a real sample. I connected a web browser to http://dilbert.com and then ran netstat -an -p tcp. The last six lines of the output contain two examples of the fact that address and port are not enough to uniquely identify a socket. There are two distinct connections between 192.168.1.3 (my workstation) and 54.252.92.236:80

Hagrawal不相信我(见注释),所以这是一个真实的例子。我将一个web浏览器连接到http://dilbert.com,然后运行netstat -an -p tcp。输出的最后6行包含两个示例,说明地址和端口不足以唯一标识套接字。在192.168.1.3(我的工作站)和54.252.92.236:80之间有两个明显的连接。

  TCP    192.168.1.3:63240      54.252.94.236:80       SYN_SENT
  TCP    192.168.1.3:63241      54.252.94.236:80       SYN_SENT
  TCP    192.168.1.3:63242      207.38.110.62:80       SYN_SENT
  TCP    192.168.1.3:63243      207.38.110.62:80       SYN_SENT
  TCP    192.168.1.3:64161      65.54.225.168:443      ESTABLISHED

Since a socket is the endpoint of a connection, there are two sockets with the address/port combination 207.38.110.62:80 and two more with the address/port combination 54.252.94.236:80.

由于套接字是连接的端点,所以有两个带地址/端口组合的套接字:207.38.110.62:80,还有两个带地址/端口组合的套接字:54.252.94.236 . 80。

I think Hagrawal's misunderstanding arises from my very careful use of the word "identifies". I mean "completely, unambiguously and uniquely identifies". In the above sample there are two endpoints with the address/port combination 54.252.94.236:80. If all you have is address and port, you don't have enough information to tell these sockets apart. It's not enough information to identify a socket.

我认为Hagrawal的误解来自于我非常谨慎地使用“识别”这个词。我的意思是“完全、明确、唯一地识别”。在上面的示例中,有两个端点,地址/端口组合为54.252.94.236:80。如果您所拥有的只是地址和端口,那么您就没有足够的信息来区分这些套接字。没有足够的信息来识别套接字。

Addendum

Paragraph two of section 2.7 of RFC793 says

RFC793第2.7节第2段说。

A connection is fully specified by the pair of sockets at the ends. A local socket may participate in many connections to different foreign sockets.

连接由两端的套接字完全指定。本地套接字可以参与许多连接到不同的外国套接字。

This definition of socket is not helpful from a programming perspective because it is not the same as a socket object, which is the endpoint of a particular connection. To a programmer, and most of this question's audience are programmers, this is a vital functional difference.

从编程的角度来看,这个套接字的定义并不是很有用,因为它与套接字对象不同,后者是特定连接的端点。对于程序员来说,这个问题的大部分听众都是程序员,这是一个至关重要的功能差异。

References

  1. TCP-IP Illustrated Volume 1 The Protocols, W. Richard Stevens, 1994 Addison Wesley

    《协议》第1卷《议定书》,W. Richard Stevens, 1994年Addison Wesley。

  2. RFC793, Information Sciences Institute, University of Southern California for DARPA

    RFC793,南加利福尼亚大学信息科学研究所。

  3. RFC147, The Definition of a Socket, Joel M. Winett, Lincoln Laboratory

    RFC147,一个插座的定义,Joel M. Winett,林肯实验室。

#2


144  

A socket consists of three things:

套接字由三件事组成:

  1. An IP address
  2. 一个IP地址
  3. A transport protocol
  4. 传输协议
  5. A port number
  6. 端口号

A port is a number between 1 and 65535 inclusive that signifies a logical gate in a device. Every connection between a client and server requires a unique socket.

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

For example:

例如:

  • 1030 is a port.
  • 1030是一个港口。
  • (10.1.1.2 , TCP , port 1030) is a socket.
  • (10.1.1.2,TCP,端口1030)是一个套接字。

#3


82  

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.

套接字表示两个网络应用程序之间的单个连接。这两个应用程序名义上运行在不同的计算机上,但是套接字也可以用于单个计算机上的进程间通信。应用程序可以创建多个套接字来进行通信。套接字是双向的,这意味着连接的任何一端都能够发送和接收数据。因此,从理论上可以在OSI模型的任何级别上从2向上创建套接字。程序员经常在网络编程中使用套接字,尽管是间接的。像Winsock这样的编程库隐藏了许多套接字编程的底层细节。自上世纪80年代初以来,插座就被广泛使用。

A port represents an endpoint or "channel" for network communications. Port numbers allow different applications on the same computer to utilize network resources without interfering with each other. Port numbers most commonly appear in network programming, particularly socket programming. Sometimes, though, port numbers are made visible to the casual user. For example, some Web sites a person visits on the Internet use a URL like the following:

端口代表网络通信的端点或“通道”。端口号允许不同的应用程序在同一台计算机上使用网络资源而不互相干扰。端口号最常出现在网络编程中,尤其是套接字编程。不过,有时对临时用户可见端口号。例如,一些网站在互联网*问的人使用的网址如下:

http://www.mairie-metz.fr:8080/ In this example, the number 8080 refers to the port number used by the Web browser to connect to the Web server. Normally, a Web site uses port number 80 and this number need not be included with the URL (although it can be).

在本例中,数字8080是指Web浏览器用于连接到Web服务器的端口号。通常,Web站点使用端口80,而这个数字不需要包含在URL中(尽管可以是)。

In IP networking, port numbers can theoretically range from 0 to 65535. Most popular network applications, though, use port numbers at the low end of the range (such as 80 for HTTP).

在IP网络中,端口号在理论上可以从0到65535。不过,大多数流行的网络应用程序使用的端口号都是低端的(比如HTTP的80)。

Note: The term port also refers to several other aspects of network technology. A port can refer to a physical connection point for peripheral devices such as serial, parallel, and USB ports. The term port also refers to certain Ethernet connection points, such as those on a hub, switch, or router.

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

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

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

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

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

#4


40  

Firsty, I think we should start with a little understanding of what constitutes getting a packet from A to B.

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

A common definition for a network is the use of the OSI Model which separates a network out into a number of layers according to purpose. There are a few important ones, which we'll cover here:

网络的一个常见定义是使用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.
  • 数据链路层。这一层负责将数据包从一个网络设备发送到另一个网络设备,并在实际执行传输的层之上。它谈到了MAC地址,知道如何根据MAC(硬件)地址找到主机,但没有更多。
  • 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.
  • 网络层是允许您跨机器和物理边界(例如物理设备)传输数据的层。网络层本质上必须支持一个附加的基于地址的机制,它与物理地址有关;输入Internet协议(IPv4)。IP地址可以通过internet从A到B获得数据包,但是不知道如何遍历单个的hops。这是由上面的层按照路由信息处理的。
  • 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.
  • 传输层。这一层负责定义信息从A到B的方式,以及对该行为的任何限制、检查或错误。例如,TCP将附加信息添加到包中,这样就可以推断包是否丢失了。

TCP contains, amongst other things, the concept of ports. These are effectively different data endpoints on the same IP address to which an Internet Socket (AF_INET) can bind.

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

As it happens, so too does UDP, and other transport layer protocols. They don't technically need to feature ports, but these ports do provide a way for multiple applications in the layers above to use the same computer to receive (and indeed make) outgoing connections.

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

Which brings us to the anatomy of a TCP or UDP connection. Each features a source port and address, and a target port and address. This is so that in any given session, the target application can respond, as well as receive, from the source.

这就引出了一个TCP或UDP连接的剖析。每个特性都有一个源端口和地址,以及一个目标端口和地址。这是这样的,在任何给定的会话中,目标应用程序都可以从源中响应,也可以接收。

So ports are essentially a specification-mandated way of allowing multiple concurrent connections sharing the same address.

因此,端口本质上是一种特定的授权方式,允许多个并发连接共享同一个地址。

Now, we need to take a look at how you communicate from an application point of view to the outside world. To do this, you need to kindly ask your operating system and since most OSes support the Berkeley Sockets way of doing things, we see we can create sockets involving ports from an application like this:

现在,我们需要了解一下如何从应用程序的角度与外部世界进行通信。要做到这一点,您需要友好地询问您的操作系统,因为大多数OSes都支持Berkeley套接字的方法,我们看到我们可以在这样的应用程序中创建包含端口的套接字:

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

Great! So in the sockaddr structures, we'll specify our port and bam! Job done! Well, almost, except:

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

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

is also possible. Urgh, that's thrown a spanner in the works!

也是可能的。呃,那是一个扳手在工作!

Ok, well actually it hasn't. All we need to do is come up with some appropriate definitions:

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

  • An internet socket is the combination of an IP address, a protocol and its associated port number on which a service may provide data. So tcp port 80, *.com is an internet socket.
  • internet套接字是一个IP地址、一个协议和一个服务可以提供数据的相关端口号的组合。所以tcp端口80,*.com是一个网络插座。
  • An unix socket is an IPC endpoint represented in the file system, e.g. /var/run/database.sock.
  • unix套接字是在文件系统中表示的IPC端点,例如/var/run/database.sock。
  • A socket API is a method of requesting an application be able to read and write data to a socket.
  • 套接字API是一种请求应用程序能够读写数据到套接字的方法。

Voila! That tidies things up. So in our scheme then,

瞧!将地事情。所以在我们的计划中,

  • A port is a numeric identifier which, as part of a transport layer protocol, identifies the service number which should respond to the given request.
  • 端口是一个数字标识符,作为传输层协议的一部分,它标识应该响应给定请求的服务编号。

So really a port is a subset of the requirements for forming an internet socket. Unfortunately, it just so happens that the meaning of the word socket has been applied to several different ideas. So I heartily advise you name your next project socket, just to add to the confusion ;)

因此,真正的端口是形成internet套接字的需求的子集。不幸的是,“套接字”的含义已经被应用到几个不同的概念上。所以我衷心地建议你说出下一个项目的名称,只是为了增加混乱。

#5


37  

With some analogy

用一些比喻

Although a lot technical stuff is already given above for sockets... I would like to add my answer, just in case , if somebody still could not feel the difference between ip, port and sockets

虽然有很多技术上的东西已经给了插座……我想添加我的答案,以防万一有人仍然感觉不到ip、端口和套接字之间的区别。

Consider a server S,

考虑一个服务器,

and say person X,Y,Z need a service (say chat service) from that server S

并且说person X,Y,Z需要一个服务(比如聊天服务)。

then

然后

IP address tells --> who? is that chat server 'S' with whom X,Y,Z want to contact

IP地址告诉——>谁?是聊天服务器的“与谁X,Y,Z想联系?”

okay, you got "who is the server"

好的,你得到了"谁是服务器"

but suppose that server 'S' is also providing some other services to other people,say 'S' provides storage services to person A,B,C

但是假设服务器'S'也为其他人提供一些其他服务,比如'S'提供存储服务给A,B,C。

then

然后

port tells ---> which? service you (X,Y,Z) need i.e. chat service and not that storage service

港告诉- - - >哪些吗?服务您(X,Y,Z)需要的是聊天服务,而不是存储服务。

okay.., you Server came to know that 'chat service' is what you want and not the storage

好吧. .你的服务器知道“聊天服务”是你想要的,而不是存储。

but

you are three and the server might want to identify all the three differently

您是三个,服务器可能想要识别所有三个不同的。

there comes the socket

有套接字

now socket tells--> which one? particular connection

socket告诉我们,>是哪个?特定的连接

that is , say ,

也就是说,

socket 1 for person X

插座1给人X。

socket 2 for person Y

插座2给人Y。

and socket 3 for person Z

和插座3给人Z。

I hope it helps someone who was still confused :)

我希望它能帮助那些仍然困惑的人:

#6


22  

There seems to be a lot of answers equating socket with the connection between 2 PC's..which I think is absolutely incorrect. A socket has always been the endpoint on 1 PC, that may or may not be connected - surely we've all used listener or UDP sockets* at some point. The important part is that it's addressable and active. Sending a message to 1.1.1.1:1234 is not likely to work, as there is no socket defined for that endpoint.

似乎有很多答案将套接字与两台PC的连接连接起来。我认为这是绝对错误的。一个套接字一直是1个PC上的端点,它可能是连接的,也可能不是连接的——在某个点上,我们肯定都使用了侦听器或UDP套接字。重要的是它是可寻址和活跃的。将消息发送到1.1.1.1:1234是不太可能的,因为没有为该端点定义套接字。

Sockets are protocol specific - so the implementation of uniqueness that both TCP/IP and UDP/IP uses* (ipaddress:port), is different than eg., IPX (Network, Node, and...ahem, socket - but a different socket than is meant by the general "socket" term. IPX socket numbers are equivalent to IP ports). But, they all offer a unique addressable endpoint.

套接字是特定于协议的,所以TCP/IP和UDP/IP使用* (ipaddress:port)的惟一性的实现不同于eg。,IPX(网络,节点,和…嗯,套接字——但是一个不同的套接字,而不是一般的“套接字”术语。IPX套接字数字相当于IP端口。但是,它们都提供了一个独特的可寻址端点。

Since IP has become the dominant protocol, a port (in networking terms) has become synonomous with either a UDP or TCP port number - which is a portion of the socket address.

由于IP已经成为主导协议,一个端口(在网络术语中)与UDP或TCP端口号(这是套接字地址的一部分)是同步的。

  • UDP is connection-less - meaning no virtual circuit between the 2 endpoints is ever created. However, we still refer to UDP sockets as the endpoint. The API functions make it clear that both are just different type of sockets - SOCK_DGRAM is UDP (just sending a message) and SOCK_STREAM is TCP (creating a virtual circuit).

    UDP是无连接的,这意味着在两个端点之间没有创建任何虚拟电路。然而,我们仍然将UDP套接字作为端点。API函数清楚地表明,它们都是不同类型的套接字——SOCK_DGRAM是UDP(只发送一条消息),而SOCK_STREAM是TCP(创建一个虚拟电路)。

  • Technically, the IP header holds the IP Address, and the protocol on top of IP (UDP or TCP) holds the port number. This makes it possible to have other protocols (eg. ICMP that have no port numbers, but do have IP addressing information).

    技术上,IP报头保存IP地址,IP地址(UDP或TCP)上的协议保留端口号。这使得有其他的协议成为可能。没有端口号的ICMP,但有IP寻址信息。

#7


21  

Short brief answer.

简短的答案。

A port can be described as an internal address within a host that identifies a program or process.

一个端口可以被描述为一个主机的内部地址,它标识一个程序或进程。

A socket can be described as a programming interface allowing a program to communicate with other programs or processes, on the internet, or locally.

一个套接字可以被描述为一个编程接口,允许一个程序与其他程序或进程,在因特网上或本地进行通信。

#8


20  

A socket = IP Address + a port (numeric address)
Together they identify an end-point for a network connection on a machine. (Did I just flunk network 101?)

一个套接字= IP地址+一个端口(数字地址),它们一起确定一个机器上的网络连接的端点。(我只是不及格吗?)

#9


19  

Generally you will get a lot of theoretical but one of the easiest ways to differentiate these two concepts is as follows:

一般来说,你会得到很多理论但最简单的区分这两个概念的方法是:

In order to get a service you need a service number. This service number is called a port. Simple as that.

为了获得服务,您需要一个服务编号。这个服务号码被称为端口。就这么简单。

For example, the HTTP as a service is running on port 80.

例如,HTTP作为服务在端口80上运行。

Now, many people can request the service and a connection from client- server has established. There will be a lot of connections. Each connection represent a client. In order to maintain each of the connection, the server creates socket per connection to maintain it's client.

现在,许多人可以请求服务和从客户端服务器建立连接。会有很多的联系。每个连接代表一个客户端。为了维护每个连接,服务器为维护它的客户端创建每个连接的套接字。

Hope this helps.Thank you.

希望这个有帮助。谢谢你!

#10


14  

They are terms from two different domains: 'port' is a concept from TCP/IP networking, 'socket' is an API (programming) thing. A 'socket' is made (in code) by taking a port and a hostname or network adapter and combining them into a data structure that you can use to send or receive data.

它们是来自两个不同领域的术语:“端口”是一个来自TCP/IP网络的概念,“socket”是一个API(编程)的东西。“套接字”是通过取一个端口和一个主机名或网络适配器,并将它们组合成一个可以用来发送或接收数据的数据结构来实现的(在代码中)。

#11


10  

After reading the excellent up-voted answers, I found that the following point needed emphasis for me, a newcomer to network programming:

在阅读了优秀的上选答案后,我发现我需要强调以下几点:网络编程的新手:

TCP-IP connections are bi-directional pathways connecting one address:port combination with another address:port combination. Therefore, whenever you open a connection from your local machine to a port on a remote server (say www.google.com:80), you are also associating a new port number on your machine with the connection, to allow the server to send things back to you, (e.g. 127.0.0.1:65234). It can be helpful to use netstat to look at your machine's connections:

TCP-IP连接是连接一个地址的双向通道:端口组合与另一个地址:端口组合。因此,当您从本地机器打开到远程服务器上的端口的连接时(比如www.google.com:80),您也会将一个新的端口号与连接在您的机器上,以便服务器将其发回给您(例如:127.0.0.1:65234)。使用netstat查看您的机器的连接是很有帮助的:

> netstat -nWp tcp (on OS X)
Active Internet connections
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)    
tcp4       0      0  192.168.0.6.49871      17.172.232.57.5223     ESTABLISHED
...

#12


9  

A socket is a special type of file handle which is used by a process to request network services from the operating system. A socket address is the triple: {protocol, local-address, local-process} where the local process is identified by a port number.

套接字是一种特殊类型的文件句柄,用于从操作系统请求网络服务。套接字地址是三重:{协议、本地地址、本地进程},本地进程由端口号标识。

In the TCP/IP suite, for example:

在TCP/IP套件中,例如:

{tcp, 193.44.234.3, 12345}

{ tcp 193.44.234.3 12345 }

A conversation is the communication link between two processes thus depicting an association between two. An association is the 5-tuple that completely specifies the two processes that comprise a connection: {protocol, local-address, local-process, foreign-address, foreign-process}

会话是两个过程之间的通信链路,因此描述了两者之间的关联。一个关联是5元组,它完全指定包含连接的两个进程:{协议、本地地址、本地进程、外地址、外进程}

In the TCP/IP suite, for example:

在TCP/IP套件中,例如:

{tcp, 193.44.234.3, 1500, 193.44.234.5, 21}

{tcp, 193.44.234.3, 1500, 193.44.234.5, 21}

could be a valid association.

可以是一个有效的关联。

A half-association is either: {protocol, local-address, local-process}

半关联是:{协议,本地地址,本地进程}

or

{protocol, foreign-address, foreign-process}

{协议,国外住址,foreign-process }

which specify each half of a connection.

它指定连接的每一半。

The half-association is also called a socket or a transport address. That is, a socket is an end point for communication that can be named and addressed in a network. The socket interface is one of several application programming interfaces (APIs) to the communication protocols. Designed to be a generic communication programming interface, it was first introduced by the 4.2BSD UNIX system. Although it has not been standardized, it has become a de facto industry standard.

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

#13


6  

A socket address is an IP address & port number

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

A connection occurs when 2 sockets are bound together.

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

#14


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.

应用程序由对网络(客户机-服务器对)进行通信的两个进程组成。这些进程通过一个称为套接字的软件接口发送和接收消息。考虑到书中所提到的“计算机网络:自上而下的方法”。有一所房子想与其他房子沟通。在这里,house类似于一个过程,而门则是一个套接字。发送过程假定在门的另一端有一个基础设施,将数据传输到目的地。一旦信息到达另一边,它就通过接收器的门(插座)进入房子(过程)。来自同一本书的插图可以帮助您:套接字是传输层的一部分,它为应用程序提供逻辑通信。这意味着,从应用程序的角度来看,两个主机都是直接连接的,即使它们之间有许多路由器和/或交换机。因此,套接字本身不是连接本身,而是连接的端点。传输层协议只在主机上实现,而不是在中间路由器上实现。端口提供了机器内部寻址的方法。其主要目的是允许多个进程通过网络发送和接收数据,而不会干扰其他进程(它们的数据)。所有的套接字都有一个端口号。当一个段到达主机时,传输层检查该区段的目的端口号。然后将该部分转发到相应的套接字。将传输层段中的数据传输到正确的套接字的工作称为去多路复用。然后将该段的数据转发给连接到套接字的进程。

#15


4  

from Oracle Java Tutorial:

从甲骨文Java教程:

A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to.

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

#16


4  

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.

套接字是一个通信端点。套接字与TCP/IP协议家族没有直接关系,它可以与您的系统支持的任何协议一起使用。C的套接字API希望你先从系统获得一个空白的套接字对象,然后,您可以绑定到本地套接字地址(直接检索的传入流量无连接协议或接受传入的连接请求为面向连接协议),或者您可以连接到一个远程套接字地址(协议)。您甚至可以同时进行这两种操作,如果您想要同时控制它们,那么本地套接字地址将绑定到一个套接字,而远程套接字地址则连接到一个套接字。连接一个套接字的无连接协议甚至是可选的,但是如果您不这样做,您还必须将您想要发送的每个包的目标地址传递给这个套接字,就像套接字知道将这些数据发送到哪里一样?优点是您可以使用单个套接字将数据包发送到不同的套接字地址。一旦您配置好了套接字,甚至连接了它,请将其视为双向通信管道。您可以使用它将数据传递到某个目的地,并且某些目的地可以使用它将数据传递给您。你给套接字写的东西被发送出去,收到的是可以阅读的。

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, always the same server address and the same source IP, as they all come from the same client, but as they have a 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.

另一方面,端口只是TCP/IP协议栈的某些协议。TCP和UDP包都有端口。一个端口就是一个简单的数字。源端口和目的端口的组合标识了两个主机之间的通信通道。例如,您可能有一个服务器,一个简单的HTTP服务器和一个简单的FTP服务器。如果现在有一个数据包到达该服务器的地址,它如何知道这是HTTP或FTP服务器的数据包?它将会知道,因为HTTP服务器将在端口80上运行,端口21上的FTP服务器将运行,所以如果数据包到达目的端口80,它是为HTTP服务器而不是为FTP服务器。另外,包有一个源端口,因为没有这样的源端口,服务器只能一次连接一个IP地址。服务器的源端口可以区分否则相同的连接:他们都有相同的目的港,如端口80,同样的目的地IP,总是相同的服务器地址和相同的源IP,因为他们都来自同一个客户,但他们有一个不同的源端口,服务器可以区分彼此。当服务器发送回回复时,它会对请求来自的端口这样做,这样客户端也可以区分收到的不同的回复。

#17


3  

Relative TCP/IP terminology which is what I assume is implied by the question. In layman's terms:

相对的TCP/IP术语,这是我假设的问题。通俗的说:

A PORT is like the telephone number of a particular house in a particular zip code. The ZIP code of the town could be thought of as the IP address of the town and all the houses in that town.

端口就像特定邮政编码中的某一特定房屋的电话号码。城镇的邮政编码可以被认为是城镇的IP地址和镇上所有的房子。

A SOCKET on the other hand is more like an established phone call between telephones of a pair of houses talking to each other. Those calls can be established between houses in the same town or two houses in different towns. It's that temporary established pathway between the pair of phones talking to each other that is the SOCKET.

另一方面,套接字更像是一对相互交谈的房子之间的电话。这些电话可以在同一城镇的房屋和不同城镇的两幢房子之间建立。这是一种临时搭建的通道,在两部手机之间相互交谈,这就是插座。

#18


3  

A socket is a structure in your software. It's more-or-less a file; it has operations like read and write. It isn't a physical thing; it's a way for your software to refer to physical things.

套接字是软件中的一个结构。这是或多或少一个文件;它有读和写操作。它不是物质的;这是一种让你的软件引用物理事物的方法。

A port is a device-like thing. Each host has one or more networks (those are physical); a host has an address on each network. Each address can have thousands of ports.

端口是设备一样的东西。每个主机都有一个或多个网络(这些网络是物理的);主机在每个网络上都有一个地址。每个地址可以有数千个端口。

One socket only may be using a port at an address. The socket allocates the port approximately like allocating a device for file system I/O. Once the port is allocated, no other socket can connect to that port. The port will be freed when the socket is closed.

一个套接字只能在地址上使用端口。套接字分配端口近似于为文件系统I/O分配一个设备。一旦端口被分配,没有其他的套接字可以连接到那个端口。当套接字关闭时,端口将被释放。

Take a look at TCP/IP Terminology.

看看TCP/IP的术语。

#19


3  

The port was the easiest part, it is just a unique identifier for a socket. A socket is something processes can use to establish connections and to communicate with each other. Tall Jeff had a great telephone analogy which was not perfect, so I decided to fix it:

端口是最简单的部分,它只是套接字的唯一标识符。套接字是一些进程可以用来建立连接和相互通信的过程。高大的杰夫有一个很好的电话类比,但并不完美,所以我决定改正它:

  • ip and port ~ phone number
  • ip和端口~电话号码。
  • socket ~ phone device
  • 套接字~电话设备
  • connection ~ phone call
  • 电话联系~
  • establishing connection ~ calling a number
  • 建立连接~调用一个数字。
  • processes, remote applications ~ people
  • 进程,远程应用程序~人。
  • messages ~ speech
  • 消息~演讲

#20


2  

A socket is a data I/O mechanism. A port is a contractual concept of a communication protocol. A socket can exist without a port. A port can exist witout a specific socket (e.g. if several sockets are active on the same port, which may be allowed for some protocols).

套接字是一个数据I/O机制。端口是通信协议的契约概念。套接字可以在没有端口的情况下存在。一个端口可以存在一个特定的套接字(例如,如果多个套接字在同一个端口上是活动的,这可能被允许用于某些协议)。

A port is used to determine which socket the receiver should route the packet to, with many protocols, but it is not always required and the receiving socket selection can be done by other means - a port is entirely a tool used by the protocol handler in the network subsystem. e.g. if a protocol does not use a port, packets can go to all listening sockets or any socket.

端口是用来确定哪些套接字接收方应将数据包路由到,许多协议,但它并不总是需要和接受套接字选择可以通过其他方式——一个端口协议处理器使用的完全是一个工具在网络子系统。如果一个协议不使用一个端口,数据包可以进入所有监听套接字或任何套接字。

#21


2  

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)

从广义上讲,插座——就是插座,就像你的电线、电缆或电话插座一样。“必要的东西”(权力、信号、信息)可以从这里出去。它隐藏了大量的细节,这并不需要使用“必要的东西”。在软件术语中,它提供了一种通用的方式来定义两个实体之间的通信机制(这些实体可以是任何东西——两个应用程序、两个物理独立的设备、一个操作系统中的用户和内核空间等等)。

A Port is an endpoint discriminator. It differentiates one endpoint from another. At networking level, it differentiates one application from another, so that the networking stack can pass on information to the appropriate application.

端口是一个端点鉴别器。它将一个端点与另一个端点区分开来。在网络级别上,它将一个应用程序与另一个应用程序区分开来,这样网络堆栈就可以将信息传递给适当的应用程序。

#22


2  

Already theoretical answers have been given to this question. I would like to give a practical example to this question, which will clear your understanding about Socket and Port.

这个问题已经得到了理论的答案。我想举一个实际的例子来说明这个问题,这将使你对插座和端口的理解更加清晰。

I found it here

我发现在这里

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.

这个例子将引导您通过连接到一个网站的过程,例如Wiley。你可以打开你的浏览器(比如Mozilla Firefox),然后在地址栏中输入www.wiley.com。您的web浏览器使用域名系统(DNS)服务器查找名称www.wiley.com,以确定其IP地址。在本例中,地址是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.

Firefox将连接到192.0.2.100地址,并连接到应用程序层web服务器正在运行的端口。Firefox知道应该期待什么端口,因为它是一个众所周知的端口。web服务器的著名端口是TCP端口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.

Firefox尝试连接的目标套接字被写入套接字:端口,或者在这个例子中,192.0.2.100:80。这是连接的服务器端,但是服务器需要知道如何将您想要查看的web页面发送到Mozilla Firefox中,因此您也可以在连接的客户端有一个套接字。

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.

客户端连接由您的IP地址组成,如192.168.1.25和随机选择的动态端口号。与Firefox相关的套接字看起来像192.168.1.25:49175。因为web服务器在TCP端口80上操作,所以这两个套接字都是TCP套接字,而如果连接到在UDP端口上运行的服务器,服务器和客户端套接字都是UDP套接字。

#23


2  

Socket is an abstraction provided by kernel to user applications for data I/O. A socket type is defined by the protocol its 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.

套接字是内核向用户应用程序提供的数据I/O的抽象。套接字类型定义的协议处理、IPC沟通等等。如果有人创建了一个TCP套接字他可以做操作喜欢阅读和写数据套接字数据的简单方法和低层协议TCP转换和转发数据包处理底层网络协议是通过特定的套接字实现的内核。好处是用户不必担心处理协议特定的nitigrities,而应该像普通缓冲区那样读写数据。在IPC的情况下也是如此,用户只是读写数据到套接字,而内核根据所创建的套接字的类型处理所有低级的细节。

Port together with IP is like providing an address to the socket, though its not necessary, but it helps in network communications.

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

#24


2  

Port and socket can be compared to the Bank Branch.

端口和套接字可以与银行分支进行比较。

The building number of the "Bank" is analogous to IP address . Bank has got different sections like :1) SAVINGS ACCOUNT DEPARTMENT 2) PERSONAL LOAN DEPARTMENT 3) HOME LOAN DEPARTMENT 4) GRIEVANCE DEPARTMENT .

“银行”的建筑编号类似于IP地址。银行有不同的部门:1)储蓄账户2)个人贷款部3)住房贷款部门4)申诉部。

so 1(SAVINGS ACCOUNT DEPARTMENT) ,2 ( PERSONAL LOAN DEPARTMENT) ,3(HOME LOAN DEPARTMENT) and 4(GRIEVANCE DEPARTMENT) are Ports.

所以1(储蓄账户部门),2(个人贷款部门),3(住房贷款部门)和4(申诉部)是港口。

Now let us say you go to open a savings account , you go to Bank(IP address) then you go to "SAVINGS ACCOUNT DEPARTMENT" (Port number 1) then you meet one of the employee working under "SAVINGS ACCOUNT DEPARTMENT" let us call him as SAVINGACCOUNT_EMPLOYEE1 for opening account .

现在,我们假设你去开一个储蓄账户,你去银行(IP地址),然后去“储蓄账户部门”(Port number 1),然后你会遇到一个在“储蓄账户部门”下工作的员工,让我们把他叫做SAVINGACCOUNT_EMPLOYEE1。

SAVINGACCOUNT_EMPLOYEE1 is your socket descriptor , so there may be SAVINGACCOUNT_EMPLOYEE1 to SAVINGACCOUNT_EMPLOYEEN these are all socket descriptors.

SAVINGACCOUNT_EMPLOYEE1是您的套接字描述符,因此可能会有SAVINGACCOUNT_EMPLOYEE1来SAVINGACCOUNT_EMPLOYEEN这些都是套接字描述符。

Likewise other departments will be having employess working under them and they are analogous to socket.

同样的,其他部门也会有员工在他们手下工作,而他们也类似于插座。

Hope this helps !!

希望这有助于! !

#25


1  

A socket is basically an endpoint for network communication, consisting of at least an IP-address and a port. In Java/C# a socket is a higher level implementation of one side of a two-way connection.

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

Also, a definition in the Java documentation.

还有,Java文档中的定义。

#26


1  

Port:

端口:

A port can refer to a physical connection point for peripheral devices such as serial, parallel, and USB ports. The term port also refers to certain Ethernet connection points, s uch as those on a hub, switch, or router.

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

Socket:

套接字:

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.

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

#27


1  

A single port can have one or more sockets connected with different external IP's like a multiple electrical outlet.

单个端口可以有一个或多个与不同外部IP相连的套接字,就像一个多电源插座。

  TCP    192.168.100.2:9001     155.94.246.179:39255   ESTABLISHED     1312
  TCP    192.168.100.2:9001     171.25.193.9:61832     ESTABLISHED     1312
  TCP    192.168.100.2:9001     178.62.199.226:37912   ESTABLISHED     1312
  TCP    192.168.100.2:9001     188.193.64.150:40900   ESTABLISHED     1312
  TCP    192.168.100.2:9001     198.23.194.149:43970   ESTABLISHED     1312
  TCP    192.168.100.2:9001     198.49.73.11:38842     ESTABLISHED     1312

#28


0  

A port denotes a communication endpoint in the TCP and UDP transports for the IP network protocol. A socket is a software abstraction for a communication endpoint commonly used in implementations of these protocols (socket API). An alternative implementation is the XTI/TLI API.

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

See also:

参见:

Stevens, W. R. 1998, UNIX Network Programming: Networking APIs: Sockets and XTI; Volume 1, Prentice Hall.
Stevens, W. R., 1994, TCP/IP Illustrated, Volume 1: The Protocols, Addison-Wesley.

1998年,《UNIX网络编程:网络api: socket和XTI》;卷1,普伦蒂斯霍尔。史蒂文斯,w·R。1994年,TCP/IP说明,第1卷:协议,Addison-Wesley。

#29


0  

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.

我知道有很多解释。但是,有一个更简单的方法可以用实例来理解。我们都可以连接到HTTP端口80,但是它是否意味着只有一个用户可以一次连接到这个端口?答案显然是“不”。多个用途的多个用户可以访问HTTP端口80,但是他们仍然可以从服务器得到正确的响应,不是吗?现在想一下,怎么样?。是的,您是正确的,它的IP地址唯一地标识不同的用户,他们联系的目的不同。如果您在到达这里之前已经阅读了前面的答案,您就会知道IP地址是套接字包含的信息的一部分。想想看,没有插座的通信是可能的吗?答案是肯定的,但是你不能在一个端口上运行多个应用程序,但是我们知道我们不是一个只在硬件上运行的“转储”开关。

#30


-1  

A port is an entity that is used by networking protocols to attain access to connected hosts. Ports could be application-specific or related to a certain communication medium. Different protocols use different ports to access the hosts, like HTTP uses port 80 or FTP uses port 23. You can assign user-defined port numbers in your application, but they should be above 1023.

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

Ports open up the connection to the required host while sockets are an endpoint in an inter-network or an inter-process communication. Sockets are assigned by APIs(Application Programming Interface) by the system.

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

A more subtle difference can be made saying that, when a system is rebooted ports will be present while the sockets will be destroyed.

更细微的差别可以这样说:当一个系统被重新引导时,端口将会出现,而套接字将被销毁。