Socket communication in Java enables communication between two endpoints over a network. There are two main types of sockets: TCP sockets and UDP sockets. Let's explain both types with examples: TCP Socket Communication: 1. **Server Side**: - The server creates a `ServerSocket` object to listen for incoming connections on a specific port. - When a client connects, the server accepts the connection and creates a `Socket` object to communicate with the client. - The server reads from and writes to the socket's input and output streams to communicate with the client. import java.io.*; import java.net.*; public class TCPServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(12345); System.out.println("Server started. Waiting for client..."); Socket clientSocket = serverSocket.accept(); System.out.println("Client connected."); BufferedReader in = new Bu
Here are some notes on scanning and formatting in Java I/O with examples: 1. Scanning (Input): - Scanning involves reading input from different sources like the keyboard, files, or network connections. - Java provides the `Scanner` class in the `java.util` package to facilitate scanning. Example: import java.util.Scanner; public class ScannerExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.nextLine(); System.out.println("Hello, " + name + "!"); scanner.close(); } } 2. Formatting (Output): - Formatting is the process of presenting data in a specific way, such as aligning text, setting precision for floating-point numbers, etc. - The `System.out.printf()` method is commonly used for formatted output in Java I/O. Example: public class FormattingExample {