๐งญ 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
- ๐จโ๐ซ Professor Slides (PDF)
- ๐งโ๐ซ TA Workshop Slides (PDF)
- ๐ฅ Workshop Recordings
- ๐ฅ Assignment Explanation Videos
๐ ๏ธ Workshop & Assignments
๐ฌ Workshop: Chat Application
- ๐ WS-09-Network
- Objective: Build a simple chat application using Java Sockets
- Architecture:
- Server:
ChatServerwithServerSocket,ExecutorServicethread pool,ClientHandlerper client - Client:
ChatClientwithMessageListenerthread for receiving messages
- Server:
- 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โClientSessionthreads โUserManager(ConcurrentHashMap)- Object serialization for
ChatMessageandFileMessage FileManagerfor per-user file storage
- TODO Sections:
ChatServer.java: Pick port, create UserManager, open ServerSocket, accept clients loopClientSession.java: Set up object streams, handle login, dispatch messages (broadcast, private, user-list, file transfer), remove user on disconnectChatClient.java: Connect to server, set up streams, login, command loopServerListener.java: Continuously read and display messages/files
- Important: Create
ObjectOutputStreambeforeObjectInputStream(avoid deadlock), useout.flush()afterwriteObject() - Bonus: JavaFX UI implementation
- Evaluation: Multi-client testing to verify broadcasting, private messages, and file transfer
๐ Repository Links
| Repository | Description |
|---|---|
| ๐ WS-09-Network | Workshop: Build a chat application with Java Sockets (incomplete for practice) |
| ๐ HW-10-Socket-Programming | Assignment: Network file-sharing chat system with private messages and file transfer |
๐ Additional Resources
- Java Socket Programming Tutorial
- Java Object Serialization
- TCP/IP Protocol Suite
- OSI Model Explained
โฉ Navigation
Tip :
When working with sockets, always remember to properly close resources (sockets, streams) and use
synchronizedblocks 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.