2
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
3
Copyright (C) 2005- The University of Notre Dame
4
This software is distributed under the GNU General Public License.
5
See the file COPYING for details.
11
/** @file datagram.h UDP datagram communications.
12
This module implements datagram communications using UDP.
13
A datagram is a small, fixed size message send to a given
14
host and port, which is not guaranteed to arrive.
16
This module is arguably misnamed: A <tt>struct datagram</tt>
17
does not represent a datagram, but rather an open port that
18
can be used to send datagrams with @ref datagram_send.
25
const char *message = "hello world!";
26
const char *address = "192.168.2.1";
29
d = datagram_create(DATAGRAM_PORT_ANY);
30
if(!d) fatal("couldn't create datagram port");
32
datagram_send(d,message,strlen(message),address,port);
35
And an example receiver:
41
char message[DATAGRAM_PAYLOAD_MAX];
42
char address[DATAGRAM_ADDRESS_MAX];
45
d = datagram_create(port);
46
if(!d) fatal("couldn't create datagram port");
48
length = datagram_recv(d,message,sizeof(message),&address,&port,10000000);
51
printf("got message: %s\n",message);
53
printf("no message received.\n");
60
/** Maximum number of characters in a text formatted datagram address. */
61
#define DATAGRAM_ADDRESS_MAX 48
63
/** Maximum number of bytes in a datagram payload */
64
#define DATAGRAM_PAYLOAD_MAX 65536
66
/** Used to indicate any available port. */
67
#define DATAGRAM_PORT_ANY 0
69
/** The address to send to for broadcasting. */
70
#define DATAGRAM_ADDRESS_BROADCAST "255.255.255.255"
72
/** Create a new port for sending or receiving datagrams.
73
@param port The UDP port number to bind to. On most versions of Unix, an ordinary user can only bind to ports greater than 1024.
74
@return A new object for sending or receiving datagrams. On failure, returns null and sets errno appropriately. A very common error is EADDRINUSE, which indicates another process is already bound to that port.
76
struct datagram * datagram_create( int port );
78
/** Destroy a datagram port.
79
@param d The datagram object to destroy.
81
void datagram_delete( struct datagram *d );
83
/** Receive a datagram.
84
@param d The datagram object.
85
@param data Where to store the received message.
86
@param length The length of the buffer, typically DATAGRAM_PAYLOAD_MAX.
87
@param addr Pointer to a string of at least DATAGRAM_ADDRESS_MAX characters, which will be filled in with the IP address of the sender.
88
@param port Pointer to an integer which will be filled in with the port number of the sender.
89
@param timeout Maximum time to wait, in microseconds.
90
@return On success, returns the number of bytes received. On failure, returns less than zero and sets errno appropriately.
92
int datagram_recv( struct datagram *d, char *data, int length, char *addr, int *port, int timeout );
95
@param d The datagram object.
96
@param data The data to send.
97
@param length The length of the datagram.
98
@param addr The address of the recipient.
99
@param port The port of the recipient.
100
@return On success, returns the number of bytes sent. On failure, returns less than zero and sets errno appropriately.
102
int datagram_send( struct datagram *d, const char *data, int length, const char *addr, int port );
104
/** Obtain the file descriptor of a datagram object.
105
@param d The datagram object.
106
@return The file descriptor associated with the underlying socket.
108
int datagram_fd( struct datagram *d );