21int main(
int argc,
char* argv[]) {
40 bj_info(
"server: polling for connections...");
52 clients[i] = new_client;
57 bj_info(
"server: client %d connected from %s", i, addr);
76 bj_info(
"server: client %d disconnected", i);
85 int n =
bj_tcp_recv(clients[i], buf,
sizeof(buf) - 1);
88 bj_info(
"server: client %d sent: \"%s\"", i, buf);
91 int len = snprintf(response,
sizeof(response),
"[echo] %s", buf);
int main(int argc, char *argv[])
struct bj_tcp_listener bj_tcp_listener
struct bj_tcp_stream bj_tcp_stream
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.
bj_error_code
A numeric representation of an error in Banjo.
@ BJ_ERROR_NETWORK_TIMEOUT
Operation timed out.
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
uint16_t bj_tcp_listener_port(const struct bj_tcp_listener *listener)
Get the listening port of the given server.
int bj_tcp_recv(struct bj_tcp_stream *stream, void *buf, size_t len)
Receives data from a TCP stream.
size_t bj_tcp_peer_address(const struct bj_tcp_stream *stream, char *buf, size_t size)
Gets the remote address of a TCP stream.
void bj_close_tcp_stream(struct bj_tcp_stream *stream)
Closes a TCP stream and releases associated resources.
int bj_tcp_available(struct bj_tcp_stream *stream)
Checks bytes available to read without blocking.
struct bj_tcp_stream * bj_accept_tcp(struct bj_tcp_listener *listener, uint32_t ms_timeout, struct bj_error **error)
Accepts an incoming connection on a TCP listener with timeout.
void bj_close_tcp_listener(struct bj_tcp_listener *listener)
Closes a TCP listener and releases associated resources.
int bj_tcp_send(struct bj_tcp_stream *stream, const void *buf, size_t len)
Sends data over a TCP stream.
struct bj_tcp_listener * bj_listen_tcp(const char *host, uint16_t port, uint16_t hint, int backlog, struct bj_error **error)
Creates a TCP listener bound to the specified host and port.
#define BJ_ADDR_STRLEN
Maximum length of an address string (including null terminator).
@ 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.
void bj_sleep(int milliseconds)
Suspends the current thread for a specified duration.
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for network API.
Header file for system interactions.
Header file for time manipulation utilities.