简体   繁体   English

发送到 127.0.0.1 的数据包是否完全使用工作连接套接字?

[英]Do packets sent to 127.0.0.1 fully use the working connection socket?

Given a client-server machines system (a MP videogame, for example), server wants for some reasons send a packet to itself (protocol used doesn't matter, I suppose).给定一个客户端 - 服务器机器系统(例如 MP 视频游戏),服务器出于某些原因想要向自己发送数据包(我认为使用的协议无关紧要)。 One can send such packet to server via IP, but this involves actually sending the packet in the internet and then wait for it to come back (also, it's kind of weird to do this).可以通过 IP 将这样的数据包发送到服务器,但这涉及在互联网上实际发送数据包,然后等待它返回(而且,这样做有点奇怪)。 That's why we can send our packet to the address 127.0.0.1.这就是为什么我们可以将数据包发送到地址 127.0.0.1。

My question is: when sending a packet to 127.0.0.1, does the packet actually get to the socket used for the estabilished connection in order to be delivered to the same machine, or it is "captured" just before and sent back (because it's a self-sending packet) leaving then the socket free to send/receive packets from the outside?我的问题是:当向 127.0.0.1 发送数据包时,数据包是否真的到达了用于建立连接的套接字以便传送到同一台机器,或者它在之前被“捕获”并发回(因为它是一个自发送的数据包)让套接字自由发送/接收来自外部的数据包?

Operating systems have some form of "loopback device" that's used when a host communicates with itself.操作系统具有某种形式的“环回设备”,当主机与其自身通信时会使用该设备。 On Linux, you can see this explicitly as the lo device:在 Linux 上,您可以明确地将其视为lo设备:

# ip -4 a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 172.16.5.202/24 brd 172.16.5.255 scope global eth0

When you send a packet to 127.0.0.1, it really does go through this device.当您向 127.0.0.1 发送数据包时,它确实会通过此设备。 It will be processed normally by your sockets API calls, consuming space in the socket buffer until processed, just as if it had come from "the Internet".它将由您的套接字 API 调用正常处理,在处理之前消耗套接字缓冲区中的空间,就像它来自“互联网”一样。

First, packets addressed to a locally-assigned IP, even when it is not loopback, are managed internally by the networking stack and not sent to the network.首先,寻址到本地分配的 IP 的数据包,即使它不是环回,也由网络堆栈内部管理,而不是发送到网络。

It seems there is a confusion in the question itself.问题本身似乎存在混淆。

The client opens a connection with the server, and when the connection is established, in the server side a new socket is created and returned to the server application.客户端打开与服务器的连接,当连接建立时,在服务器端创建一个新的套接字并返回给服务器应用程序。 If the connection is opened with a server in 127.0.0.1 (loopback address), the server needs to be listening on address 127.0.0.1 or INADDR_ANY, it will receive the datagrams and the connection can be established.如果与 127.0.0.1(环回地址)中的服务器建立连接,则该服务器需要侦听地址 127.0.0.1 或 INADDR_ANY,它将接收数据报并建立连接。 If the server is listening only to 127.0.0.1 it won't receive messages from outside.如果服务器仅侦听 127.0.0.1,它将不会接收来自外部的消息。

Sockets is just the interface provided by the operating system to deal with different protocols.套接字只是操作系统提供的用于处理不同协议的接口。 When the client sends the message to the server, it sends it through the socket, it is sent to the kernel networking stack which, at the IP layer looks at the destination address and decides what to do with that datagram.当客户端向服务器发送消息时,它通过套接字将其发送到内核网络堆栈,内核网络堆栈在 IP 层查看目标地址并决定如何处理该数据报。 If it goes to loopback or a locally-assigned address it will pass the datagram internally to the appropriate data structure to be received on the other end of the connection: the socket returned by the accept system call in the server, if it is a established connection.如果它进入环回或本地分配的地址,它将在内部将数据报传递给连接另一端要接收的适当数据结构:服务器中接受系统调用返回的套接字,如果它是已建立的联系。

These datagrams never go to the NIC and are never sent out to the network.这些数据报永远不会到达 NIC,也永远不会发送到网络。

Well this really depends upon an implementation - but on Linux - even a packet that originates on a node say 192.168.0.1 and is destined for the same host 192.168.0.1 would not see so much as Ethernet card of the machine.嗯,这真的取决于实现 - 但在 Linux 上 - 即使是一个来自节点的数据包,比如192.168.0.1并且目的地是同一主机192.168.0.1也不会像机器的以太网卡那样看到那么多。 So sending it to the IP address itself is not going to hurt.因此,将其发送到 IP 地址本身不会受到伤害。 But if you choose to send it to 127.0.0.1 that's okay too.但是,如果您选择将其发送到127.0.0.1 ,那也没关系。

Coming back to your question does the packet gets sent to the socket.回到你的问题,数据包是否被发送到套接字。 It depends - if you are listening on INADDR_ANY , then it can get sent to the server.这取决于 - 如果您正在侦听INADDR_ANY ,那么它可以发送到服务器。 If you are listening only on a specific address (that of your Ethernet Adapter say) you will not receive it if you send to .如果您只侦听特定地址(例如您的以太网适配器的地址),如果您发送到 .

For a packet to be delivered to a socket - there has to be a socket that is 'connected' or 'listening' on a given address.要将数据包传送到套接字 - 必须有一个在给定地址上“连接”或“侦听”的套接字。 That's broadly how it is.大致就是这样。

Hope that helps.希望有帮助。

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

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