Re: Basic networking question
Re: Basic networking question
- Subject: Re: Basic networking question
- From: Quinn <email@hidden>
- Date: Wed, 11 May 2005 10:54:56 +0100
At 15:34 -0700 10/5/05, Alex Kettner wrote:
I would like to be able to send and receive some data over the
network. The data size can be very small < 1K. The functionality is
basically to broadcast an application's existence once it starts up
and receive like packets of data from the network from other copies
of the same application.
At 9:19 +1000 11/5/05, Tom Birch wrote:
Will all the copies of the program be running on the same local
network, if so then Bonjour is what you most likely want to use for
applications to notify others of their existence. Establishing the
connection and sending data after that is very easy, so start by
reading the Bonjour documentation.
Good advice. Also, by using Bonjour for service discovery, you take
advantage of its sophisticated caching. Simply broadcasting "I'm
here" packets is very wasteful of network resources. So, I strongly
recommend Bonjour for the discovery part of your problem.
Bonjour takes care of the "broadcast an application's existence" part
of your question, but it doesn't really address the issue of how to
exchange data. The result of a Bonjour query is a list of IPv4/IPv6
addresses, and a port number. It's then up to you as to how to
communicate with that service.
The best approach is really determined by the specifics of your
application. You wrote "receive like [did you mean "little"?]
packets of data from the network from other copies of the same
application". This implies that you don't need a persistent
connection between your applications, so it might make sense to use
UDP to transmit this data. If that's the case, you should go looking
for UDP examples.
You then have to decide what level of API you want to use. If you're
integrating your code into some sort of GUI application (that is,
anything with CFRunLoop at its core), you probably want to look at
CFSocket. OTOH, if you're writing a command line tool, or you want
code portable to other platforms, you might want to use sockets
directly.
As Justin noted, Stevens is a great place to start with sockets code.
There are some very simple UDP send/receive examples in that book. I
can't recommend it highly enough.
I don't know of any samples on the CFSocket front, but it's not too
hard to understand once you understand the underlying sockets API.
So, how does this all fit together. Imagine a GUI application doing
UDP. Here's what I'd do on the receive side:
1. Create a UDP socket using the sockets API.
2. Wrap that socket in a CFSocket.
3. Add that socket to your runloop. This sets up a data callback for socket.
4. Use CFNetServices to register your service with Bonjour
(CFNetServiceRegister).
5. When data arrives, CFSocket will call your socket's callback. You
can then read the data and process it (or you can have CFSocket read
it for you).
And on the send side:
1. Use CFNetServices to browse for your service
(CFNetServiceBrowserCreate etc). This will notify you as services
come and go.
2. Once you find a service, use CFNetServices to resolve it
(CFNetServiceResolve). This will result in a port number and a list
of IPv4 and IPv6 addresses. [If you can't be bothered to deal with
IPv6, just ignore the IPv6 address. However, handling IPv6 is a
pretty simple extension.]
3. Create a UDP socket (or reuse the one you're using for listening)
and use it to send data to the service.
How does that sound?
Share and Enjoy
--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Technical Support * Networking, Communications, Hardware
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden