CS18000: Problem Solving and Object-Oriented Programming - Network I/O Notes

Early computing required manual transfer of files between computers (A to B). The 1960s marked the beginning of direct communication methods among computers, where packets of information started to be transmitted over physical wires, leading to a significant shift towards wireless communication.

Computer Networks
  • Network: Defined as a collection of interconnected computers within a building that can exchange information.

  • Network of Networks: This refers to the connection of multiple networks across buildings, which has ultimately resulted in the creation of the Internet. Influential figures such as Professor Douglas Comer from Purdue University contributed significantly to this development.

Key Definitions
  • Internet Protocol (IP): This serves as a unique identifying address for devices connected to a network, e.g., 128.10.2.21.

  • Domain Name System (DNS): This system is responsible for mapping human-readable domain names (e.g., www.example.com) to corresponding IP addresses.

  • Transmission Control Protocol (TCP): It identifies ports that facilitate network connections, ensuring reliable data transmission.

  • Socket: A socket is a combination of an IP address and a TCP port; it enables communication sources between clients and servers.

Client-Server Architecture
  • Server: A process that is continuously waiting for client connections to handle requests. Commonly, servers are dedicated to serving multiple clients simultaneously.

  • Client: A process that initiates a connection to a server to request data or services. Each computer in a network can run both client and server processes, allowing bi-directional data exchange.

Use of Sockets
  • Sockets act as endpoints for communication between clients and servers.

  • IP Addresses: Must be globally unique and are expressed in a dotted-decimal format (e.g., 128.10.9.143).

  • Port Numbers: An integer range from 0 to 65535 (16 bits); lower numbers (0-1023) are reserved for privileged access and specific protocols (e.g., HTTP, FTP).

Java Networking: Object Communication
  • Java supports object serialization for networking purposes using classes like ObjectOutputStream and ObjectInputStream.

  • Header Requirement: Objects transmitted between client and server must include a header that needs proper management; flushing is necessary to ensure the correct transfer of data.

  • Blocking: If not managed carefully, can hinder data transfer, thus requiring a strategy for stream management.

ObjectStream Client-Server Timeline
  1. Server starts and waits for client connections.

  2. Client connects, and the server sends an ObjectOutputStream along with the header.

  3. The client receives the header and sends back object streams.

  4. The objects are exchanged in a specific order, adhering to the protocol defined.

Java Networking Classes
  • Socket: Facilitates managed TCP/IP communication with the server using new Socket("host", port).

  • ServerSocket: Listens for incoming connections on a specified port, instantiated with ServerSocket ss = new ServerSocket(port).

Example: Object Server
import java.io.*;
import java.net.*;
public class Server {
   public static void main(String[] args) throws IOException, ClassNotFoundException {
       ServerSocket serverSocket = new ServerSocket(4242);
       Socket socket = serverSocket.accept();
       ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
       oos.flush();
       ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
       String s1 = "hello there";
       oos.writeObject(s1);
       oos.flush();
       String s2 = (String) ois.readObject();
       oos.close();
       ois.close();
   }
}
Example: Object Client
import java.io.*;
import java.net.*;
public class Client {
   public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException {
       Socket socket = new Socket("data.cs.purdue.edu", 4242);
       ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
       ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
       oos.flush();
       String s1 = (String) ois.readObject();
       String s2 = s1.toUpperCase();
       oos.writeObject(s2);
       oos.flush();
       oos.close();
       ois.close();
   }
}
Client-Server with Threads
  • In scenarios where multiple clients connect, servers utilize threads to handle each connection simultaneously.

  • Each client can operate with dedicated threads, allowing for effective reading from and writing to the server without interference.

Example: Echo Server Implementation
  • Functionality: Accepts multiple client connections; each handled within a separate thread; reads input and echoes back responses.

public class EchoServer implements Runnable {
   Socket socket;
   public EchoServer(Socket socket) { this.socket = socket; }
   public void run() {
       // Details omitted for brevity (input handling and echoing)
   }
}

public static void main(String[] args) throws IOException {
   ServerSocket serverSocket = new ServerSocket(4343);
   while (true) {
       Socket socket = serverSocket.accept();
       EchoServer server = new EchoServer(socket);
       new Thread(server).start();
   }
}
Network Communication in Java
  • Java leverages standard I/O classes with additional abstractions specifically for network connections, enhancing functionality and usability.

  • The ServerSocket class waits for incoming connections, while the Socket class manages active TCP connections for data exchange.

  • Implementing threads supports responsiveness, preventing blocking during network operations, crucial for maintaining effective communication in client-server architecture.