TCP Server

The tcp_server class encapsulates the server core which provides methods to create, initialize and start a TCP pre-forked server.

Constructor

tcp_server_t *tcp_server_new()
[source]

Create new tcp_server object.

Returns

On success new tcp_server object, or NULL if the new tcp server could not be created.

Destructor

void tcp_server_destroy(tcp_server_t **self_p)
[source]

Destroy tcp_server object. You must use this for any tcp server created via the tcp_server_new method.

Parameters
  • self_p (tcp_server_t**) – pointer to tcp_server_t object reference, so the destructor can nullify it

Returns

None (void)

TCP Server methods

int tcp_server_init(tcp_server_t *self_p, const char *server_iface, int server_port, voidVoid_ptr_t callback, int workers_n)
[source]

Initialize properties of a tcp_server object.

Parameters
  • self_p (tcp_server_t*) – reference to a tcp_server_t object

  • server_iface (const char*) – name of the network interface to which the server will bind.

  • server_port (int) – listening port of the server

  • callback (voidVoid_ptr_t) – connection callback

  • workers_n (int) – number of workers to dispatch

Returns

On 0 on success, -1 on failiure.

Note

The connection callback function should be provided by the user application. The callback should be a function pointer of type void (*callbcak) (void *args)

void tcp_server_run(tcp_server_t *self_p)
[source]

Server main processing loop.

Parameters
  • self_p (tcp_server_t*) – reference to a tcp_server_t object

Returns

None (void)