Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
net_udp_server.c

UDP echo server example.

UDP echo server example.Demonstrates: bj_open_udp, bj_udp_recv, bj_udp_send, 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_open_udp("0.0.0.0", PORT, BJ_RESOLVE_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_udp_recv(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_addr(dgram, addr, sizeof(addr));
uint16_t sender_port = bj_datagram_port(dgram);
bj_info("server: received %zu bytes from %s:%d",
bj_datagram_len(dgram), addr, sender_port);
// Echo the data back
int sent = bj_udp_send(udp, addr, sender_port,
bj_datagram_data(dgram), bj_datagram_len(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;
}
int main(int argc, char *argv[])
Definition audio_pcm.c:177
struct bj_datagram bj_datagram
Definition api.h:359
struct bj_error bj_error
Definition api.h:333
struct bj_udp bj_udp
Definition api.h:358
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
size_t bj_datagram_addr(const struct bj_datagram *dgram, char *buf, size_t size)
Gets the sender's address from a datagram.
void bj_close_datagram(struct bj_datagram *dgram)
Closes a datagram and frees resources.
struct bj_udp * bj_open_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_datagram_port(const struct bj_datagram *dgram)
Gets the sender's port from a datagram.
size_t bj_datagram_len(const struct bj_datagram *dgram)
Gets the data length from a datagram.
int bj_udp_send(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.
uint16_t bj_udp_local_port(const struct bj_udp *udp)
Gets the local port of a UDP socket.
#define BJ_ADDR_STRLEN
Maximum length of an address string (including null terminator).
Definition net.h:245
struct bj_datagram * bj_udp_recv(struct bj_udp *udp, struct bj_error **error)
Receives a datagram from the UDP socket.
void bj_close_udp(struct bj_udp *udp)
Closes a UDP socket.
const void * bj_datagram_data(const struct bj_datagram *dgram)
Gets the data pointer from a datagram.
@ BJ_RESOLVE_IPV4
Restrict address resolution to IPv4.
Definition net.h:118
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.