A collection of C++ examples demonstrating various network socket programming techniques. These examples cover fundamental networking concepts and provide a practical introduction to socket programming in C++.
This repository contains implementation examples for:
- TCP sender/receiver communication
- ICMP (ping) implementation
- UDP broadcast messaging
- Multicast group communication
- 01-receiver.cpp: TCP receiver implementation that accepts sender connections and handles communication
- 01-sender.cpp: TCP sender implementation that connects to a TCP receiver and exchanges messages
- 02-icmp.cpp: Raw socket implementation for ICMP echo request/reply (similar to ping utility)
- 03-broadcast.cpp: UDP broadcast sender that transmits messages to all hosts on a local network
- 03-receiver.cpp: Broadcast receiver that listens for messages sent to the broadcast address
- 04-multicast.cpp: Multicast sender that transmits messages to a specific multicast group
- 04-receiver.cpp: Multicast receiver that joins a multicast group and receives messages
This project uses CMake for building. Follow these steps to compile all examples:
mkdir build && cd build
cmake ..
make
This will build all the executables in the build directory:
- 01-receiver
- 01-sender
- 02-icmp
- 03-broadcast
- 03-receiver
- 04-multicast
- 04-receiver
- Start the receiver in one terminal:
./01-receiver
- Connect with the sender in another terminal:
./01-sender
- Type messages in the sender terminal to send to the receiver.
Requires root/sudo privileges to create raw sockets:
sudo ./02-icmp
- Start the receiver in one terminal:
./03-receiver
- Send broadcast messages in another terminal:
./03-broadcast
- Start the multicast receiver:
./04-receiver
- Send multicast messages:
./04-multicast
- SOCK_STREAM: Connection-oriented, reliable (TCP)
- SOCK_DGRAM: Connectionless, unreliable (UDP)
- Raw Sockets: Low-level network protocol access (ICMP)
- Unicast: One-to-one (TCP)
- Broadcast: One-to-all in a network (UDP broadcast)
- Multicast: One-to-many specific subscribed receivers (UDP multicast)
-
Multicast Configuration:
- Set
IP_MULTICAST_LOOP
to 1 when running sender and receiver on the same machine
- Set
-
ICMP Implementation:
- Requires root/administrator privileges
This code is provided for educational purposes only. No warranties or guarantees of any kind are provided. Use at your own risk.
- MIT License
- You are free to use, modify, and distribute this code for personal, educational, or commercial purposes.
- Attribution is appreciated but not required.
- If you find this code useful in your projects or publications, you may reference it as:
Socket Programming Examples from https://github.com/anecjong/Network-Socket-Programming-Example-CPP
When monitoring multicast traffic with tcpdump, you might see checksum warnings like this:
$ sudo tcpdump -i any host 238.238.238.238 and port 55556 -vv
192.xxx.xxx.xxx.55555 > 238.238.238.238.55556: [bad udp cksum 0xa003 -> 0x4580!] UDP, length 2
Don't worry about these "bad udp cksum" warnings. They occur because tcpdump captures packets before the NIC hardware calculates the checksum. On modern Linux systems, checksum calculations are offloaded to the network interface hardware:
$ ethtool -k lo
Features for lo:
rx-checksumming: on [fixed]
tx-checksumming: on
tx-checksum-ipv4: off [fixed]
tx-checksum-ip-generic: on [fixed]
tx-checksum-ipv6: off [fixed]
tx-checksum-fcoe-crc: off [fixed]
tx-checksum-sctp: on [fixed]
The packets are actually valid when they're transmitted - tcpdump just sees them before the checksums are calculated.