Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Multiplexing input/output with the soc_poll() function

&pagelevel(4)&pagelevel

soc_poll() also enables a program to monitor several connections simultaneously.

The following program section illustrates the use of soc_poll():

#include <sys.socket.h>
#include <sys.poll.h>
...
struct pollfd fds[3];
int timeout = 0;
unsigned long nfds = 3;
 
fds[0].events = POLLIN;
fds[1].events = POLLOUT
 
fds[2].events = POLLIN;
 ...
soc_poll(fds, nfds, timeout);

The socket descriptors and events to be tested are transmitted in an array of pollfd structure elements. fds is a pointer to this array. nfds specifies the number of structure elements.

In the example shown in this section these are descriptors 0...2 and the POLLIN and POLLOUT events. POLLIN indicates the “read” readiness and POLLOUT the “write” readiness of the socket.

The timeout parameter specifies how the soc_poll() function should behave if no event is to be tested:

  • If timeout = 0, soc_poll() tests all specified descriptors of the event to be tested only once. soc_poll() is then reset, regardless of whether the test was successful or not.

  • If timeout > 0 a waiting time is specified in seconds. During this waiting time soc_poll() is blocked as long as none of the events to be tested occur.

  • If timeout = -1 soc_poll() is blocked until one of the events to be tested occurs.

The return value of soc_poll() indicates the frequency of the occurrence, i.e., at least one bit is set in the revents return field of the corresponding pollfd structure element.

pollfd structure as declared in <sys.poll.h>:

struct pollfd {
   int      fd;               /* socket file descriptor to poll*/
   short    events;           /* events on interest on fd*/
   short    revents;          /* events that occurred on fd */
};


Example: Using soc_poll() to test for pending connection requests

The following program code is the same as the previous example except that the select() function has been replaced with the soc_poll() function.

#include <sys.types.h>
#include <stdlib.h>
#include <sys.socket.h>
#include <sys.poll.h>
#include <netinet.in.h>
#include <netdb.h>
#include <stdio.h>
#define TRUE 1
#define TESTPORT 5555
/*
 * This program uses soc_poll() to check whether someone is attempting to 
 * establish a connection, and then calls accept().
 */
  
main()
{
    int sock;
    struct sockaddr_in server;
    struct sockaddr_in client;
    int clientlen;
    int msgsock;
    struct pollfd fds[1];
    unsigned long nfds = 1;
    int timeout = 5;
    memset(&server,'\0',sizeof(server)); memset(&client,'\0',sizeof(client));
 
   clientlen = sizeof(client);
 
/* Initialize the fds structure arrays to request the "read" readiness of the 
listen socket */
fds[0].fd = 0;
fds[0].events = POLLIN;
fds[0].revents = 0;
 
/* Create socket. */
      sock = socket(AF_INET, SOCK_STREAM, 0);
      if (sock < 0) {
              perror("opening stream socket");
              exit(1);
      }
   /* Assign the socket a name using wildcards */
   server.sin_family = AF_INET;
   server.sin_addr.s_addr = htonl(INADDR_ANY);
   server.sin_port = htons(TESTPORT);
   if (bind(sock, (struct sockaddr *)&server, sizeof server) < 0) {
           perror("binding stream socket");
           exit(1);
   }
      /* Start acceptance of connections. */
      listen(sock, 5);
      do {
              fds[0].fd = sock;
              if (soc_poll(fds, nfds, timeout)) <= 0){
                      perror("soc_poll");
                      continue;
     }
     else
     {
     if (fds[0].revents & POLLIN) {
         fds[0].revents = 0;
         msgsock = accept(sock, (struct sockaddr *)&client, &clientlen);
         if (msgsock >= 0)
           {
            /* Successful acceptance of request to establish connection*/
            /* Follow-up processing of the data which is transferred */
            /* via this connection */
            printf("End of program after successful conection setup\n");
            break;
           }
         else
           {
            /* An error has occurred */
            /* Error message and possibly renewed waiting for a request */
            /* to establish a connection */
            printf("End of program: An error occurred during connection 
            setup\n");
            break;
           }
       }
     }
    } while (TRUE);
exit(0);
}