This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

non-blocking socket?



Hi, I am wondering there is any way to use a non-blocking receive in
cygwin programs. I don't care if it is winsock or unix emulation
socket. From what we learned from cygwin faq, I tried the following program
but it still appears to block.

I appreciate if non-blocking socket is supposed to work and if so, how
to use it.

Thanks.

------------------

prompt% gcc nb.c -lwsock32
prompt% ./a.exe
now try to receive from port 30000
    < ... blocking ... >

------------------


#define Win32_Winsock 1
#include <windows.h>

#define PORT 30000

main(int argc, char **argv)
{
  int s;
  int flags;
  struct sockaddr_in from;
#define N 100
  char buf[N];

  {
    WORD wVersionRequested = MAKEWORD( 1, 1 );
    WSADATA wsaData[1];
    if (WSAStartup(wVersionRequested, wsaData) != 0) {
      printf("WSAStartup failed\n");
    }
  }

  if ((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
    printf("socket failed\n");
    exit(1);
  }
  {			       
    /* winsock function to make it non-blocking */
    unsigned long a[1];
    int e = ioctlsocket(s, FIONBIO, a);
    if (e == -1) {
      printf("ioctlsocket failed\n");
      exit(1);
    }
  }

  memset(&from, '\0', sizeof(from));
  from.sin_family = AF_INET;
  from.sin_addr.s_addr = htonl(INADDR_ANY);
  from.sin_port = htons((u_short)PORT);
  if (bind(s, (struct sockaddr *)&from, sizeof(from)) == -1) {
    printf("bind failed\n");
    exit(1);
  }
  {
    struct sockaddr_in addr;
    int flen = sizeof(struct sockaddr_in);
    int _ = printf("now try to receive from port %d\n", PORT);
    int l = recvfrom(s, buf, N, 0, (struct sockaddr *)&addr, &flen);
    printf("OK: l = %d\n", l);
  }
}

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]