[erlang-questions] sending packets using gen_udp

Chandru <>
Thu Aug 2 19:41:14 CEST 2007


On 01/08/07, igwan <> wrote:
> Chandru a écrit :
> >
> > Where are you sending the packets? When you say you see only 9 packets
> > on your network interface, which n/w interface are you talking about?
> > The source or the destination? If it is the destination, probably your
> > receiving end has a small receive buffer?
> >
>
> I'm talking about the source interface. I checked the stats with "ip -s
> link" and the counter increments only by nine packets and of course the
> destination doesn't get more. This is on a 100 Mbps ethernet interface
> and I'm able to reach a decent sending rate if I wait between each
> sending, but this puts a high load on my emulator and complicates
> programming.
>

I wrote some C code to test this and it turns out that I get a similar
behaviour. I actually see more lost packets when running the C program
compared to erlang.

So I don't think this is an erlang specific problem. I think the
packets are being dropped somewhere in the kernel and this being UDP,
you are not being told about it. I've attached the C code in case  you
are interested in trying. Edit the macros in the code to change the
"destination host" and "send buffer size".

cheers,
Chandru
-------------- next part --------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>

#define DEST_HOST "172.24.19.78"
#define DEST_PORT 12345
#define SEND_BUFFER_SIZE 100000
#define NUM_PACKETS_TO_SEND 100
#define SEND_DATAGRAM_SIZE 1472

int 
main (void)
{
  int s = socket (AF_INET, SOCK_DGRAM, 0);	/* open UDP socket */
  char datagram[SEND_DATAGRAM_SIZE];	
  struct sockaddr_in cli_addr;
  struct sockaddr_in sin;
  int count = 0;
  int send_buffer = SEND_BUFFER_SIZE;

  sin.sin_family = AF_INET;
  sin.sin_port = htons (12345);
  sin.sin_addr.s_addr = inet_addr (DEST_HOST);

  cli_addr.sin_family = AF_INET;
  cli_addr.sin_port = htons (DEST_PORT);
  cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);


  bind(s, (struct sockaddr *) &cli_addr, sizeof(cli_addr));
    if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &send_buffer, sizeof(send_buffer)))
    {
      printf("setsockopt error %d, %s\n", errno, strerror(errno));
      exit(errno);
    }

  memset (datagram, 0, SEND_DATAGRAM_SIZE);

  while (count < NUM_PACKETS_TO_SEND)
    {
      /* Indicate the packet count in the first two bytes */
      datagram[0] = (count & 0xff00) >> 8;
      datagram[1] = count & 0xff;
      if (sendto (s,
		  datagram,
		  sizeof(datagram),
		  0,
		  (struct sockaddr *) &sin,
		  sizeof (sin)) < 0)
	printf ("error\n");
      else
	printf (".");
      count++;
    }
  printf ("Sent %d packets\n", count);
  return 0;
}



More information about the erlang-questions mailing list