File: timeoutaccept.c

package info (click to toggle)
publicfile 0.52-13
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 896 kB
  • sloc: ansic: 3,903; makefile: 468; sh: 296
file content (32 lines) | stat: -rw-r--r-- 625 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "timeoutaccept.h"
#include "ndelay.h"
#include "select.h"
#include "error.h"
#include "readwrite.h"

int timeoutaccept(int s,int timeout)
{
  struct sockaddr_in sa;
  int dummy;
  int fd;
  fd_set rfds;
  struct timeval tv;

  if (ndelay_on(s) == -1) return -1;

  FD_ZERO(&rfds);
  FD_SET(s,&rfds);
  tv.tv_sec = timeout; tv.tv_usec = 0;

  if (select(s + 1,&rfds,(fd_set *) 0,(fd_set *) 0,&tv) == -1) return -1;

  dummy = sizeof sa;
  fd = accept(s,(struct sockaddr *) &sa,&dummy);
  if (fd == -1) return -1;

  ndelay_off(s);
  return fd;
}