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.
#define PORT 9000
int main(
int argc,
char* argv[]) {
(void)argc;
(void)argv;
return 1;
}
if (!udp) {
return 1;
}
bj_info(
"server: waiting for datagrams...");
while (1) {
if (!dgram) {
if (error) {
}
continue;
}
bj_info(
"server: received %zu bytes from %s:%d",
if (sent < 0) {
} else {
bj_info(
"server: echoed %d bytes back", sent);
}
}
return 0;
}
int main(int argc, char *argv[])
struct bj_datagram bj_datagram
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.
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
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).
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.
bj_bool bj_begin(int systems, struct bj_error **error)
Initialises the system.
void bj_end(void)
De-initialises the system.
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for network API.
Header file for system interactions.