TcpClient

TCP client for connecting to a server

It

Platform-specific TCP client handle

#if defined(_WIN32) || defined(_WIN64)
struct It {
  SOCKET sockfd = INVALID_SOCKET
  HANDLE thread = NULL
}
#else
struct It {
  I32 sockfd = -1
  pthread_t thread = NULL
}
#endif

create

Create a TCP client and connect to a server

TcpClient create(I8* ip, I16 port)

Parameters

  • ip — Server IP address
  • port — Server port number

Returns — Connected client object


destroy

Destroy a TCP client and close the connection

U0 destroy(TcpClient client)

Parameters

  • client — The TCP client

write

Send data to the server

U8 write(TcpClient client, I8* buffer, U32 size)

Parameters

  • client — The TCP client
  • buffer — Data to send
  • size — Number of bytes to send

Returns — Non-zero on success


read

Read data from the server (non-blocking, may be partial)

U8 read(TcpClient client, I8* buffer, U32 size)

Parameters

  • client — The TCP client
  • buffer — Receive buffer
  • size — Maximum bytes to read

Returns — Non-zero on success


readAll

Read data from the server, blocking until all bytes arrive

U8 readAll(TcpClient client, I8* buffer, U32 size)

Parameters

  • client — The TCP client
  • buffer — Receive buffer
  • size — Exact number of bytes to read

Returns — Non-zero on success