Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
net_tcp_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
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 bj_info("client: connecting to 127.0.0.1:8080...");
28 struct bj_tcp_stream* stream = bj_connect_tcp("127.0.0.1", 8081, 0, 3000, &error);
29
30 if (!stream) {
31 bj_err("client: connect failed (%s)", bj_error_message(error));
32 bj_clear_error(&error);
33 return 1;
34 }
35 bj_info("client: connected to server");
36
37 const char* name = "Banjo";
38 bj_info("client: sending name: %s", name);
39 bj_tcp_send(stream, name, strlen(name));
40
41 char buf[512];
42 int n = bj_tcp_recv(stream, buf, sizeof(buf) - 1);
43 if (n > 0) {
44 buf[n] = '\0';
45 bj_info("client: server replied: %s", buf);
46 }
47
48 bj_close_tcp_stream(stream);
49 bj_info("client: connection closed");
50
51 bj_end();
52
53 return 0;
54}
int main(int argc, char *argv[])
Definition audio_pcm.c:177
struct bj_error bj_error
Definition api.h:333
struct bj_tcp_stream bj_tcp_stream
Definition api.h:357
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
int bj_tcp_recv(struct bj_tcp_stream *stream, void *buf, size_t len)
Receives data from a TCP stream.
void bj_close_tcp_stream(struct bj_tcp_stream *stream)
Closes a TCP stream 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_stream * bj_connect_tcp(const char *host, uint16_t port, uint16_t hint, uint32_t ms_timeout, struct bj_error **error)
Connects to a TCP server at the specified host and port.
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.
String utility functions.
Header file for system interactions.