套接字API accept()函数如何工作? [英] How does the socket API accept() function work?

查看:247
本文介绍了套接字API accept()函数如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

套接字API是用于TCP/IP和UDP/IP通信的事实标准(即我们所知道的网络代码).但是,accept()是它的核心功能之一,有点神奇.

The socket API is the de-facto standard for TCP/IP and UDP/IP communications (that is, networking code as we know it). However, one of its core functions, accept() is a bit magical.

借用半正式定义:

accept()用于服务器端. 它接受收到的传入尝试 从创建一个新的TCP连接 远程客户端,并创建一个新的 与套接字关联的套接字 此连接的地址对.

accept() is used on the server side. It accepts a received incoming attempt to create a new TCP connection from the remote client, and creates a new socket associated with the socket address pair of this connection.

换句话说,accept返回一个新的套接字,服务器可以通过该套接字与新连接的客户端进行通信.旧的套接字(在其上调用accept的套接字)在同一端口上保持打开状态,以侦听新的连接.

In other words, accept returns a new socket through which the server can communicate with the newly connected client. The old socket (on which accept was called) stays open, on the same port, listening for new connections.

accept如何工作?如何实施?在这个话题上有很多困惑.许多人声称接受会打开一个新端口,而您通过该端口与客户端进行通信.但这显然是不正确的,因为没有打开任何新端口.实际上,您可以通过同一端口与不同的客户端进行通信,但是如何进行呢?当多个线程在同一端口上调用recv时,数据如何知道要去哪里?

How does accept work? How is it implemented? There's a lot of confusion on this topic. Many people claim accept opens a new port and you communicate with the client through it. But this obviously isn't true, as no new port is opened. You actually can communicate through the same port with different clients, but how? When several threads call recv on the same port, how does the data know where to go?

我想这是客户端地址与套接字描述符相关联的东西,每当数据通过recv到达时,都会被路由到正确的套接字,但是我不确定.

I guess it's something along the lines of the client's address being associated with a socket descriptor, and whenever data comes through recv it's routed to the correct socket, but I'm not sure.

对于此机制的内部工作有一个透彻的解释将是很棒的.

It'd be great to get a thorough explanation of the inner-workings of this mechanism.

推荐答案

您的困惑在于认为套接字是由Server IP标识的:Server Port.实际上,套接字是由四重信息唯一标识的:

Your confusion lies in thinking that a socket is identified by Server IP : Server Port. When in actuality, sockets are uniquely identified by a quartet of information:

Client IP : Client PortServer IP : Server Port

Client IP : Client Port and Server IP : Server Port

因此,尽管在所有接受的连接中服务器IP和服务器端口都是恒定的,但客户端信息才是客户端信息,它可以跟踪一切进行的方向.

So while the Server IP and Server Port are constant in all accepted connections, the client side information is what allows it to keep track of where everything is going.

用于说明问题的示例:

假设我们在192.168.1.1:80处有一台服务器,并有两个客户端10.0.0.110.0.0.2.

Say we have a server at 192.168.1.1:80 and two clients, 10.0.0.1 and 10.0.0.2.

10.0.0.1在本地端口1234上打开一个连接,并连接到服务器.现在,服务器具有一个标识如下的套接字:

10.0.0.1 opens a connection on local port 1234 and connects to the server. Now the server has one socket identified as follows:

10.0.0.1:1234 - 192.168.1.1:80  

现在10.0.0.2在本地端口5678上打开一个连接,并连接到服务器.现在,服务器具有两个插槽,标识如下:

Now 10.0.0.2 opens a connection on local port 5678 and connects to the server. Now the server has two sockets identified as follows:

10.0.0.1:1234 - 192.168.1.1:80  
10.0.0.2:5678 - 192.168.1.1:80

这篇关于套接字API accept()函数如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆