From: karh@pa621a.inland.com
Newsgroups: biz.sco.opendesktop
Subject: Sockets and select (S)
Date: 9 Nov 92 23:08:47 GMT
Organization: Inland Steel Company, East Chicago, IN
Nntp-Posting-User: creekmore

Help me,

  I seem to have a problem with Sockets and the select system service.
  Basically, it doesn't work.

  Select always times out!!!!

  I'm using ODT 1.1 on a DEC433mp (one processor)

  Refer to code segment below.

------- cut here ------- cut here -------- cut here --------- cut here --------
/*
      Proof of my misery
*/

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>

#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/times.h>
#include <netinet/in.h>
#include <netdb.h>

int main (argc, argv)
      int    argc;
      char** argv;
{
    static char* program = "EXAMPLE";
    static char* module  = "main";

    int s;                        /* Socket Handle */
    int i, n;                     /* Local Ints */
    struct sockaddr_in sin;       /* Internet host address */
 
    struct timeval timeout;       /* Timeout Structure */
    fd_set readfds;               /* File Discriptor set for READ */
    fd_set execptfds;             /* File Discriptor set for EXCEPTIONS */

    int status;                   /* Function return status */

    char buffer [2 * BUFSIZ];     /* Such a large buffer */

    /*********************************************************************/
    /*  Create an IP-family socket on which to make the connection       */
    /*********************************************************************/

    s = socket (AF_INET, SOCK_STREAM, 0);
    if (s < 0)
    {
      fprintf (stderr, "%s\\%s: socket %d line %d", program, module, s,
               __LINE__);
      perror ("");
      exit (EXIT_FAILURE);
    }
    
    /*********************************************************************/
    /*  Create a "sockaddr_in" structure which describes the remote      */
    /*  IP address we want to connect to   (USE YOUR OWN SERVER PLEASE)  */
    /*********************************************************************/
    
    sin.sin_family = AF_INET;
    (void) memcpy (&sin.sin_addr, inet_aton ("156.144.7.211"), 
                   sizeof (sin.sin_addr));
    sin.sin_port   = htons ((short) 5928);

    /*********************************************************************/
    /*  Connect to that address...                                       */
    /*********************************************************************/

    if (connect (s, &sin, sizeof (sin)) < 0) 
    {
        fprintf (stderr, "%s\\%s: connect %d", program, module, __LINE__);
        perror ("");
        exit (EXIT_FAILURE);
    }
                    
    /*********************************************************************/
    /*  Get the data                                                     */
    /*********************************************************************/

    count = 0;                    /* Initialize byte counters */
    n = 0;

    for (;;)
    {
        timeout.tv_sec  = 15;     /* Set Timeout seconds */
        timeout.tv_usec = 0;      /* Set Timeout microseconds */
                                  
        FD_ZERO (&readfds);       /* Zero all descriptor bits */
        FD_ZERO (&execptfds);

        FD_SET (s, &readfds);     /* Set Array of descriptor bits */
        FD_SET (s, &execptfds);

        status = select (1, &readfds, NULL, &execptfds, &timeout);
        if (status < 0)
        {
            fprintf (stderr, "%s\\%s: Select failed\n", program, module);
            perror ("");
            break;
        }
        if (status == 0)
        {
            (void) fprintf (stderr, "%s\\%s: Select timeout\n", program,
                            module);
            break;
        }

        n = read (s, buffer, sizeof (buffer));
        if (n <= 0)
            break;

        count += n;

        if ((i = fwrite (buffer, sizeof (char), n, stdout)) != n)
            fprintf (stderr, "%s\\%s: Read %d wrote %d \n", program, module,
                     n, i);
    }

    if (n < 0)
    {
        fprintf (stderr, "%s\\%s: read failed \n", program, module);
        perror ("");
    }

    /*********************************************************************/
    /*  Close socket                                                     */
    /*********************************************************************/

    (void) shutdown (s, 2);
    (void) close (s);

    exit (EXIT_SUCCESS);

}
------- cut here ------- cut here -------- cut here --------- cut here --------

What's the problem?

-- Bill
-----------------------------------------------------------------------------
Opinions expressed    | William D. Karh           |       InterNet:
are not those of my   | Dept. Process Automation  |  karh@pa881a.inland.com
employer...           | Inland Steel FPC          |
Newsgroups: biz.sco.opendesktop