๐Ÿงญ Topic: Network

Quick Overview :

This topic introduces network programming in Java, covering the OSI model, TCP/IP protocols, IP addresses, ports, sockets, and client-server architecture. It provides hands-on experience building networked applications with multi-threading and object serialization.


๐Ÿ“Œ Covered in This Topic

OSI Model Layers

  • 7. Application Layer: HTTP, FTP, SMTP, DNS, SSH (DATA)
  • 6. Presentation Layer: SSL/TLS, JPEG, ASCII, Encryption (DATA)
  • 5. Session Layer: NETBIOS, Authentication, Sessions (DATA)
  • 4. Transport Layer: TCP, UDP - Connection Flow Control (SEGMENTS)
  • 3. Network Layer: IP, ICMP, Routing (PACKETS)
  • 2. Data Link Layer: Ethernet, MAC Addressing, Switching (FRAMES)
  • 1. Physical Layer: CABLES, Signals, Binary Transmission (BITS)

Internet Protocols

TCP (Transmission Control Protocol)

  • Connection-oriented protocol
  • Provides reliable, ordered, and error-checked delivery of data
  • Suitable for applications requiring high reliability: web browsing, email, file transfer
  • Slower than UDP due to overhead of ensuring reliability
  • Uses three-way handshake (SYN, SYN-ACK, ACK) to establish connection

UDP (User Datagram Protocol)

  • Connectionless protocol
  • Provides unreliable, unordered delivery of data
  • Suitable for applications prioritizing speed over reliability: online gaming, streaming media, VoIP
  • Faster than TCP due to minimal overhead

IP Addresses

  • Unique numerical label assigned to each device connected to a network
  • Used for addressing and routing packets across networks
  • Two main versions: IPv4 and IPv6

Ports

  • Numerical identifier used by transport layer to distinguish between multiple services/applications
  • 16-bit unsigned integers ranging from 0 to 65535
  • Commonly used ports:
    • 80 โ€“ HTTP (Web traffic)
    • 443 โ€“ HTTPS (Secure Web traffic)
    • 22 โ€“ SSH (Secure shell access)
    • 25 โ€“ SMTP (Email sending)
    • 3306 โ€“ MySQL
    • 8080 โ€“ Common alternative for HTTP (especially for development)

Sockets

  • Represents one endpoint of a two-way communication channel between two programs over a network
  • Enable communication between client and server applications using TCP or UDP
  • ServerSocket: For servers, listens on specific port for incoming connections
  • Socket: For clients, connects to serverโ€™s IP address and port number
  • Created by specifying an IP address and a port number
  • Relationship: Multiple sockets can use the same port on different hosts or same host when connecting to different remote clients

Network Topologies

  • Various ways to connect network nodes (covered in slides)

๐Ÿ“‘ Slides & Materials


๐Ÿ› ๏ธ Workshop & Assignments

๐Ÿ’ฌ Workshop: Chat Application

  • ๐Ÿ“‚ WS-09-Network
  • Objective: Build a simple chat application using Java Sockets
  • Architecture:
    • Server: ChatServer with ServerSocket, ExecutorService thread pool, ClientHandler per client
    • Client: ChatClient with MessageListener thread for receiving messages
  • Concepts Practiced: Socket programming, Multi-threading, Thread Pool, I/O Streams, Synchronized Collections, Daemon Threads, Client-Server Architecture
  • TODO Sections:
    • ChatServer.java: broadcast(), broadcastSystemMessage(), removeClient()
    • ChatClient.java: connectToServer(), readUserInputAndSend(), cleanup()
    • MessageListener.java: run() method
  • Common Mistakes to Avoid: Not using synchronization in broadcast, not closing streams, forgetting to skip sender in broadcast, using new Thread instead of ExecutorService, not reading continuously from inputReader, not using setDaemon(true)

๐Ÿงฎ Assignment: Network File-Sharing Chat System

  • ๐Ÿ“‚ HW-10-Socket-Programming
  • Objective: Build a multi-client network chat system with file sharing
  • Features:
    • Public chat messages
    • Private messages (/msg <username> <message>)
    • Online users list (/users)
    • File transfers (/sendfile <username> <filepath>)
  • Architecture:
    • ChatServer โ†’ ClientSession threads โ†’ UserManager (ConcurrentHashMap)
    • Object serialization for ChatMessage and FileMessage
    • FileManager for per-user file storage
  • TODO Sections:
    • ChatServer.java: Pick port, create UserManager, open ServerSocket, accept clients loop
    • ClientSession.java: Set up object streams, handle login, dispatch messages (broadcast, private, user-list, file transfer), remove user on disconnect
    • ChatClient.java: Connect to server, set up streams, login, command loop
    • ServerListener.java: Continuously read and display messages/files
  • Important: Create ObjectOutputStream before ObjectInputStream (avoid deadlock), use out.flush() after writeObject()
  • Bonus: JavaFX UI implementation
  • Evaluation: Multi-client testing to verify broadcasting, private messages, and file transfer

RepositoryDescription
๐Ÿ“‚ WS-09-NetworkWorkshop: Build a chat application with Java Sockets (incomplete for practice)
๐Ÿ“‚ HW-10-Socket-ProgrammingAssignment: Network file-sharing chat system with private messages and file transfer

๐ŸŒ Additional Resources


โฉ Navigation


Tip :

When working with sockets, always remember to properly close resources (sockets, streams) and use synchronized blocks when accessing shared collections. The chat workshop is excellent practice for understanding the client-server model. For the assignment, test with multiple client instances to verify all features work correctly. The stream handshake issue (ObjectOutputStream before ObjectInputStream) is a common pitfall โ€“ always create streams in the correct order on both ends! Additionally, using thread pools (ExecutorService) is far better than creating new Thread objects for each client.