This program implements a basic TCP echo server that listens on a specified port and echoes bback any messages received from clients. The server will continue running until it receives the "NAPOZZ" command from a client.
./server <PORT>
PORT
: The port number to listen on (required)-h
: Display help message
./server 8080
- Single client connection handling
- Echo functionality (sends back received messages)
- Clean shutdown with "NAPOZZ" command
- Support for IPv4 connections
- Buffer size of 80 bytes
This project consists of three C programs that demonstrate basic network communication:
- MyHost - DNS lookup tool
- Server - TCP server implementation
- Client - TCP client implementation
Compile each program using gcc:
gcc -o myhost myhost.c
gcc -o server server.c
gcc -o client client.c
A simple DNS lookup tool that resolves hostnames to IP addresses.
./myhost <hostname>
Example:
./myhost google.fr
Options:
-h
: Display help message
A TCP server that listens for incoming connections and echoes back received messages.
./server <PORT>
Example:
./server 8080
The server will:
- Listen on the specified port
- Accept one client connection
- Echo back any received messages
- Close when receiving "NAPOZZ" command
A TCP client that can connect to the server and send messages.
./client <URL:PORT>
Example:
./client localhost:8080
The client will:
- Connect to the specified server
- Allow you to type messages to send
- Display server responses
- Close connection when sending "NAPOZZ"
- Communication uses TCP sockets
- Messages are text-based
- Special command "NAPOZZ" terminates the connection
- Server echoes back all received messages
All programs include basic error handling for:
- Invalid arguments
- Connection failures
- Network errors
- Invalid hostnames
This program resolves hostnames to IP addresses, supporting both IPv4 and IPv6.
stdio.h
: Standard I/O operationsstring.h
: String manipulation functionsnetdb.h
: Network database operations (DNS lookups)arpa/inet.h
: Internet operations (IP 8000 address conversions)
#include <stdio.h> // printf(), etc
#include <string.h> // strcmp(), etc
#include <netdb.h> // getaddrinfo()
#include <arpa/inet.h> // inet_ntop()
help()
: Displays usage instructions with ASCII artmain()
: Entry point that:- Accepts hostname as command line argument
- Handles -h flag for help
- Performs DNS lookup (implementation not shown in visible code)
# Get help
./myhost -h
# Lookup hostname
./myhost google.fr
- Takes a hostname as input
- Uses DNS resolution to find IP addresses
- Displays both IPv4 and IPv6 addresses if available
- Shows ASCII art error message if no argument provided
Note: The visible code section doesn't show the actual DNS resolution implementation, which would typically use getaddrinfo()
from the netdb.h
library.
- Create TCP socket client
- Parse server address and port from args
- Connect to server
- Enter command loop
- Send/receive data until "NAPOZZ" command
- Clean up and exit
#include <arpa/inet.h> // inet_addr()
#include <stdio.h> // printf(), scanf()
#include <string.h> // strcmp()
#include <sys/socket.h> // socket(), connect()
#include <unistd.h> // read(), write(), close()
./client <IP_ADDRESS:PORT>
# Example
./client 127.0.0.1:8080
- Connects to TCP server
- Interactive command input
- Receives and displays server responses
- Buffer size: 80 bytes for commands, 255 bytes for responses
- Special command "NAPOZZ" exits program
- Shows connection status
- Error handling for connection failures
- Text-based command/response
- Commands limited to 80 bytes
- Server responses limited to 255 bytes
- "NAPOZZ" is reserved shutdown command
- Invalid arguments
- Connection failures
- Socket creation errors
The program implements a basic TCP client that connects to a server, sends commands, and displays responses until terminated with "NAPOZZ".
- Parse command line args for port
- Create TCP socket
- Bind to specified port
- Listen for connections
- Accept client connection
- Echo loop until "NAPOZZ"
- Clean shutdown
#include <arpa/inet.h> // Socket functions
#include <sys/socket.h> // Socket API
#include <string.h> // String operations
#include <unistd.h> // System calls
- Socket binding: Port
- Connection handling: Single client
- Buffer size: 80 bytes
- Protocol: Text-based echo
- Termination: "NAPOZZ" command
# Compile
gcc -o server server.c
# Run (example on port 8080)
./server 8080
- Create socket (
socket()
) - Bind to port (
bind()
) - Listen (
listen()
) - Accept connection (
accept()
) - Echo loop (
recv()
/send()
) - Close (
close()
)
- Socket creation fails
- Bind fails
- Listen fails
- Accept fails
- Send/Receive fails
The server implements a basic TCP echo service that reflects back client messages until receiving "NAPOZZ".