Professional Web Applications Themes

Broadcasting via sockets? - Mac Programming

Folks, since I'm doing this in a Cocoa app on OS X, I hope you don't mind if I ask this Unix Sockets question here. If anyone knows a sockets NG or so, feel free to point me there. I'm trying to get a feel of sockets, so I thought I'd write a simple P2P app. I got stream sockets working to transfer data between two apps just fine, but for the life of me I can't get broadcast sockets to work, which I'd need to find other copies of my P2P app on the net. It gets its own ...

  1. #1

    Default Broadcasting via sockets?

    Folks,

    since I'm doing this in a Cocoa app on OS X, I hope you don't mind if I
    ask this Unix Sockets question here. If anyone knows a sockets NG or so,
    feel free to point me there. I'm trying to get a feel of sockets, so I
    thought I'd write a simple P2P app.

    I got stream sockets working to transfer data between two apps just
    fine, but for the life of me I can't get broadcast sockets to work,
    which I'd need to find other copies of my P2P app on the net. It gets
    its own loopback packet just fine, but the other copies on my network
    don't notice the transmissions.

    I gave my group the address 226.0.0.1 (found that in some code on the
    web), and I'm using port 9999. Also tried 50000, but that didn't do any
    good either. I use socket(AF_INET, SOCK_DGRAM, IPPROTO_IP) to create the
    little critters.

    I could post some actual source code, but thought I'd not scare away
    everyone right away.

    I'd also be interested in any working sample code you happen to have.

    Cheers,
    -- M. Uli Kusterer
    Uli Guest

  2. #2

    Default Re: Broadcasting via sockets?

    In article <t-online.com>,
    Uli Kusterer <de> wrote:
     

    You can't just pick a random broadcast address to send on. Each one of your
    network interfaces has a broadcast address, and that's the address you should
    send to. I believe that the ADDRESS & NETMASK | ~NETMASK is reserved for the
    broadcast address, but I am not 100% sure that's guaranteed.

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

  3. #3

    Default Re: Broadcasting via sockets?

    In article <mit.edu>,
    Miro Jurisic <org> wrote:
     

    Miro,

    do you know any place where I can find out more about this? The best
    site I found so far was <http://www.ecst.csuchico.edu/~beej/guide/net/>
    but neither that nor the Unix Sockets FAQ talk much about broadcasting.

    Furthermore, I couldn't find much info on the netmask (it is a pretty
    un-googleable term, as it seems to be a name in loads of command-line
    tools). There seems to be no mention of it in the manpages, at least
    according to apropos and xCode's man page search.

    I also found this: <http://mail-index.netbsd.org/port-vax/1999/03/17/0007.html>
    but this doesn't mention anything about how I'd get a netmask and
    finally a broadcast address, and in.h says:

    #define INADDR_BROADCAST (u_int32_t)0xffffffff /* must be masked */

    But nowhere says with what mask or how I'd generate one. How do I pass
    that into a call like sendto() etc. that takes a sockaddr_in?

    I'm sorry, but I've been running my head against the wall the past week,
    and I'm out of places to look for to get this info :-(

    I'm really at the end of my straw.

    Grumbling,
    -- M. Uli Kusterer
    http://www.zathras.de
    Uli Guest

  4. #4

    Default Re: Broadcasting via sockets?

    Uli Kusterer <de> wrote in message news:<t-online.com>... 

    You might want to look into Rendezvous, it's easy to add to Cocoa apps
    and would let you automatically find other servers. Check here:

    http://www.macdevcenter.com/pub/a/mac/2002/11/15/cocoa.html?page=1

    for how to do it. This is how iTunes finds other shared libraries,
    Safari finds other Macs with web servers and Hydra from the
    CodingMonkeys (http://hydra.globalse.org) finds other copies of itself
    for collaborative editing.

    Note: Apple has cross platform versions of Rendezvous so you could
    add the same capabilities to a Windows app. (See Windows iTunes)

    --jim
    Jim Guest

  5. #5

    Default Re: Broadcasting via sockets?

    In article <t-online.com>,
    Uli Kusterer <de> wrote:
     
    >
    > do you know any place where I can find out more about this? The best
    > site I found so far was <http://www.ecst.csuchico.edu/~beej/guide/net/>
    > but neither that nor the Unix Sockets FAQ talk much about broadcasting.[/ref]

    I don't have a copy handy, but I am 99% sure that the Stevens' TCP/IP books talk
    about this.
     

    I don't know how to do it other than using OT. In fact, InetInterfaceInfo has
    fBroadcastAddr, which I would therefore suggest you use :-)

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

  6. #6

    Default Re: Broadcasting via sockets?

    Miro Jurisic wrote: 

    no, not true. you can broadcast datagrams on any socket address. but
    the caveat is that, to receive those broadcasts, other nodes must all be
    listening on that same address. you might be confused about INADDR_ANY
    here, i'm not sure. INADDR_ANY doesn't have anything to do with
    datagrams or broadcasts.

    as for the original poster's question, sorry, you haven't provided
    enough information. i've written my own tcp/ip discovery protocols and
    i can attest to the fact that datagram broadcast works just fine on
    berkeley sockets, and on macosx.

    but here's something you might not know: broadcast datagrams won't work
    on the internet as a whole, just on your local LAN. ISPs set up their
    routers to reject broadcast datagrams. if they had to relay every
    broadcast to every host on the internet, they'd get so bogged down that
    there would be no bandwidth left over for anything else.

    that leaves internet discovery of peers as an interesting and mostly
    unsolved problem. that's why many p2p networks have relied on central
    directories, the way napster was set up.
    Jhnny Guest

  7. #7

    Default Re: Broadcasting via sockets?

    In article <t-online.com>,
    Uli Kusterer <de> wrote:
     

    I wouldn't recommend broadcasting. Simple multicasting would be slightly
    better, but the best way to do this is with Rendezvous (which uses
    multicast) since this is exactly what Rendezvous was designed for
    (service discovery).

    That said, if you want to send broadcast packets, you should create a
    UDP socket with socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ), bind to the
    wildcard address (INADDR_ANY) and a fixed port (e.g. a port you should
    register with IANA if you're going to release your app to the public.
    For private use, just pick something that isn't in use in the IANA port
    number list), enable the SO_BROADCAST option with setsockopt, send
    packet(s) to 255.255.255.255 and the fixed port you picked, and call
    recvfrom to receive responses. The IP stack will translate
    255.255.255.255 to a subnet-directed broadcast (e.g. 10.0.1.255)
    automatically (on BSD systems...Windows will let you send a real
    all-ones broadcast, which I think is more correct).

    Using Rendezvous is simpler, more efficient on the network, and provides
    much more functionality. If you're running Panther, all you have to do
    is call DNSServiceRegister to publish your app then use DNSServiceBrowse
    to find it. You'll get called back for each instance of the app on the
    network along with its name, port number, extra information via the TXT
    record(s), etc. If you use Rendezvous, you also won't need to register a
    fixed port with IANA since it is discovered dynamically.

    Since you mentioned you're developing a P2P app, you should know that
    neither broadcast nor Rendezvous is going to allow you to discover other
    instances of your app across networks. It is limited to link-local
    discovery (routers won't forward broadcast or multicast DNS packets by
    design).

    BTW...If you're going to do any network programming, I'd highly
    recommend getting Unix Network Programming Volume 1, 3rd Edition.
    Bob Guest

  8. #8

    Default Re: Broadcasting via sockets?

    In article <nashville.comcast.net>,
    Jhnny Fvrt (it means "genetic antagonism") <com>
    wrote:
     

    "Jhnny",

    that sounds like good news for me :-)
     

    I never meant to imply that it didn't work. Simply that because of some
    lack of knowledge and sources to correct this lack I simply can't find
    the way that makes it work. I wouldn't have blamed the computer until a
    few more weeks of banging my head had gone by ... :-)
     

    I guessed that it wouldn't go across the whole internet, though I had
    hoped that they'd at least be forwarded into "the next subnetwork".
    Well, it's not important. Right now if this works on a LAN it's already
    pretty nice. It's only a toy right now, but I have an idea where it
    could be used in practice, but even there it would be a LAN-only
    solution.
     

    Ah. I'd always wondered about that. I thought it was for performance
    reasons, thanks for telling me about the details.

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
    Uli Guest

  9. #9

    Default Re: Broadcasting via sockets?

    In article <nashville.comcast.net>,
    Jhnny Fvrt (it means "genetic antagonism") <com>
    wrote:
     

    Oh, one more question:

    Is there a Safe(tm) or Recommended(tm) way of getting such a socket
    address? I guess hard-coding it isn't really a nice thing to do. I guess
    I could use Rendezvous, but actually I'd kind of like top avoid that
    right now. Is hard-coding it (maybe letting the user change it) the best
    way right now, or is there a better option?

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
    Uli Guest

  10. #10

    Default Re: Broadcasting via sockets?

    In article <google.com>,
    net (Jim Schimpf) wrote:
     

    Thanks,

    Somehow after coming across cocoadevcentral I had forgotten that it
    wasn't the same as macdevcenter. Good to know they have an article on
    that.

    But Rendezvous only works in LANs, right?

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
    Uli Guest

  11. #11

    Default Re: Broadcasting via sockets?

    Uli Kusterer wrote: 

    as far as i'm aware, hard-coding is the only way. that's kind of the
    tradition. the same way that all web servers are expected to serve up
    content on port 80, for instance.

    so the traditional thing is to pick some oddball high-numbered port for
    your broadcast datagrams. there is a small chance you'll pick the same
    port as some other app the end user has, so it's good if there's a way
    for the user to change it.
     

    i don't know very much about rendezvous. it's supposedly apple's
    version of an open standard, but i haven't heard of very much adoption
    by other companies, so for now i'm avoiding it. i prefer stuff that's
    widely available and internet-standard.
    Jhnny Guest

  12. #12

    Default Re: Broadcasting via sockets?

    In article <t-online.com>, Uli Kusterer wrote: 

    That's a multicast address rather than a broadcast one. You have to create
    the sockets differently to make these work - see:

    http://ntrg.cs.tcd.ie/undergrad/4ba2/multicast/antony/example.html

    for some examples using berkley sockets, and http://www.tack.ch/multicast/
    for more examples with some explanation. Whether you can send/receive
    multicasts from outside your network is entirely up to your ISP,
    however.

    (Note especially the setsockopt() calls in the examples: these are the
    important parts)

    Andrew.
    Andrew Guest

  13. #13

    Default Re: Broadcasting via sockets?

    In article <nashville.comcast.net>,
    Jhnny Fvrt (it means "genetic antagonism") <com>
    wrote:
     

    Rendezvous has actually hand a pretty amazing adoption rate compared
    with other technologies:

    - Virtually every IP printer out there either supports Rendezvous or is
    in the process of adding it.

    - RAID systems (Apple, Chaparral, etc.).

    - Sybase

    - TiVo

    - Obviously every Apple product (e.g. AirPort, iTunes, iChat, etc.).

    - Lots of non-Apple software (e.g. FTP clients, Web browsers,
    collaboration software, etc)

    - And more products are adopting it every day.

    Apple provides implementations and sample apps for just about every
    platform out there: Mac OS X, Mac OS 9, Windows, Windows CE, VxWorks,
    Linux, Solaris, and any other POSIX-ish Unix platform. All available via
    Apple's Open Source project:

    <http://developer.apple.com/darwin/projects/rendezvous/>
     

    Rendezvous is available as an open standard:

    <http://www.zeroconf.org/>
    <http://www.multicastdns.org/>

    There are IETF Internet drafts available for everything.
    Bob Guest

  14. #14

    Default Re: Broadcasting via sockets?

    In article <logicalshift.demon.co.uk>,
    Andrew Hunter <demon.co.uk> wrote:
     

    Yeah, I realize that I was apparently mixing up some terms here. I'm
    simply trying to send a message to all computers in the LAN. Whether I
    broadcast, multicast or gypsum cast, I don't really care.
     

    Andrew,

    thanks, I'll check those sites out.

    Cheers,
    -- Uli
    Uli Guest

  15. #15

    Default Re: Broadcasting via sockets?

    In article <nashville.comcast.net>,
    Jhnny Fvrt (it means "genetic antagonism") <com>
    wrote:
     

    Sorry, I didn't mean the port. I'm aware that those are generally
    registered with the IANA. What I really meant was the address used for
    the group for the broadcast (multicast?) on a network to which the
    broadcasted addresses go.

    Cheers,
    -- Uli
    Uli Guest

  16. #16

    Default Re: Broadcasting via sockets?

    In article <apple.com>,
    Bob Bradley <com> wrote:
     

    I think the code I was working with actually was multicast. -- At least
    I was using the setsockopt() calls to loop multicasts and such stuff.
    Guess I mixed the two up. Sorry.
     

    Thanks Bob. Good to know that. But for now, having it not propagate
    into the internet may actually be a good thing.

    Cheers,
    -- Uli
    Uli Guest

  17. #17

    Default Re: Broadcasting via sockets?

    In article <mit.edu>,
    Miro Jurisic <org> wrote:
     

    Ah! I'll check that, thanks :-)

    Cheers,
    -- Uli
    Uli Guest

  18. #18

    Default Re: Broadcasting via sockets?

    Uli Kusterer wrote: 
    >
    > Sorry, I didn't mean the port. I'm aware that those are generally
    > registered with the IANA. What I really meant was the address used for
    > the group for the broadcast (multicast?) on a network to which the
    > broadcasted addresses go.[/ref]

    d00d. you're not "getting" broadcast datagrams yet!

    you set up a socket for UDP broadcasting, and bind it to your chosen
    port. send datagrams on it. at the other end, programs set up a UDP
    socket to listen on the same port. all such listeners will receive the
    broadcast.

    there are no other ip or port addresses involved. all peers use their
    regular old IP addresses. the UDP port number for senders and listeners
    is the same. i repeat: there are no other ip or port addresses
    involved.
    Jhnny Guest

Similar Threads

  1. broadcasting events
    By SilentCry in forum ASP.NET Building Controls
    Replies: 2
    Last Post: May 23rd, 11:42 AM
  2. broadcasting when a connection is closed
    By natebaca in forum Macromedia Flash Flashcom
    Replies: 0
    Last Post: September 13th, 05:52 AM
  3. broadcasting flv files
    By toeriffic in forum Macromedia Flash Flashcom
    Replies: 2
    Last Post: August 25th, 11:51 PM
  4. Requeriment for broadcasting
    By carlosguillermogonza in forum Macromedia Flash Flashcom
    Replies: 0
    Last Post: July 25th, 08:49 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139