cheese-cakee/Unix_networking

A collection of POSIX networking programs

★ 5Forks 0CGitHub ↗Compare

README

Unix Networking in C

A collection of POSIX networking programs I wrote following Beej's Guide to Network Programming.

Repo Architecture

.
|-- TCP/
|   |-- server.c                 # Forking TCP greeting server
|   `-- client.c                 # TCP client
|-- UDP/
|   |-- datagramserver.c         # IPv6 UDP listener
|   |-- datagramclient.c         # IPv6 UDP sender
|   `-- broadcaster.c            # IPv4 UDP broadcaster
|-- Multichat Server/
|   |-- multichatserver.c        # Multi-client chat with poll()
|   `-- selectmserver.c          # Multi-client chat with select() and /nick
|-- misc/
|   |-- findip.c                 # Hostname and address lookup
|   |-- pollexample.c            # Minimal poll() example
|   |-- partial_send_handling.c  # Reusable sendall() helper
|   `-- floatdoubleieee.c        # Floating-point packing demonstration
`-- docs/images/                 # Example screenshots

Build

You need a C compiler and standard POSIX networking headers. Netcat/Telnet is required for the chat examples.

sudo apt install build-essential netcat-openbsd

mkdir -p build
cc -Wall -Wextra TCP/server.c -o build/tcp-server
cc -Wall -Wextra TCP/client.c -o build/tcp-client
cc -Wall -Wextra UDP/datagramserver.c -o build/udp-server
cc -Wall -Wextra UDP/datagramclient.c -o build/udp-client
cc -Wall -Wextra UDP/broadcaster.c -o build/udp-broadcaster
cc -Wall -Wextra "Multichat Server/multichatserver.c" -o build/chat-poll
cc -Wall -Wextra "Multichat Server/selectmserver.c" -o build/chat-select
cc -Wall -Wextra misc/findip.c -o build/findip
cc -Wall -Wextra misc/pollexample.c -o build/poll-example
cc -Wall -Wextra misc/floatdoubleieee.c -o build/float-double-ieee
cc -Wall -Wextra -c misc/partial_send_handling.c -o build/partial-send-handling.o

Run

TCP

# Terminal 1
./build/tcp-server

# Terminal 2
./build/tcp-client localhost

TCP server accepting a localhost connection and the client receiving a response

UDP

# Terminal 1
./build/udp-server

# Terminal 2: unicast
./build/udp-client ::1 "hello over UDP"

# Terminal 2: broadcast
./build/udp-broadcaster 255.255.255.255 "hello everyone"

Broadcast delivery depends on the host network, firewall, and WSL configuration.

Multi-client chat

Run one server, then connect from two or more other terminals:

./build/chat-poll
# or
./build/chat-select

nc localhost 9034

Both servers use port 9034. The select() version also supports /nick <name>.

Select-based chat server with three connected clients, message broadcasting, and nickname support

Utilities

./build/findip example.com
./build/poll-example
./build/float-double-ieee

Contributors

cheese-cakee

Issues