Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jeremyapp

macrumors newbie
Original poster
Apr 30, 2008
27
0
Hey,

I was wondering if anyone could point me in the right direction with this one. I have a fair amount of experience with Cocoa Touch, but I'm new to programming for the Mac.

I would like to create a very simple application, where when I press a button in it, it broadcasts a data packet on its local network. I would like to have a similar application that listens for this data packet, and does something when it receives one.

Where should I look for help in creating such an application?

Thanks!
 
Bonjour.

On other platforms it is known as mDNSresponder.

This may be more complicated than desired, especially considering it's not actually a solution. Bonjour just helps you find other hosts/services. It doesn't do any of the actual communication. A UDP broadcast would probably be a fair amount easier to set up than Bonjour.
 
This may be more complicated than desired, especially considering it's not actually a solution. Bonjour just helps you find other hosts/services. It doesn't do any of the actual communication. A UDP broadcast would probably be a fair amount easier to set up than Bonjour.

No but Bonjour handles all the listening etc. Plus the daemon is already done that listens for and sends out requests for services. You just need to implement the service yourself.

It is more of a framework which eases the process than anything else.
 
No but Bonjour handles all the listening etc. Plus the daemon is already done that listens for and sends out requests for services. You just need to implement the service yourself.

It is more of a framework which eases the process than anything else.

Unless we receive more info from the OP, we have no reason to believe that Bonjour will provide any benefit at all, especially since he's still going to have to implement the actual broadcast and receiving of the actual UDP packet.

Despite the conceptual relationship from the user's point of view, when it comes to implementation, Bonjour is wholly independent from the actual service being offered. What Bonjour helps with is figuring out what IP addresses and ports provide an arbitrary service. It doesn't actually help with the arbitrary service itself--at all. It's been a few years since I've written any bonjour code myself, but this much I remember very clearly.
 
I imagine I won't be needing anything like Bonjour, simply because I don't really care about who is receiving the message. In fact, I don't particularly care about the data that is being sent or received either.

Essentially, what I'm creating is akin to a doorbell. All I really need is an application that broadcasts a very simple message (a single digit, perhaps?) on a local network. I'll have a client open on a few computers that listens for this message and does something when it's heard (i.e. play a sound, etc.).

Because I don't really care about the data being sent or who gets the message, I was wondering if there was a relatively easy way to accomplish this.
 
As the initial reply stated: UDP broadcast.

UDP is connectionless, as opposed to TCP which requires creating a connection and knowing both ends.

The broadcast example posted, though, requires at least knowing the IP addresses of who is listening, which makes it the wrong sample (TCP). What you really want are the connectionless (UDP) sender/receiver samples:

http://www.cs.utah.edu/~swalton/listings/sockets/programs/part1/chap4/connectionless-sender.c
http://www.cs.utah.edu/~swalton/listings/sockets/programs/part1/chap4/connectionless-receiver.c

(from the URL posted by the first response.)

While this sample code is intended for Linux, I did just verify that they do compile and WORK on Snow Leopard. It's a hair more than what you want, but just a hair.


Note: I did get compile warnings about implicit declarations of exit() and abort(). This is because they're in a different header (stdlib.h) on Mac OS X. So, you should add that include to get rid of the warnings.
 
Thanks for the help!

I was able to make this work. I had to make a few modifications to that connectionless sender code but it's working great. If you're interested, here's the code that broadcasts the message:

Code:
- (void)broadcastAlert {
	char string[15] = "Hello World!";
	char buffer[BUFSIZE];
	struct sockaddr_in addr;
	int sd, addr_size;
	
	if ( (sd = socket(PF_INET, SOCK_DGRAM, 0)) < 0 )
	{
		NSLog(@"Socket");
	}
	addr.sin_family = AF_INET;
	addr.sin_port = htons(9999);
	if ( inet_aton("127.0.0.1", &addr.sin_addr) == 0 )
	{
		NSLog(@"127.0.0.1");
	}
	if(sendto(sd, string, 13, 0, (struct sockaddr*)&addr, sizeof(addr))) {
		NSLog(@"Message sent!");
	}
	bzero(buffer, BUFSIZE);
	addr_size = sizeof(addr);
	close(sd);
}

I ran the connectionless receiver code as-is on a second machine on the network and it logged my message. All that's left now is to incorporate that into a GUI app and add whatever effect I'll need.

Thanks again!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.