Robot
Move a little robot.
|
#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"
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... | |
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.
x_coord | Robot's x coordinate. |
y_coord | Robot's y coordinate. |
x_max | Value max for x (so number of lines in the grid - 1). |
y_max | Value max for y (so number of columns in the grid - 1). |
content | Pointer to string structure containing response content. |
robot_grid | Pointer to string structure containing the robot grid. |
cookie | Pointer to string structure containing cookies. |
p_tem | p_tem Pointer to the structure containing all template data. |
void handle_client | ( | int | clientfd, |
struct sockaddr_in | client_addr, | ||
struct templates * | p_tem | ||
) |
Handles a given client.
clientfd | Client's file descriptor. |
client_addr | Client's address. |
p_tem | Pointer to the structure containing all template data. |
int main | ( | void | ) |
Creates server and accept clients' connexions.
See https://clembytes.fr/2025/07/17/robot-episode-1-create-a-server-in-c/ for details.
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).
clientfd | Client's file descriptor. |
p_buf | Pointer to beginning of the receiving buffer. |
p_data_len | Pointer to the size of already written data. |
p_buf_size | Pointer to the maximum size of the receiving buffer. |