Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
net_udp_server.c
Go to the documentation of this file.
1
9
10#include <banjo/log.h>
11#include <banjo/main.h>
12#include <banjo/net.h>
13#include <banjo/system.h>
14
15#define PORT 9000
16
17int main(int argc, char* argv[]) {
18 (void)argc;
19 (void)argv;
20
21 if (!bj_begin(BJ_NETWORK_SYSTEM, 0)) {
22 return 1;
23 }
24
25 struct bj_error* error = 0;
26
27 struct bj_udp* udp = bj_open_udp("0.0.0.0", PORT, BJ_RESOLVE_IPV4, &error);
28 if (!udp) {
29 bj_err("server: failed to open UDP socket (%s)", bj_error_message(error));
30 bj_clear_error(&error);
31 bj_end();
32 return 1;
33 }
34
35 bj_info("server: listening on port %d", bj_udp_local_port(udp));
36 bj_info("server: waiting for datagrams...");
37
38 while (1) {
39 struct bj_datagram* dgram = bj_udp_recv(udp, &error);
40 if (!dgram) {
41 if (error) {
42 bj_err("server: recv error (%s)", bj_error_message(error));
43 bj_clear_error(&error);
44 }
45 continue;
46 }
47
48 char addr[BJ_ADDR_STRLEN];
49 bj_datagram_addr(dgram, addr, sizeof(addr));
50 uint16_t sender_port = bj_datagram_port(dgram);
51
52 bj_info("server: received %zu bytes from %s:%d",
53 bj_datagram_len(dgram), addr, sender_port);
54
55 // Echo the data back
56 int sent = bj_udp_send(udp, addr, sender_port,
57 bj_datagram_data(dgram), bj_datagram_len(dgram), &error);
58 if (sent < 0) {
59 bj_err("server: send error (%s)", bj_error_message(error));
60 bj_clear_error(&error);
61 } else {
62 bj_info("server: echoed %d bytes back", sent);
63 }
64
65 bj_close_datagram(dgram);
66 }
67
68 bj_close_udp(udp);
69 bj_end();
70 return 0;
71}
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.