[uBroadcast] Says “Hello world!” in C

This is it, my first article with a working uBroadcast library in C. Today I discuss a simple example with two programs: the first one is a UDP broadcast sniffer, the second one sends “Hello world!” messages on the network. While furnished archive contains BugOne communication code, I’ll describe it later.

I tried to keep the API as simple as possible. The first header file I’ll discuss is uBroadcast.h. It contains functions for uMessage (structure containing message data) manipulation. The second one is uBroadcast_UDP.h which contains the UDP communication functions.

First, one need a message. To understand, how to use functions in uBroadcast.h, you can read the following example. A simple “Hello world!” message is created.

// Message creation
uMessage msg;
// Clear message (init checksum, clear data)
ub_clear(&msg);
// Write data, here a string
ub_write_str(&msg, "Hello world!");
// Finalize message, write checksum
ub_flush(&msg);

// Check message validity
if (!ub_check(&msg)) {
  printf("error: invalid message!");
}

By now, I miss some functions to write int or float values as I didn’t required them to start. However I’ll create them when I’ll implement the BugOne protocol.

The second useful header is uBroadcast_UDP.h. It allows to open UDP connection, broadcast and receive messages. Two examples are provided, the first allows to send a message, the next one is the code of the sniffer.

// INIT MESSAGE (SEE ABOVE)
// CONNECT, INIT MEMORY, USE DEFAULT PORT
ub_UDP * conn = ub_udp_connect(UB_BROADCAST_UDP_PORT);

// Send message
ub_udp_send(conn, &msg);
// Disconnect (frees the memory, pointer is no more usable)
ub_udp_disconnect(conn);

The code above broadcasts a message (for example “Hello world!”) to the default uBroadcast port. A code that sends a message with no one listening its useless. The next piece of code, is a uBroadcast UDP sniffer.

#include "uBroadcast_UDP.h"

#include <stdio.h>
#include <unistd.h>

int main(void) {
	uMessage msg;
	ub_UDP * conn = ub_udp_connect(UB_BROADCAST_UDP_PORT);

	ub_clear(&msg);

	while (1) {
		if (!ub_udp_recv(conn, &msg)) {
			ub_debug(&msg);
		}
	}

	ub_udp_disconnect(conn);
}

The sniffer opens a connection on default uBroadcast port, message is initialized and then every correct message received is printed to standard output.

Note that recv function is blocking. Error may happen during reception, either due to socket or when you receive you’re own message… yes, when application A broadcast a message, it is received in other applications but also by application A. So uBroadcast library has a simple mechanism to handle message received back … but with no guaranty, sorry.

Full source code is available here, just extract the archive and run make in the folder, you will obtain binaries particularly: udp_sniffer and udp_hello. When launching udp_hello, you’ll get a point on each “Hello world!” sent. udp_sniffer on the other side display an hex version of the messages received. Here is a screenshot of the result.

uBroadcast_hello

Leave a Reply

Your email address will not be published. Required fields are marked *