A minimal implementation of a TCP/IP protocol using socket programming in C to create a message transmission application.
It's surreal that, if two machines are connected over same LAN (mobile hotspot/wifi), a communication can be established between the two machines using sockets without the need of connecting the machines physically with any wire.
- Uses IPv4 and TCP sockets
- Server listens for connections on port 2000
- Client connects to the server and sends messages
- Supports bidirectional message transmission
Ensure you have the following installed on your system:
- GCC compiler
- A Linux or macOS system (Windows users can use WSL or MinGW)
$ git clone <repository_url>
$ cd <repository_name>You can compile manually using gcc:
$ gcc -o server server.c
$ gcc -o client client.cStart the server first to listen for incoming connections:
$ ./serverThe server will bind to port 2000 and wait for connections.
Open another terminal and run the client to connect to the server:
$ ./clientYou should see a message indicating a successful connection.
If you want to run the client on a different machine:
- Find the server machine's IP address:
Look for the appropriate network interface (e.g.,
$ ifconfig a | grep inet192.168.x.xon a Wi-Fi network). - Modify the
client.cfile to replace127.0.0.1with the server's IP address:char* ip = "192.168.x.x"; // Replace with the server's IP
- Recompile the client:
$ gcc -o client client.c
- Run the client on the second machine:
$ ./client
- Ensure both machines are connected to the same network.
Once the client is connected, you can start sending messages from the client terminal. The server will receive and print them.
To exit, type exit and press Enter in the client terminal.
Once communication is done, stop the server with Ctrl + C.
- Ensure both server and client are running on the same network.
- If the server is running on another machine, update the client's IP address accordingly in
client.c. - Check if server is bound on port
2000:$ lsof -i :2000
- If you get permission issues, try running the server with:
$ sudo ./server