Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
net_udp_server.c

UDP echo server example.

UDP echo server example.Demonstrates: bj_create_udp, bj_receive_udp, bj_send_udp, bj_datagram_* accessors. Pair with udp_client.c to test.

#include <banjo/log.h>
#include <banjo/main.h>
#include <banjo/net.h>
#include <banjo/system.h>
#define PORT 9000
int main(int argc, char* argv[]) {
(void)argc;
(void)argv;
return 1;
}
struct bj_error* error = 0;
struct bj_udp* udp = bj_create_udp("0.0.0.0", PORT, BJ_NET_HINT_IPV4, &error);
if (!udp) {
bj_err("server: failed to open UDP socket (%s)", bj_error_message(error));
bj_clear_error(&error);
bj_end();
return 1;
}
bj_info("server: listening on port %d", bj_udp_local_port(udp));
bj_info("server: waiting for datagrams...");
while (1) {
struct bj_datagram* dgram = bj_receive_udp(udp, &error);
if (!dgram) {
if (error) {
bj_err("server: recv error (%s)", bj_error_message(error));
bj_clear_error(&error);
}
continue;
}
char addr[BJ_ADDR_STRLEN];
bj_datagram_address(dgram, addr, sizeof(addr));
uint16_t sender_port = bj_datagram_port(dgram);
bj_info("server: received %zu bytes from %s:%d",
bj_datagram_length(dgram), addr, sender_port);
// Echo the data back
int sent = bj_send_udp(udp, addr, sender_port,
bj_datagram_data(dgram), bj_datagram_length(dgram), &error);
if (sent < 0) {
bj_err("server: send error (%s)", bj_error_message(error));
bj_clear_error(&error);
} else {
bj_info("server: echoed %d bytes back", sent);
}
}
bj_end();
return 0;
}
struct bj_datagram bj_datagram
Definition api.h:346
struct bj_error bj_error
Definition api.h:348
struct bj_udp bj_udp
Definition api.h:379
const char * bj_error_message(const struct bj_error *error)
Gets the error message from an error object.
void bj_clear_error(struct bj_error **error)
Frees an error and sets the pointer to NULL.
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:141
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
Definition log.h:169
struct bj_datagram * bj_receive_udp(struct bj_udp *udp, struct bj_error **error)
Receives a datagram from the UDP socket.
size_t bj_datagram_length(const struct bj_datagram *dgram)
Gets the data length from a datagram.
uint16_t bj_datagram_port(const struct bj_datagram *dgram)
Gets the sender's port from a datagram.
void bj_destroy_datagram(struct bj_datagram *dgram)
Closes a datagram and frees resources.
void bj_destroy_udp(struct bj_udp *udp)
Closes a UDP socket.
struct bj_udp * bj_create_udp(const char *host, uint16_t port, uint16_t hints, struct bj_error **error)
Opens a UDP socket bound to the specified port.
uint16_t bj_udp_local_port(const struct bj_udp *udp)
Gets the local port of a UDP socket.
int bj_send_udp(struct bj_udp *udp, const char *host, uint16_t port, const void *data, size_t len, struct bj_error **error)
Sends a datagram to the specified destination.
#define BJ_ADDR_STRLEN
Maximum length of an address string (including null terminator).
Definition net.h:275
size_t bj_datagram_address(const struct bj_datagram *dgram, char *buf, size_t size)
Gets the sender's address from a datagram.
const void * bj_datagram_data(const struct bj_datagram *dgram)
Gets the data pointer from a datagram.
@ BJ_NET_HINT_IPV4
Restrict address resolution to IPv4.
Definition net.h:121
bj_bool bj_begin(int systems, struct bj_error **error)
Initialises the system.
void bj_end(void)
De-initialises the system.
@ BJ_NETWORK_SYSTEM
Definition system.h:82
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for network API.
#define PORT
Header file for system interactions.
int main(void)