A lightweight C program that demonstrates core network programming concepts by connecting to a local or remote web server, sending an HTTP GET request, and scanning the response for specific keywords (like "robot").
The program follows the standard TCP Client Lifecycle:
- Socket Creation: Initializes a socket using
AF_INET(IPv4) andSOCK_STREAM(TCP). - Configuration: Sets up the destination IP (e.g.,
127.0.0.1) and Port (e.g.,8000). - Connection: Performs a TCP three-way handshake with the server.
- Request: Sends a raw HTTP GET request string.
- Response: Captures the incoming data stream into a local buffer.
- Analysis: Uses
strstr()to scan the received HTML/text for the keyword "robot".
- A C compiler (like
gcc). - Python 3 (for running a local test server).
-
Start a Local Server In one terminal, navigate to the folder containing your files and start a Python web server:
python3 -m http.server 8000
-
Compile the C Code In a second terminal, compile the program:
gcc functions.c -o functions
-
Execute Run the binary:
./functions
The current version includes a Debug Block that prints the raw HTTP response headers and body. This is useful for verifying exactly what the server is sending before the logic scan occurs.
- Byte Ordering: Using
htons()to ensure the port number is "Network Byte Order" compliant. - Null Termination: Manually adding
\0to the end of therecvbuffer so C string functions don't read into "garbage" memory. - Blocking Calls: Understanding how
recv()pauses program execution until data arrives from the server.
Created as part of a deep dive into C Master networking logic.