Unix 域套接字如何区分多个客户端? [英] How do Unix Domain Sockets differentiate between multiple clients?

查看:41
本文介绍了Unix 域套接字如何区分多个客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TCP 有元组对(IP 地址/端口/类型)来告诉一个客户端和另一个客户端.UDP 传递客户端 IP 和端口.unix 域如何跟踪不同的客户端?

TCP has the tuple pairs (IP Addr/port/type) to tell one client from another. UDP passes the client IP and port. How does the unix domain keep track of different clients?

换句话说,服务器创建一个绑定到某个路径的套接字,比如/tmp/socket.2 个或更多客户端连接到/tmp/socket.下面发生了什么来跟踪来自客户端 1 和客户端 2 的数据?我想网络堆栈在域套接字中没有任何作用,所以内核在这里完成所有工作吗?

In other words the server creates a socket bound to some path say /tmp/socket. 2 or more clients connect to /tmp/socket. What is going on underneath that keeps track of data from client1 and client2? I imagine the network stack plays no part in domain sockets so is the kernel doing all the work here?

是否有像 IP 协议格式和 TCP/UDP 格式一样的 unix 域协议格式?域套接字数据报协议的格式是否在某处发布?每个unix都不同还是像POSIX之类的东西对其进行了标准化?

Is there a unix domain protocol format like there is an IP protocol format and TCP/UDP formats? Is the format of domain socket datagram protocols published somewhere? Is every unix different or does something like POSIX standardize it?

感谢您的照明.我找不到任何解释这一点的信息.每个消息来源只是掩盖了如何使用域套接字.

Thanks for any illumination. I could not find any information that explained this. Every source just glossed over how to use the domain sockets.

推荐答案

如果您创建一个 PF_UNIX 类型为 SOCK_STREAM 的套接字,并在其上接受连接,那么每次你接受一个连接,你会得到一个新的文件描述符(作为 accept 系统调用的返回值).此文件描述符从客户端进程中的文件描述符读取数据并将数据写入其中.因此它就像一个 TCP/IP 连接一样工作.

If you create a PF_UNIX socket of type SOCK_STREAM, and accept connections on it, then each time you accept a connection, you get a new file descriptor (as the return value of the accept system call). This file descriptor reads data from and writes data to a file descriptor in the client process. Thus it works just like a TCP/IP connection.

没有unix 域协议格式".没有必要,因为 Unix 域套接字无法通过网络连接连接到对等方.在内核中,代表 SOCK_STREAM Unix 域套接字末端的文件描述符指向一个数据结构,该数据结构告诉内核哪个文件描述符位于连接的另一端.当您将数据写入文件描述符时,内核会在连接的另一端查找文件描述符,并将数据附加到其他文件描述符的读取缓冲区.内核不需要将您的数据放入带有描述其目的地的标头的数据包中.

There's no "unix domain protocol format". There doesn't need to be, because a Unix-domain socket can't be connected to a peer over a network connection. In the kernel, the file descriptor representing your end of a SOCK_STREAM Unix-domain socket points to a data structure that tells the kernel which file descriptor is at the other end of the connection. When you write data to your file descriptor, the kernel looks up the file descriptor at the other end of the connection and appends the data to that other file descriptor's read buffer. The kernel doesn't need to put your data inside a packet with a header describing its destination.

对于 SOCK_DGRAM 套接字,您必须告诉内核应该接收数据的套接字的路径,并使用它来查找该接收套接字的文件描述符.

For a SOCK_DGRAM socket, you have to tell the kernel the path of the socket that should receive your data, and it uses that to look up the file descriptor for that receiving socket.

如果您在连接到服务器套接字之前(或者如果您使用的是SOCK_DGRAM,则在发送数据之前)绑定到客户端套接字的路径,那么服务器进程可以使用 getpeername(用于 SOCK_STREAM).对于SOCK_DGRAM,接收端可以使用recvfrom来获取发送socket的路径.

If you bind a path to your client socket before you connect to the server socket (or before you send data if you're using SOCK_DGRAM), then the server process can get that path using getpeername (for SOCK_STREAM). For a SOCK_DGRAM, the receiving side can use recvfrom to get the path of the sending socket.

如果不绑定路径,那么接收进程就无法得到唯一标识peer的id.至少,不是在我运行的 Linux 内核上 (2.6.18-238.19.1.el5).

If you don't bind a path, then the receiving process can't get an id that uniquely identifies the peer. At least, not on the Linux kernel I'm running (2.6.18-238.19.1.el5).

这篇关于Unix 域套接字如何区分多个客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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