Skip to main content

Executables

server

The server executable starts a process that receives messages via UNIX signals.

Usage

./server

Behavior

  • Displays its PID on startup for clients to use
  • Registers signal handlers for SIGUSR1 and SIGUSR2
  • Receives binary data bit-by-bit through signals
  • Reconstructs and prints characters as they are received
  • Runs indefinitely until terminated

Signals Handled

SIGUSR1
signal
Represents a binary 0 bit. The server shifts the current character left and adds 0.
SIGUSR2
signal
Represents a binary 1 bit. The server shifts the current character left and adds 1.

Example Output

SUCCESS!, Server is ready :D! The PID: 12345
Waiting fot the string...

client

The client executable sends a string message to a running server process.

Usage

./client <PID> <string to send>

Arguments

PID
integer
required
The process ID of the server. Must be a valid numeric PID.
string to send
string
required
The message to transmit to the server. Can contain any characters.

Exit Codes

0
success
Message sent successfully
1
error
Error occurred - invalid arguments, wrong PID format, or signal sending failed

Example

./client 12345 "Hello, World!"

Core Functions

send_message

Sends a string message to a process by transmitting each character bit-by-bit using UNIX signals.
int send_message(unsigned int pid, unsigned char *str)

Parameters

pid
unsigned int
required
The process ID of the target server process
str
unsigned char*
required
Pointer to the null-terminated string to send

Return Value

0
int
Success - all signals were sent successfully
1
int
Failure - kill() returned -1, indicating signal sending failed

Behavior

  • Iterates through each character in the string
  • For each character, sends 8 bits from most significant to least significant
  • Sends SIGUSR2 for bit value 1, SIGUSR1 for bit value 0
  • Uses 700 microsecond delay between signals (via usleep(700))
  • Returns immediately on first kill() error

Implementation Example

int	send_message(unsigned int pid, unsigned char *str)
{
	int	bits;
	int	i;

	i = 0;
	while (str[i])
	{
		bits = 8;
		while (--bits >= 0)
		{
			if ((str[i] >> bits) & 1)
			{
				if (kill(pid, SIGUSR2) == -1)
					return (1);
			}
			else
			{
				if (kill(pid, SIGUSR1) == -1)
					return (1);
			}
			usleep(700);
		}
		i++;
	}
	return (0);
}

handle_sigusr

Signal handler that receives individual bits and reconstructs characters.
void handle_sigusr(int sigsent)

Parameters

sigsent
int
required
The signal number received (SIGUSR1 or SIGUSR2)

Behavior

  • Maintains static variables to track current character (c) and bit count (i)
  • On SIGUSR1: shifts current character left (adds 0 bit)
  • On SIGUSR2: shifts current character left and sets LSB to 1 (adds 1 bit)
  • After receiving 8 bits, prints the complete character using ft_printf
  • Resets counters after printing each character

Implementation Example

void	handle_sigusr(int sigsent)
{
	static size_t	c = 0;
	static size_t	i = 0;

	if (sigsent == SIGUSR1)
		c = c << 1;
	else if (sigsent == SIGUSR2)
		c = (c << 1) | 1;
	i++;
	if (i == 8)
	{
		ft_printf("%c", c);
		i = 0;
		c = 0;
	}
}

ft_atoi

Converts a string to an integer.
int ft_atoi(const char *str)

Parameters

str
const char*
required
The string to convert. May contain leading whitespace and an optional +/- sign.

Return Value

integer
int
The converted integer value, with sign applied if present

Behavior

  • Skips leading whitespace (spaces and characters 9-13: tab, newline, etc.)
  • Handles optional ’+’ or ’-’ sign
  • Converts consecutive digits to integer
  • Stops at first non-digit character
  • Returns signed result

Implementation Example

int	ft_atoi(const char *str)
{
	int	i;
	int	sign;
	int	num;

	i = 0;
	sign = 1;
	num = 0;
	while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13))
		i++;
	if (str[i] == '+' || str[i] == '-')
	{
		if (str[i] == '-')
			sign = -1;
		i++;
	}
	while (str[i] != '\0' && str[i] >= '0' && str[i] <= '9')
	{
		num = (num * 10) + (str[i] - '0');
		i++;
	}
	return (sign * num);
}

ft_printf

Formatted output function similar to standard printf.
int ft_printf(char const *str, ...)

Parameters

str
const char*
required
Format string with conversion specifiers
...
variadic
required
Variable arguments corresponding to format specifiers

Return Value

count
int
Number of characters printed

Supported Format Specifiers

  • %c - Character
  • %s - String
  • %d / %i - Signed decimal integer
  • %u - Unsigned decimal integer
  • %x - Hexadecimal lowercase
  • %X - Hexadecimal uppercase
  • %p - Pointer address

Usage Examples

// Print server PID
ft_printf("SUCCESS!, Server is ready :D! The PID: %d\n", getpid());

// Print character
ft_printf("%c", c);

// Print error messages
ft_printf("Wrong PID\n");
ft_printf("Client failed sending signal\n");
ft_printf("Usage : ./client <PID> <string to send>\n");

Build docs developers (and LLMs) love