R: Non-blocking Accept in C

From: Alfredo Scotto (scotto@aedi.it)
Date: Thu Jan 13 2000 - 04:46:18 EST


Hi,
This code is ok for your requirements.
Alfredo

int AcceptCall(int serverfd,int timeout) /* return accepted socket or -1 */
/* if timeout is 0, wait without timeout */
{
    int i,curfd,namelen;
    struct sockaddr_in peername;
    struct timeval tmo;
    fd_set fds,efds;

    tmo.tv_sec = timeout; /* Timeout in seconds */
    tmo.tv_usec = 0;
    FD_ZERO(&fds); FD_ZERO(&efds);
    FD_SET(serverfd,&fds); FD_SET(serverfd,&efds);
    if(select (serverfd+1,&fds,NULL,&efds,
                        timeout ? &tmo:(struct timeval *)0)){
      if((curfd=accept(serverfd,(struct sockaddr*)&peername,&namelen))>=0){
          close(serverfd);
          return(curfd);
      }
    }
    return(-1);
}

.......................
+---------------------+...............................
|Ing. Alfredo Scotto +-----------------------------+.
|AEDI - Archintel Electronic Data Interchange srl |.
|Tel.: +39-055-401741/2047371 Fax: +39-055-401741 |.
|E-mail: scotto@aedi.it URL: http://www.aedi.it/ |.
|Get Public Key from pgp-public-keys@keys.pgp.net |.
+---------------------------------------------------+

-----Messaggio Originale-----
Da: Kendall Lister <kendall@charon.net.au>
A: Linux Networking Mailing List <linux-net@vger.rutgers.edu>
Data invio: mercoledý 12 gennaio 2000 23.15
Oggetto: Re: Non-blocking Accept in C

> On Wed, 12 Jan 2000, Larry Griffin wrote:
>
> > This particular server side only is looking for one client so I go
> > into a loop that checks, sleeps for a half second or so and checks
> > again. I'm trying to get the server to account for link outages or
> > other failures. It had code in there to set reuse but I commented it
> > out while working on this other problem.
>
> When you say you are delaying between checks, are you doing this:
>
> while (TRUE) {
> select(n, &readfds, NULL, NULL, 0.5);
> }
>
> or
>
> while (TRUE) {
> select(n, &readfds, NULL, NULL, 0);
> sleep 0.5;
> }
>
> If you use the second alogorithm, you'll probably never register a
> connection.
>
> > Right now I've set up a simple client and server just to trouble shoot
> > the problem. Maybe I need to upgrade my release. This is RedHat
> > 2.0.36.
>
> Well, I'm using 2.0.38, and I've just written a tiny HTTP interface for a
> daemon, based on about an hour of reading up on sockets - so my advice may
> not be the best you can get... :)
>
> --
> Kendall Lister, Systems Operator for Charon I.S. - kendall@charon.net.au
> Charon Information Services - Friendly, Cheap Melbourne ISP: 9589 7781
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-net" in
> the body of a message to majordomo@vger.rutgers.edu

-
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to majordomo@vger.rutgers.edu



This archive was generated by hypermail 2b29 : Sat Jan 15 2000 - 21:00:29 EST