Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
net_udp_client.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#include <string.h>
16
17#define SERVER_HOST "127.0.0.1"
18#define SERVER_PORT 9000
19
20int main(int argc, char* argv[]) {
21 (void)argc;
22 (void)argv;
23
24 if (!bj_begin(BJ_NETWORK_SYSTEM, 0)) {
25 return 1;
26 }
27
28 struct bj_error* error = 0;
29
30 struct bj_udp* udp = bj_open_udp(0, 0, BJ_RESOLVE_IPV4, &error);
31 if (!udp) {
32 bj_err("client: failed to open UDP socket (%s)", bj_error_message(error));
33 bj_clear_error(&error);
34 bj_end();
35 return 1;
36 }
37
38 bj_info("client: using local port %d", bj_udp_local_port(udp));
39
40 const char* message = "Hello from Banjo UDP!";
41 bj_info("client: sending \"%s\" to %s:%d", message, SERVER_HOST, SERVER_PORT);
42
43 int sent = bj_udp_send(udp, SERVER_HOST, SERVER_PORT, message, strlen(message), &error);
44 if (sent < 0) {
45 bj_err("client: send failed (%s)", bj_error_message(error));
46 bj_clear_error(&error);
47 bj_close_udp(udp);
48 bj_end();
49 return 1;
50 }
51 bj_info("client: sent %d bytes", sent);
52
53 bj_info("client: waiting for reply...");
54 if (!bj_udp_wait(udp, 3000)) {
55 bj_err("client: timeout waiting for reply");
56 bj_close_udp(udp);
57 bj_end();
58 return 1;
59 }
60
61 struct bj_datagram* dgram = bj_udp_recv(udp, &error);
62 if (!dgram) {
63 bj_err("client: recv failed (%s)", bj_error_message(error));
64 bj_clear_error(&error);
65 bj_close_udp(udp);
66 bj_end();
67 return 1;
68 }
69
70 char reply[512];
71 size_t len = bj_datagram_len(dgram);
72 if (len >= sizeof(reply)) {
73 len = sizeof(reply) - 1;
74 }
75 memcpy(reply, bj_datagram_data(dgram), len);
76 reply[len] = '\0';
77
78 bj_info("client: received reply: \"%s\"", reply);
79
80 bj_close_datagram(dgram);
81 bj_close_udp(udp);
82 bj_end();
83 return 0;
84}
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.