简体   繁体   English

客户端如何知道accept()后发送数据的端口

[英]How does a client know which port to send data after accept()

Let's say we have a server that can accept multiple clients.假设我们有一个可以接受多个客户端的服务器。 First, it has to create a socket, then bind it with a port and an IP and finally listen to requests for connection from clients.首先,它必须创建一个套接字,然后将它与一个端口和一个 IP 绑定,最后监听来自客户端的连接请求。 After accept() ing a connection with a client, the server creates a new socket to communicate with the specific client.accept()与客户端建立连接后,服务器会创建一个新套接字来与特定客户端进行通信。 My question is whether or not the client is going to send its data to the same port it sent its initial request to, and if not how does it know where to send it?我的问题是客户端是否会将其数据发送到它发送初始请求的同一端口,如果不是,它如何知道将其发送到哪里?

Generally There is always a default port allotted for each kind of communication.Operating System may kept it open or close,it can be checked.一般来说,每种通信都有一个默认端口分配。操作系统可以保持打开或关闭,可以检查。

Let's say for FTP connection, There is a separate port allotted for handshake,It don't matter how many new FTP connection are being requested, all new connection will go to that same port, Once handshake is completed data exchange is done via another port, Even if we don't specify port. Let's say for FTP connection, There is a separate port allotted for handshake,It don't matter how many new FTP connection are being requested, all new connection will go to that same port, Once handshake is completed data exchange is done via another port , 即使我们不指定端口。 If Network manager has Port List entries earlier it will request to the same port.如果网络管理器之前有端口列表条目,它将请求相同的端口。

Example for SSH if you request for SSH 的示例,如果您要求

ssh -X <IP> 

Even if you don't mention port, Your system know which port to request for and at server side there is always some port open who will be listening to your request and based on data you send while handshake it will continue listening or block you.即使您没有提及端口,您的系统也知道要请求哪个端口,并且在服务器端总是有一些开放的端口将监听您的请求,并且根据您在握手时发送的数据,它将继续监听或阻止您。

Bonus is you can open your custom port at server side who will be listening to your request.奖励是您可以在服务器端打开您的自定义端口,该端口将监听您的请求。 TCP implementation by default declare which port will be used for what kind of communication. TCP 实现默认声明哪个端口将用于哪种通信。

The client connects with a source IP and port to a server with a destination IP and port.客户端通过源 IP 和端口连接到具有目标 IP 和端口的服务器。 After accept exactly the same IP and port on both sides are continued to be used for data exchange as for the establishment of the connection.在接受完全相同的 IP 后,双方的端口继续用于数据交换,以建立连接。

A socket connection is uniquely identified by a tuple of [Protocol, Local IP, Local Port, Peer IP, Peer Port] .套接字连接由[Protocol, Local IP, Local Port, Peer IP, Peer Port]的元组唯一标识。

A TCP server creates a listening socket with a tuple of [TCP, Listen IP, Listen Port, 0, 0] . TCP 服务器创建一个监听套接字,其元组为[TCP, Listen IP, Listen Port, 0, 0] When a client requests to connect to a server, the network routes the request to the specified IP/Port.当客户端请求连接到服务器时,网络会将请求路由到指定的 IP/端口。 The receiving device then routes the request to a matching listening socket, performs a 3way handshake with the client, and puts it into a queue.然后接收设备将请求路由到匹配的侦听套接字,与客户端执行 3 次握手,并将其放入队列中。 Later, when accept() is called, it extracts the next pending client from the queue and returns a new socket identified with a tuple of [TCP, Listen IP, Listen Port, Client IP, Client Port] .稍后,当调用accept()时,它会从队列中提取下一个挂起的客户端,并返回一个新的套接字,该套接字由[TCP, Listen IP, Listen Port, Client IP, Client Port]的元组标识。

A TCP client creates a connecting socket with a tuple of [TCP, Local IP, Local Port, 0, 0] . TCP 客户端使用[TCP, Local IP, Local Port, 0, 0]的元组创建连接套接字。 When the 3way handshake is complete, the socket's tuple is updated to [TCP, Local IP, Local Port, Server IP, Server Port] . 3way 握手完成后,套接字的元组更新为[TCP, Local IP, Local Port, Server IP, Server Port]

All subsequent data exchanges use these tuples.所有后续数据交换都使用这些元组。

Data sent out from the Client's socket will be sent to the associated Server IP/Port and stored in the buffer of the connected Server socket whose tuple matches both the Client and Server.从客户端套接字发出的数据将被发送到关联的服务器 IP/端口,并存储在连接的服务器套接字的缓冲区中,其元组与客户端和服务器都匹配。

Data sent out from the Server's listening socket will be ignored, since there is no associated Client.从服务器的侦听套接字发出的数据将被忽略,因为没有关联的客户端。

Data sent out from a connected Server socket will be sent to the associated Client IP/Port and stored in the buffer of the Client socket whose tuple matches the Client and Server.从连接的服务器套接字发出的数据将被发送到关联的客户端 IP/端口,并存储在其元组与客户端和服务器匹配的客户端套接字的缓冲区中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM