for (;;)
{
if (msgrcv(msqid, &flag, sizeof(struct flags) - sizeof(long), 1, IPC_NOWAIT)>0)
break;
}
msgsnd(msqid, &message , sizeof(struct messages) - sizeof(long), 0);
Is there any accept()
function like in sockets
IPC, but for message queues
IPC? My server should wait client connection and only when client connected send data back to it. I special send from client some useless data and check data in infinity loop(that means client is connected, I know that is very stupid algorithm).
No, there is nothing directly analogous. Message queues are more like connectionless datagram sockets than they are like connection-oriented stream sockets (which support accept()
and so forth).
Some implementations (z/OS?) expose the number of processes blocked on a msgrcv
, which is similar to what you are looking for, but this is not portable.
As I see it, you have two easy options.
First, do what you are doing but don't IPC_NOWAIT in the server's msgrcv
. No point in spinning in a loop if you're not doing anything. Just block until a client announces itself. (And use different message types for communication from client-to-server and server-to-client — you don't want the clients consuming their messages to the server, nor vice-versa.)
Second, switch to AF_UNIX stream sockets, which will give you accept()
able semantics.
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.