Skip to content

This repository is demonstrating network programming concepts using Java sockets, covering both TCP and UDP protocols, as well as packet manipulation.

Notifications You must be signed in to change notification settings

YounesBensafia/java-socket-programming

Repository files navigation

Network Programming with Java Sockets

This repository contains six exercises demonstrating network programming concepts using Java sockets, covering both TCP and UDP protocols, as well as packet manipulation.

Prerequisites

  • Java Development Kit (JDK) 8 or higher
  • Basic understanding of network protocols (TCP/IP, UDP)
  • Terminal/Command line access

Project Structure

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

Exercises Overview

01 - TCP DateTime Server

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 ServeurTCP

Terminal 2 (Client):

cd 01-tcp-datetime-server
java ClientTCP

Expected Behavior:

  • Server displays connection details and client IP
  • Client receives and displays current date/time from server

02 - TCP Bidirectional Chat

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 ServeurTCP

Terminal 2 (Client):

cd 02-tcp-bidirectional-chat
java ClientTCP

Usage:

  • Type messages in either terminal to send
  • Type end to terminate the conversation
  • Server and client take turns sending messages

03 - TCP Echo Server

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 ServeurTCP

Terminal 2 (Client):

cd 03-tcp-echo-server
java ClientTCP

Usage:

  • Type any message in the client terminal
  • Server echoes back the exact message
  • Type bye or null to terminate

04 - UDP Chat Application

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 ServeurUDP

Terminal 2 (Client):

cd 04-udp-chat-application
java ClientUDP

Usage:

  • Client sends first message
  • Server responds to each message
  • Type bye or end to terminate
  • Uses ports 9876 (client send) and 9877 (client receive)

05 - UDP DNS Resolver

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 ServeurDNS

Terminal 2 (DNS Client):

cd 05-udp-dns-resolver
java ClientDNS

Pre-configured Domains:

Usage:

  • Enter domain name to resolve
  • Server returns corresponding IP address
  • Type quit or exit to close client
  • Server continues running until manually stopped

06 - Raw Packet Capture

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 PacketCapture

Interactive Prompts:

  1. Enter source IP address (e.g., 192.168.1.10)
  2. Enter source port (e.g., 8080)
  3. Enter destination IP address (e.g., 192.168.1.20)
  4. 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.


Key Concepts Demonstrated

TCP vs UDP

  • TCP (Exercises 1-3): Connection-oriented, reliable, ordered delivery
  • UDP (Exercises 4-5): Connectionless, faster, no guaranteed delivery

Network Programming Patterns

  • Client-Server architecture
  • Request-Response model
  • Bidirectional communication
  • Packet-level manipulation

Socket Programming

  • ServerSocket and Socket (TCP)
  • DatagramSocket and DatagramPacket (UDP)
  • Input/Output streams
  • Buffer management

Error Handling

  • Checksum calculation and verification
  • Connection error handling
  • Input validation
  • Resource cleanup (finally blocks)

Common Commands

Compile all files in a directory:

javac *.java

Run with explicit classpath:

java -cp . ClassName

Stop a running server:

Press Ctrl+C in the server terminal

Troubleshooting

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

Network Ports Used

  • 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)

Notes

  • 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

Author

Younes Bensafia

License

Educational purposes - Network Programming with Java

About

This repository is demonstrating network programming concepts using Java sockets, covering both TCP and UDP protocols, as well as packet manipulation.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages