HP Rp7410 BSD Sockets Interface Programmer's Guide - Page 76
Sending and Receiving Data Asynchronously
View all HP Rp7410 manuals
Add to My Manuals
Save this manual to your list of manuals |
Page 76 highlights
Advanced Topics for Stream Sockets Sending and Receiving Data Asynchronously Notification that out-of-band data has been received is also done asynchronously; see the section "Sending and Receiving Out-of-band Data" in this chapter for more details. The following example sets up an asynchronous SOCK_STREAM listen socket. This is typical of an application that needs to be notified when connection requests arrive. int ls; int flag = 1; int iohndlr(); /* SOCK_STREAM listen socket initialized */ /* for ioctl, to turn on async */ /* the function which handles the SIGIO */ signal (SIGIO, iohndlr); /* set up the handler */ if (ioctl (ls, FIOASYNC, &flag) == -1) { perror ("can't set async on socket"); exit(1); } flag = -getpid();/* process group negative = deliver to process */ if (ioctl (ls, SIOCSPGRP, &flag) == -1) { perror ("can't set pgrp"); exit(1); } /* signal can come any time now */ The following example illustrates the use of process group notification. Note that the real utility of this feature is to allow multiple processes to receive the signal, which is not illustrated here. For example, the socket could be of type SOCK_DGRAM; a signal here can be interpreted as the arrival of a service-request packet. Multiple identical servers could be set up, and the first available one could receive and process the packet. int flag = 1; int iohndlr(); signal (SIGIO, iohndlr); /* ioctl to turn on async */ setpgrp(); /* set my processes' process group */ if (ioctl (s, FIOASYNC, &flag) == -1) { perror ("can't set async on socket"); exit(1); } flag = getpid(); /* process group + = deliver to each process in group */ if (ioctl (s, SIOCSPGRP, &flag) == -1) { perror ("can't set pgrp"); exit(1); } /* signal can come any time now */ 76 Chapter 3