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

select_cl.c « socket_tests « cygwin « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 44aebd45c81bcb01ae94f3a4e0ffbad1086c118e (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
#include "select_test.h"

int
main ()
{
  int sfd, flags;
  fd_set writefds;
  size_t nwritten = 0;
  ssize_t nw;
  char buf[BUF_SIZE];

  if ((sfd = unixConnect (SV_SOCK_PATH, SOCK_STREAM)) < 0)
    errExit ("unixConnect");
  flags = fcntl (sfd, F_GETFL);
  if (fcntl (sfd, F_SETFL, flags | O_NONBLOCK) < 0)
    errExit ("fcntl");

  printf ("waiting for socket to be ready for write...\n");
  FD_ZERO (&writefds);
  FD_SET (sfd, &writefds);
  if (select (sfd + 1, NULL, &writefds, NULL, NULL) < 0)
    errExit ("select");
  if (FD_ISSET (sfd, &writefds))
    printf ("ready for write, writing until buffer full\n");
  else
    errExit ("something's wrong");
  while (1)
    {
      nw = write (sfd, buf, BUF_SIZE);
      if (nw < 0)
	{
	  if (errno == EAGAIN)
	    {
	      printf ("buffer full\n");
	      break;
	    }
	  else
	    errExit ("write");
	}
      nwritten += nw;
    }
  printf ("wrote %zu bytes\n", nwritten);
  printf ("waiting for write ready again...\n");
  FD_ZERO (&writefds);
  FD_SET (sfd, &writefds);
  if (select (sfd + 1, NULL, &writefds, NULL, NULL) < 0)
    errExit ("select");
  if (FD_ISSET (sfd, &writefds))
    printf ("ready for write, writing once more\n");
  if ((nw = write (sfd, buf, BUF_SIZE)) < 0)
    errExit ("write");
  nwritten += nw;
  printf ("wrote %zd more bytes for a total of %zu\n", nw, nwritten);
}