discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sending UDP packets


From: Lars Sonchocky-Helldorf
Subject: Re: Sending UDP packets
Date: Wed, 25 Nov 2009 23:24:14 +0100


Am 25.11.2009 um 21:39 schrieb Andreas Höschler:

Hi all,

This is not really GNUstep related except that the problem occurs in a MacOSX/GNUstep application (Objective-C wrapper around TCP code) but I hope one of you can help me anyway. I need to notify a dozen of MacOSX/GNUstep applications when a specific event occurs in some service process. I thought it would be a good idea to use UDP packets for that. I googled and found the following code snippets

**********************************
UDP sender
**********************************
#define BUFLEN 512
#define NPACK 10
#define PORT 9930
#define SRV_IP "127.0.0.1"

     {
      struct sockaddr_in si_other;
      int s, i, slen=sizeof(si_other);
      char buf[BUFLEN];
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) NSLog(@"socket could not be opened");

     memset((char *) &si_other, 0, sizeof(si_other));
      si_other.sin_family = AF_INET;
      si_other.sin_port = htons(PORT);
      if (inet_aton(SRV_IP, &si_other.sin_addr)==0)
      {
         fprintf(stderr, "inet_aton() failed\n");
         exit(1);
      }

      for (i=0; i<NPACK; i++)
     {
         printf("Sending packet %d\n", i);
         sprintf(buf, "This is packet %d\n", i);
         if (sendto(s, buf, BUFLEN, 0, &si_other, slen) == -1)
           NSLog(@"sendto() failed");
       }
       close(s);
     }

**********************************
UDP receiver
**********************************
     {
      struct sockaddr_in si_me, si_other;
      int s, i, slen=sizeof(si_other);
      char buf[BUFLEN];
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) NSLog (@"socket failure");
      memset((char *) &si_me, 0, sizeof(si_me));
      si_me.sin_family = AF_INET;
      si_me.sin_port = htons(PORT);
      si_me.sin_addr.s_addr = htonl(INADDR_ANY);
      if (bind(s, &si_me, sizeof(si_me))==-1) NSLog(@"bind failed");
      for (i=0; i<NPACK; i++)
        {
if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)NSLog (@"recvfrom()"); printf("Received packet from %s:%d\nData: %s\n\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
        }
     }

This works great on my Mac (10.2) when SRV_IP is "127.0.0.1". However, in the end the sender process will run on machine A and the receiver process on machine B, C, D, ... in our 192.168.1.0 LAN. I therefore tried to use SRV_IP = "192.168.1.255" instead. When I do that I get

UDPSender[9746] sendto() failed

Why does this not work or what is the correct address to use for interhost UDP traffic in a LAN? What am I doing wrong?

Try using multicast IPs:

http://tools.ietf.org/html/rfc3171

http://en.wikipedia.org/wiki/Multicast_address

http://en.wikipedia.org/wiki/IP_multicast


Thanks a lot,

 Andreas

regards,

        Lars





reply via email to

[Prev in Thread] Current Thread [Next in Thread]