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

查看:378
本文介绍了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.底层发生了什么,可跟踪来自client1和client2的数据?我想网络堆栈在域套接字中不起作用,所以内核在这里完成所有工作吗?

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.

推荐答案

如果创建类型为SOCK_STREAMPF_UNIX套接字并接受其上的连接,则每次接受连接时,都会得到一个新文件描述符(作为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获取发送套接字的路径.

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.

如果不绑定路径,则接收过程将无法获得唯一标识对等方的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天全站免登陆