discuss-gnustep
[Top][All Lists]
Advanced

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

Re: Sending UDP packets


From: Saso Kiselkov
Subject: Re: Sending UDP packets
Date: Thu, 26 Nov 2009 09:05:31 +0100
User-agent: Thunderbird 2.0.0.23 (X11/20090817)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Try replacing the "NSLog (@"sendto() failed");" with "perror ("sendto()
failed") - that will tell you exactly what went wrong. Also, as Lars
suggested, your application might be an ideal example of when to use IP
multicast (if you need, I can rewrite this code to work over multicast).

- --
Saso

Andreas Höschler wrote:
> 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?
> 
> Thanks a lot,
> 
>  Andreas
> 
> 
> 
> 
> 
> _______________________________________________
> Discuss-gnustep mailing list
> Discuss-gnustep@gnu.org
> http://lists.gnu.org/mailman/listinfo/discuss-gnustep

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAksONskACgkQRO8UcfzpOHDo6QCgpgAgvbgo2AjSTrpBV+nChw/9
Q8AAoIi6U065cqsUmK5EkAwV6bvvfYij
=ptJm
-----END PGP SIGNATURE-----




reply via email to

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