Skip to content

anecjong/Network-Socket-Programming-Example-CPP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Socket Programming Test Codes

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++.

Overview

This repository contains implementation examples for:

  • TCP sender/receiver communication
  • ICMP (ping) implementation
  • UDP broadcast messaging
  • Multicast group communication

Examples

TCP Communication (01-*.cpp)

  • 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

ICMP Implementation (02-*.cpp)

  • 02-icmp.cpp: Raw socket implementation for ICMP echo request/reply (similar to ping utility)

Broadcast Communication (03-*.cpp)

  • 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

Multicast Communication (04-*.cpp)

  • 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

Compilation

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

Usage Examples

TCP Sender/Receiver

  1. Start the receiver in one terminal:
./01-receiver
  1. Connect with the sender in another terminal:
./01-sender
  1. Type messages in the sender terminal to send to the receiver.

ICMP (Ping)

Requires root/sudo privileges to create raw sockets:

sudo ./02-icmp

UDP Broadcast

  1. Start the receiver in one terminal:
./03-receiver
  1. Send broadcast messages in another terminal:
./03-broadcast

Multicast

  1. Start the multicast receiver:
./04-receiver
  1. Send multicast messages:
./04-multicast

Key Networking Concepts

Socket Types

  • SOCK_STREAM: Connection-oriented, reliable (TCP)
  • SOCK_DGRAM: Connectionless, unreliable (UDP)
  • Raw Sockets: Low-level network protocol access (ICMP)

Communication Patterns

  • Unicast: One-to-one (TCP)
  • Broadcast: One-to-all in a network (UDP broadcast)
  • Multicast: One-to-many specific subscribed receivers (UDP multicast)

Important Notes

  1. Multicast Configuration:

    • Set IP_MULTICAST_LOOP to 1 when running sender and receiver on the same machine
  2. ICMP Implementation:

    • Requires root/administrator privileges

License

This code is provided for educational purposes only. No warranties or guarantees of any kind are provided. Use at your own risk.

Usage Rights

  • 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
    

Using tcpdump

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.

About

C++ Network Socket Programming Example Codes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published