Robot
Move a little robot.
server.c File Reference
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <poll.h>
#include "lib_server.h"
Include dependency graph for server.c:

Functions

ssize_t read_client (int clientfd, char **p_buf, size_t *p_data_len, size_t *p_buf_size)
 Reading client's buffer at given position. More...
 
const char * generate_content (int x_coord, int y_coord, int x_max, int y_max, struct string *content, struct string *robot_grid, struct string *cookie, struct templates *p_tem)
 Generate HTTP response's content for given coordinates. More...
 
void handle_client (int clientfd, struct sockaddr_in client_addr, struct templates *p_tem)
 Handles a given client. More...
 
int main (void)
 Creates server and accept clients' connexions. More...
 

Function Documentation

◆ generate_content()

const char * generate_content ( int  x_coord,
int  y_coord,
int  x_max,
int  y_max,
struct string content,
struct string robot_grid,
struct string cookie,
struct templates p_tem 
)

Generate HTTP response's content for given coordinates.

Parameters
x_coordRobot's x coordinate.
y_coordRobot's y coordinate.
x_maxValue max for x (so number of lines in the grid - 1).
y_maxValue max for y (so number of columns in the grid - 1).
contentPointer to string structure containing response content.
robot_gridPointer to string structure containing the robot grid.
cookiePointer to string structure containing cookies.
p_temp_tem Pointer to the structure containing all template data.
Returns
Content-Type.

◆ handle_client()

void handle_client ( int  clientfd,
struct sockaddr_in  client_addr,
struct templates p_tem 
)

Handles a given client.

Parameters
clientfdClient's file descriptor.
client_addrClient's address.
p_temPointer to the structure containing all template data.

◆ main()

int main ( void  )

Creates server and accept clients' connexions.

  • Creates a socket and get its file descriptor.
  • Define IP address and port.
  • Attaches socket to the previously defined address and port.
  • Marks the socket as ready to receive entry connexions.
  • Reads all necessary templates files.
  • Get clients connexion address.
  • Calls handle_client().
  • Closes client's file descriptor.

See https://clembytes.fr/2025/07/17/robot-episode-1-create-a-server-in-c/ for details.

◆ read_client()

ssize_t read_client ( int  clientfd,
char **  p_buf,
size_t *  p_data_len,
size_t *  p_buf_size 
)

Reading client's buffer at given position.

Reading of client buffer into buf at position data_len (data_len is the size of already written data) for as many characters as possible (to fill the buffer buf or to read everything).

Performs realloc when receiving buffer is full (double size).

Parameters
clientfdClient's file descriptor.
p_bufPointer to beginning of the receiving buffer.
p_data_lenPointer to the size of already written data.
p_buf_sizePointer to the maximum size of the receiving buffer.
Returns
Integer: 0: client disconnected -1: fail n: read response, number of characters read