Welcome to mirror list, hosted at ThFree Co, Russian Federation.

select_sv.c « socket_tests « cygwin « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e9cf6dc8ccc6f11338a64c33ee16ab2ef8104ec2 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "select_test.h"

int
main ()
{
  int sfd, cfd, flags;
  fd_set readfds;
  size_t nread = 0;
  char buf[BUF_SIZE];

  if (remove (SV_SOCK_PATH) < 0 && errno != ENOENT)
    errExit ("remove");

  if ((sfd = unixBind (SV_SOCK_PATH, SOCK_STREAM)) < 0)
    errExit ("unixBind");

  if (listen (sfd, BACKLOG) < 0)
    errExit ("listen");

  printf ("waiting for connection request...\n");
  FD_ZERO (&readfds);
  FD_SET (sfd, &readfds);
  if (select (sfd + 1, &readfds, NULL, NULL, NULL) < 0)
    errExit ("select");
  if (FD_ISSET (sfd, &readfds))
    printf ("connection request received; accepting\n");
  else
    errExit ("something's wrong");
  cfd = accept (sfd, NULL, NULL);
  if (cfd < 0)
    errExit ("accept");

  flags = fcntl (cfd, F_GETFL);
  if (fcntl (cfd, F_SETFL, flags | O_NONBLOCK) < 0)
    errExit ("fcntl");

  printf ("slowly reading from socket...\n");
  while (1)
    {
      FD_ZERO (&readfds);
      FD_SET (cfd, &readfds);
      if (select (cfd + 1, &readfds, NULL, NULL, NULL) < 0)
	errExit ("select");
      if (!FD_ISSET (cfd, &readfds))
	errExit ("something's wrong");
      ssize_t nr = read (cfd, buf, 10);
      if (nr < 0)
	{
	  if (errno == EPIPE)
	    break;
	  else
	    errExit ("read");
	}
      else if (nr == 0)
	break;
      nread += nr;
    }
  printf ("read %zu bytes\n", nread);
}