Skip to main content

Starting the Server

After building the project, start the server with:
./server

Server Startup Output

When the server starts successfully, you’ll see:
SUCCESS!, Server is ready :D! The PID: 12345
Waiting fot the string...
The PID (Process ID) will be different each time you start the server. In this example, the PID is 12345.

How the Server Works

1

Server prints its PID

On startup, the server displays its process ID:
ft_printf("SUCCESS!, Server is ready :D! The PID: %d\n", getpid());
You need this PID to send messages from the client.
2

Server registers signal handlers

The server sets up handlers for two UNIX signals:
signal(SIGUSR1, handle_sigusr);
signal(SIGUSR2, handle_sigusr);
  • SIGUSR1 represents a 0 bit
  • SIGUSR2 represents a 1 bit
3

Server waits for signals

The server enters an infinite loop, pausing until it receives signals:
while (1)
{
    pause();
}
The pause() function suspends execution until a signal is received.
4

Server processes incoming bits

When signals arrive, the handle_sigusr function:
  1. Receives each bit (SIGUSR1 = 0, SIGUSR2 = 1)
  2. Shifts and builds a character bit by bit
  3. After receiving 8 bits, prints the complete character
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;
}

Getting the Server PID

The server displays its PID in the startup message. You can also find it using system tools:
pgrep server

Example Session

$ ./server
SUCCESS!, Server is ready :D! The PID: 54321
Waiting fot the string...
Hello!
The server prints received messages directly to stdout as they arrive.

Server Behavior

Infinite Loop

The server runs continuously until explicitly stopped. It uses pause() to wait efficiently without consuming CPU.

Signal-Driven

All communication is handled through UNIX signals (SIGUSR1 and SIGUSR2), making it a pure signal-based IPC mechanism.

Character-by-Character

Messages are received and printed one character at a time as each set of 8 bits arrives.

No Buffering

Characters are printed immediately using ft_printf("%c", c) without waiting for a complete message.

Stopping the Server

To stop the server, use keyboard interrupt:
Ctrl+C
This sends a SIGINT signal that terminates the process.
The server doesn’t handle SIGINT specially, so it will terminate immediately without cleanup. Any partially received message (less than 8 bits) will be lost.

Alternative: Kill Command

You can also stop the server using the kill command:
kill <PID>
For example, if the server PID is 54321:
kill 54321
Do not use kill -9 (SIGKILL) unless necessary, as it forces immediate termination without allowing any cleanup.

Server Limitations

The server prints characters as they arrive without indicating where one message ends and another begins. If you send multiple messages, they’ll appear concatenated.
The server doesn’t send acknowledgment signals back to the client, so the client can’t verify successful receipt.
The server processes one signal at a time. If multiple clients send signals simultaneously, they may interfere with each other.
The server prints each character immediately. Null bytes in messages will not be visible but will reset the character buffer.

Troubleshooting

Ensure the executable has been built:
make
And that it has execute permissions:
ls -l server
chmod +x server
The PID is printed in the first line of output:
SUCCESS!, Server is ready :D! The PID: 12345
                                       ^^^^^
If you missed it, check with pgrep server or restart the server.
Verify:
  1. The server is still running (not killed)
  2. The client is using the correct PID
  3. The client successfully sent the message without errors

Build docs developers (and LLMs) love