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

UDP client example.

UDP client example.Demonstrates: bj_open_udp, bj_udp_send, bj_udp_wait, bj_udp_recv. Pair with udp_server.c to test.

#include <banjo/log.h>
#include <banjo/main.h>
#include <banjo/net.h>
#include <banjo/system.h>
#include <string.h>
#define SERVER_HOST "127.0.0.1"
#define SERVER_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, BJ_RESOLVE_IPV4, &error);
if (!udp) {
bj_err("client: failed to open UDP socket (%s)", bj_error_message(error));
bj_clear_error(&error);
bj_end();
return 1;
}
bj_info("client: using local port %d", bj_udp_local_port(udp));
const char* message = "Hello from Banjo UDP!";
bj_info("client: sending \"%s\" to %s:%d", message, SERVER_HOST, SERVER_PORT);
int sent = bj_udp_send(udp, SERVER_HOST, SERVER_PORT, message, strlen(message), &error);
if (sent < 0) {
bj_err("client: send failed (%s)", bj_error_message(error));
bj_clear_error(&error);
bj_end();
return 1;
}
bj_info("client: sent %d bytes", sent);
bj_info("client: waiting for reply...");
if (!bj_udp_wait(udp, 3000)) {
bj_err("client: timeout waiting for reply");
bj_end();
return 1;
}
struct bj_datagram* dgram = bj_udp_recv(udp, &error);
if (!dgram) {
bj_err("client: recv failed (%s)", bj_error_message(error));
bj_clear_error(&error);
bj_end();
return 1;
}
char reply[512];
size_t len = bj_datagram_len(dgram);
if (len >= sizeof(reply)) {
len = sizeof(reply) - 1;
}
memcpy(reply, bj_datagram_data(dgram), len);
reply[len] = '\0';
bj_info("client: received reply: \"%s\"", reply);
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
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.
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.
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_bool bj_udp_wait(struct bj_udp *udp, uint32_t ms_timeout)
Waits for data to become available on a UDP socket.
@ 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 SERVER_HOST
#define SERVER_PORT
String utility functions.
Header file for system interactions.