delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2001/08/14/12:34:37

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT sources DOT redhat DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sources DOT redhat DOT com>
List-Help: <mailto:cygwin-help AT sources DOT redhat DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT sources DOT redhat DOT com
Delivered-To: mailing list cygwin AT sources DOT redhat DOT com
X-Authentication-Warning: makita.cygnus.com: keiths owned process doing -bs
Date: Tue, 14 Aug 2001 09:33:59 -0700 (PDT)
From: Keith Seitz <keiths AT cygnus DOT com>
To: Roderick Groesbeek <rgroesb AT triple-it DOT com>
cc: <cygwin AT cygwin DOT com>
Subject: Re: 1.3.2: Cygwin && UDP && O_NONBLOCK
In-Reply-To: <00f801c124d4$40c6ed90$93b849d5@uu.net>
Message-ID: <Pine.GSO.4.33.0108140915470.23443-100000@makita.cygnus.com>
MIME-Version: 1.0

On Tue, 14 Aug 2001, Roderick Groesbeek wrote:

> #ifdef __linux__
>    #include <linux/in.h>
> #endif
> #ifdef __CYGWIN__
>    #include <cygwin/in.h>
> #endif

Why not just #include <netinet/in.h>?

>
> #include <unistd.h>
> #include <fcntl.h>
>
>
>
> #define BUFSIZE 1024
>
> int main(int argc, char* argv[], char* env[])
> {
>   int sock;
>   int ret;
>   char buf[BUFSIZE];
>   struct sockaddr_in me, from;
>   unsigned int from_len;
>
>   int flags;
>
>
>   sock = socket(PF_INET, SOCK_DGRAM, 0);
>
>   if (sock < 0) {
>         perror("socket");
>         exit(1);
>   }
>
>   flags = fcntl(sock, F_GETFL, 0);
>   ret = fcntl(sock, F_SETFL, flags & ~O_NONBLOCK);
>   printf("ret=%d|\n", ret);

This fcntl stuff shouldn't really be necessary, but it should do no harm.

>   bzero((char*) &from, sizeof(from));

Ugh. You mean "bzero (&me, sizeof (me));"?

>   me.sin_family = AF_INET;
>   me.sin_addr.s_addr = htonl(INADDR_ANY);
>   me.sin_port = htons(1025);
>   ret = bind(sock, (struct sockaddr *) & me, sizeof(me));
>   if (ret < 0) {
>         perror("bind");
>         exit(1);
>   }
>   while(1) {
>         int len;
>
>         len = recvfrom(sock, buf, BUFSIZE, 0, (struct sockaddr *) &from,
> &from_len);

This is not correct. from_len is not initialized (typo?). You MUST set it
to "sizeof (from)". This is a "value/result argument".

>         perror("recvfrom");
>         printf("len=%d|\n", len);
>   }
>
>   return 0;
> }

FWIW, I tried the following program on my system and had no problems
whatsoever, with or without SET_BLOCKING set.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#ifdef SET_BLOCKING
#include <fcntl.h>
#endif

#ifdef __CYGWIN__
typedef unsigned int socklen_t;
#endif

int
main (int argc, char *argv[])
{
  int ret;
  char buf[1024];
  socklen_t len;
  struct sockaddr_in my_addr, client_addr;
  int sock;
#ifdef SET_BLOCKING
  int flags;
#endif

  sock = socket (AF_INET, SOCK_DGRAM, 0);
  if (sock < 0)
    {
      perror ("couldn't create socket");
      exit (1);
    }

#ifdef SET_BLOCKING
  flags = fcntl (sock, F_GETFL, 0);
  flags &= ~O_NONBLOCK;
  ret = fcntl (sock, F_SETFL, flags);
  if (ret < 0)
   {
     perror ("couldn't force blocking");
     exit (1);
   }
#endif


  bzero (&my_addr, sizeof (my_addr));
  my_addr.sin_family = AF_INET;
  my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
  my_addr.sin_port = htons (10025);
  if (bind (sock, (struct sockaddr *) &my_addr, sizeof (my_addr)) < 0)
    {
      perror ("couldn't bind to address");
      exit (1);
    }

  while (1)
    {
      len = sizeof (client_addr);
      ret = recvfrom (sock, buf, 1024, 0, (struct sockaddr *) &client_addr, &len);
      printf ("recvfrom returned %d\n", ret);
      if (ret < 0)
	{
	  perror ("error getting data from socket");
	  continue;
	}

      /* Do something with the data. Echo it. */
      ret = sendto (sock, buf, ret, 0, (struct sockaddr *) &client_addr, len);
      if (ret < 0)
	{
	  perror ("error sending data");
	  continue;
	}

      printf ("Sent %d bytes to client at %x\n", ret, client_addr.sin_addr.s_addr);
    }

  return 0;
}




--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019