简体   繁体   中英

Can I use non-blocking sockets to send FDs from one process to another using “sendmsg/recvmsg”?

I need to create a client-server model which are connected through Unix socket. My client will request the server to open a file for which only server has the permissions. Server will open the file and send the FD using "sendmsg" API and client will receive the FD using the "recvmsg". Now my question is can I achieve this using the non-blocking socket? Also if it is possible, how do I know the length of message on client side?

Please point me to some non-blocking implementation of the same.

You should use sendfile to transmit the file between the FD and the socket.

If the socket is non-blocking, then the sendfile (or send or sendmsg) call will likely return a value indicating a partial amount completed. Use select() or poll() to get notified of when it's possible to send again (picking up from the previous point in the file descriptor when the previous send left off).

As for the file length, you'll have to add that to your request/response protocol yourself. (eg call stat() on the server, send the file length, then send the file itself).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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