This repository contains six exercises demonstrating network programming concepts using Java sockets, covering both TCP and UDP protocols, as well as packet manipulation.
- Java Development Kit (JDK) 8 or higher
- Basic understanding of network protocols (TCP/IP, UDP)
- Terminal/Command line access
tp3_java/
├── 01-tcp-datetime-server/ # TCP server that sends current date and time
├── 02-tcp-bidirectional-chat/ # Interactive TCP chat between client and server
├── 03-tcp-echo-server/ # TCP echo service implementation
├── 04-udp-chat-application/ # Connectionless UDP chat application
├── 05-udp-dns-resolver/ # DNS name resolution simulation using UDP
└── 06-raw-packet-capture/ # Packet creation and analysis with checksum verification
Description: A TCP server that sends the current system date and time to connecting clients. The server also receives and displays the client's IP address.
How to Execute:
Terminal 1 (Server):
cd 01-tcp-datetime-server
javac ServeurTCP.java ClientTCP.java
java ServeurTCPTerminal 2 (Client):
cd 01-tcp-datetime-server
java ClientTCPExpected Behavior:
- Server displays connection details and client IP
- Client receives and displays current date/time from server
Description: Implements a continuous bidirectional communication channel where the server and client can exchange messages interactively.
How to Execute:
Terminal 1 (Server):
cd 02-tcp-bidirectional-chat
javac ServeurTCP.java ClientTCP.java
java ServeurTCPTerminal 2 (Client):
cd 02-tcp-bidirectional-chat
java ClientTCPUsage:
- Type messages in either terminal to send
- Type
endto terminate the conversation - Server and client take turns sending messages
Description: A TCP echo service that returns exactly what it receives from the client, demonstrating basic request-response patterns.
How to Execute:
Terminal 1 (Server):
cd 03-tcp-echo-server
javac ServeurTCP.java ClientTCP.java
java ServeurTCPTerminal 2 (Client):
cd 03-tcp-echo-server
java ClientTCPUsage:
- Type any message in the client terminal
- Server echoes back the exact message
- Type
byeornullto terminate
Description: Demonstrates connectionless communication using UDP datagrams for a chat application between server and client.
How to Execute:
Terminal 1 (Server):
cd 04-udp-chat-application
javac ServeurUDP.java ClientUDP.java
java ServeurUDPTerminal 2 (Client):
cd 04-udp-chat-application
java ClientUDPUsage:
- Client sends first message
- Server responds to each message
- Type
byeorendto terminate - Uses ports 9876 (client send) and 9877 (client receive)
Description: Simulates a Domain Name System (DNS) resolver that translates domain names to IP addresses using UDP protocol.
How to Execute:
Terminal 1 (DNS Server):
cd 05-udp-dns-resolver
javac ServeurDNS.java ClientDNS.java
java ServeurDNSTerminal 2 (DNS Client):
cd 05-udp-dns-resolver
java ClientDNSPre-configured Domains:
- www.google.com
- www.facebook.com
- www.youtube.com
- www.github.com
- localhost
- And 5 more domains
Usage:
- Enter domain name to resolve
- Server returns corresponding IP address
- Type
quitorexitto close client - Server continues running until manually stopped
Description: Demonstrates packet creation, parsing, and integrity verification using checksums. Simulates IP and TCP header construction and analysis.
How to Execute:
cd 06-raw-packet-capture
javac PacketCapture.java
java PacketCaptureInteractive Prompts:
- Enter source IP address (e.g., 192.168.1.10)
- Enter source port (e.g., 8080)
- Enter destination IP address (e.g., 192.168.1.20)
- Enter destination port (e.g., 80)
Features:
- Creates TCP/IP packets with proper headers
- Calculates IP and TCP checksums (RFC 1071)
- Displays packet contents in hexadecimal
- Verifies packet integrity
- Demonstrates error detection with corrupted packet
Note: This is a simulation. Java does not allow direct raw socket access for security reasons.
- TCP (Exercises 1-3): Connection-oriented, reliable, ordered delivery
- UDP (Exercises 4-5): Connectionless, faster, no guaranteed delivery
- Client-Server architecture
- Request-Response model
- Bidirectional communication
- Packet-level manipulation
- ServerSocket and Socket (TCP)
- DatagramSocket and DatagramPacket (UDP)
- Input/Output streams
- Buffer management
- Checksum calculation and verification
- Connection error handling
- Input validation
- Resource cleanup (finally blocks)
Compile all files in a directory:
javac *.javaRun with explicit classpath:
java -cp . ClassNameStop a running server:
Press Ctrl+C in the server terminal
Connection refused:
- Ensure the server is running before starting the client
- Check that port numbers match between server and client
- Verify no firewall is blocking the ports
Class not found:
- Make sure files are compiled before running
- Check you're in the correct directory
- Verify class names match file names
Port already in use:
- Another instance may be running
- Change the port number in both server and client
- Wait a few seconds for the OS to release the port
- 12345: TCP DateTime Server (Exercise 1)
- 12346: TCP Chat (Exercise 2)
- 12347: TCP Echo Server (Exercise 3)
- 9876/9877: UDP Chat (Exercise 4)
- 9999: UDP DNS Server (Exercise 5)
- N/A: Raw Packet Capture (Exercise 6)
- All exercises use localhost by default
- To test across network, change SERVER_ADDRESS to target machine IP
- Servers handle one client at a time (no multithreading)
- UDP exercises demonstrate unreliable nature of connectionless protocols
- Raw packet exercise is educational and does not send packets on network
Younes Bensafia
Educational purposes - Network Programming with Java