public inbox for cygwin-developers@cygwin.com
 help / color / mirror / Atom feed
From: Corinna Vinschen <corinna-cygwin@cygwin.com>
To: cygwin-developers@cygwin.com
Subject: Re: Questions about select for sockets
Date: Tue, 6 Apr 2021 20:24:06 +0200	[thread overview]
Message-ID: <YGynRrexRDaqT0MU@calimero.vinschen.de> (raw)
In-Reply-To: <6306d8f3-cf05-07bb-e944-05aed69266f3@cornell.edu>

On Apr  6 13:37, Ken Brown wrote:
> On 4/6/2021 12:28 PM, Corinna Vinschen wrote:
> > On Apr  6 11:44, Ken Brown wrote:
> > > On 4/6/2021 10:33 AM, Corinna Vinschen wrote:
> > > > We may also have to change the saw_shutdown_read/saw_shutdown_write
> > > > handling.  I checked this on Linux and what happens is:
> > > > 
> > > >     After shutdown (fd, SHUT_RD), the socket is ready for reading and writing
> > > 
> > > This seems surprising to me.  Is it really the shutdown that caused it to be
> > > ready for writing in your test, or was it ready for writing anyway (e.g.,
> > > because the relevant buffer was empty)?
> > 
> > I guess so, too.  How to make sure the socket isn't ready for writing
> > without going to great lengths?
> 
> I guess you could have a subprocess write to the socket in a loop, so that
> its buffer will quickly fill up and a further write will block.

Yeah, I was trying to minimize work, but I now lazily created a blocking
server in the same process with a non-blocking client, calling send(2)
until it fails.

And now everything is as expected.  SHUT_RD -> ready for reading,
SHUT_WR -> ready for writing, SHUT_RDWR -> ready for both.

I attached my STC, for completeness.  Call with an argument
0 (== SHUT_RD), 1 (== SHUT_WR), or 2 (== SHUT_RDWR).


Corinna


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>

int
sel (int fd, const char *when)
{
  fd_set r, w, e;
  int ret;
  struct timeval tv;

  printf ("%s\n", when);
  FD_ZERO (&r);
  FD_ZERO (&w);
  FD_ZERO (&e);
  FD_SET (fd, &r);
  FD_SET (fd, &w);
  FD_SET (fd, &e);
  tv.tv_sec = 1;
  tv.tv_usec = 0;
  errno = 0;
  ret = select (fd + 1, &r, &w, &e, &tv);
  printf ("select: %d", ret);
  if (select < 0)
    printf ("%d <%s>\n", errno, strerror (errno));
  putchar ('\n');
  if (FD_ISSET (fd, &r))
    printf ("ready for reading\n");
  if (FD_ISSET (fd, &w))
    printf ("ready for writing\n");
  if (FD_ISSET (fd, &r) || FD_ISSET (fd, &w))
    {
      int err, len;

      len = sizeof err;
      getsockopt (fd, SOL_SOCKET, SO_ERROR, &err, &len);
      printf ("SO_ERROR: %d <%s>\n", err, strerror (err));
    }
  if (FD_ISSET (fd, &e))
    printf ("ready for exception\n");
  putchar ('\n');
}

int sfd;

int
server_1 ()
{
  struct sockaddr_in in;

  sfd = socket (AF_INET, SOCK_STREAM, 0);
  if (sfd < 0)
    {
      printf ("SRV socket: %d <%s>\n", errno, strerror (errno));
      return -1;
    }
  in.sin_family = AF_INET;
  in.sin_port = htons (65444);
  in.sin_addr.s_addr = inet_addr ("127.0.0.1");
  if (bind (sfd, (const struct sockaddr *) &in, sizeof in) < 0)
    {
      printf ("SRV bind: %d <%s>\n", errno, strerror (errno));
      return -1;
    }
  if (listen (sfd, 5) < 0)
    {
      printf ("SRV listen: %d <%s>\n", errno, strerror (errno));
      return -1;
    }
  return 0;
}

int
server_2 ()
{
  if (accept (sfd, NULL, NULL) < 0)
    {
      printf ("SRV accept: %d <%s>\n", errno, strerror (errno));
      return -1;
    }
  return 0;
}

char buf[65536];

int
main (int argc, char **argv)
{
  int fd, arg;
  struct sockaddr_in in;

  fd = socket (AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
  if (fd < 0)
    {
      printf ("socket: %d <%s>\n", errno, strerror (errno));
      return 1;
    }

  if (server_1 ())
    return 2;

  in.sin_family = AF_INET;
  in.sin_port = htons (65444);
  in.sin_addr.s_addr = inet_addr ("127.0.0.1");
  sel (fd, "Pre connect");
  if (connect (fd, (const struct sockaddr *) &in, sizeof in) != 0
      && errno != EINPROGRESS)
    {
      printf ("connect: %d <%s>\n", errno, strerror (errno));
      return 3;
    }

  if (server_2 () < 0)
    return 4;

  sel (fd, "Post shutdown");

  /* write until buffer full */
  while (send (fd, buf, sizeof buf, 0) > 0)
    ;

  sel (fd, "Pre shutdown");
  arg = argc > 1 ? atoi (argv[1]) : SHUT_RDWR;
  if (shutdown (fd, argc > 1 ? atoi (argv[1]) : SHUT_RDWR) < 0)
    {
      printf ("shutdown: %d <%s>\n", errno, strerror (errno));
      return 4;
    }
  sel (fd, "Post shutdown");
}

  reply	other threads:[~2021-04-06 18:24 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-03 18:16 Ken Brown
2021-04-06 14:20 ` Corinna Vinschen
2021-04-06 14:33   ` Corinna Vinschen
2021-04-06 15:44     ` Ken Brown
2021-04-06 16:28       ` Corinna Vinschen
2021-04-06 17:37         ` Ken Brown
2021-04-06 18:24           ` Corinna Vinschen [this message]
2021-04-06 19:36             ` Corinna Vinschen
2021-04-07 14:04               ` Ken Brown
2021-04-07 15:31                 ` Corinna Vinschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YGynRrexRDaqT0MU@calimero.vinschen.de \
    --to=corinna-cygwin@cygwin.com \
    --cc=cygwin-developers@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).