c-template
socket_server.h File Reference

TCP socket servers, clients, and tooling for working with sockets. More...

#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/time.h>
#include "socket.h"
#include "../../utils/logger.h"
#include "../../sync/wait_group.h"
Include dependency graph for socket_server.h:

Go to the source code of this file.

Data Structures

struct  socket_server
 a generic tcp socket server using file descriptor socket_number it uses a dedicated pthread for accepting new client connections, with each connection being processed in another pthread in detached state. socket uses SO_REUSEADDR More...
 
struct  client_conn
 a structure containing a file descriptor and address information More...
 
struct  conn_handle_data
 struct containing arguments passed into pthread More...
 

Typedefs

typedef struct socket_server socket_server
 
typedef struct client_conn client_conn
 
typedef struct conn_handle_data conn_handle_data
 

Functions

socket_servernew_socket_server (addr_info hints, thread_logger *thl, int max_conns, char *port)
 returns a new socket server bound to the port number and ready to accept connections More...
 
void * async_listen_func (void *data)
 listens for new connections and spawns a thread to process the connection thread that is created to process the connection runs as a detached thread will poll for new connections to accept every 500 miliseconds More...
 
void * async_handle_conn_func (void *data)
 handles connections in a dedicated pthread
is laucnched in a pthread by async_listen_func when any new connection is received More...
 
client_connaccept_client_conn (socket_server *srv)
 helper function for accepting client connections times out new attempts if they take 3 seconds or more More...
 

Detailed Description

TCP socket servers, clients, and tooling for working with sockets.

Author
Bonedaddy

uses wait_group.h to provide lightweight synchronization between pthreads

Warning
before use you must call setup_signal_handling() so that all threads get properly cleaned up on exit
Note
you will want to adjust async_handle_conn_func to suit your needs as right now it is just an echo client it is likely you will need to have #define _POSIX_C_SOURCE 201112L see the following for more information

Definition in file socket_server.h.

Typedef Documentation

◆ client_conn

◆ conn_handle_data

◆ socket_server

Function Documentation

◆ accept_client_conn()

client_conn* accept_client_conn ( socket_server srv)

helper function for accepting client connections times out new attempts if they take 3 seconds or more

Returns
Failure: NULL client conn failed
Success: non-NULL populated client_conn object

◆ async_handle_conn_func()

void* async_handle_conn_func ( void *  data)

handles connections in a dedicated pthread
is laucnched in a pthread by async_listen_func when any new connection is received

Parameters
datavoid * to a conn_handle_data object
Note
uses select to determine if we can read data from the connection
select runs for 3 seconds before timing out and releasing resources with the connection
Warning
currently implements an example echo client
you will want to adapt to your specific use case

◆ async_listen_func()

void* async_listen_func ( void *  data)

listens for new connections and spawns a thread to process the connection thread that is created to process the connection runs as a detached thread will poll for new connections to accept every 500 miliseconds

Parameters
datavoid pointer to a socket_server struct
Note
detached thread created calling async_handle_conn_func
Warning
may change the 500 milisecond sleep

◆ new_socket_server()

socket_server* new_socket_server ( addr_info  hints,
thread_logger thl,
int  max_conns,
char *  port 
)

returns a new socket server bound to the port number and ready to accept connections