9.2 Sockets Explained
Sockets are a fundamental concept in network programming, allowing applications to communicate over a network. In Java SE 11, sockets are used to establish connections between clients and servers, enabling data exchange. Understanding sockets is crucial for developing networked applications.
Key Concepts
1. Socket Definition
A socket is an endpoint for sending and receiving data across a network. It consists of an IP address and a port number, which together uniquely identify the connection.
2. ServerSocket
A ServerSocket is used on the server side to listen for incoming client connections. It waits for a client to connect and then creates a Socket object to handle the communication with that client.
Example
ServerSocket serverSocket = new ServerSocket(8080); Socket clientSocket = serverSocket.accept();
3. Socket
A Socket is used on the client side to connect to a server. Once connected, the client and server can exchange data through input and output streams.
Example
Socket socket = new Socket("localhost", 8080); InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream();
4. Streams
Streams are used to send and receive data over a socket. An InputStream is used to read data from the socket, and an OutputStream is used to write data to the socket.
Example
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); PrintWriter writer = new PrintWriter(out, true);
5. TCP vs. UDP
TCP (Transmission Control Protocol) provides reliable, ordered, and error-checked delivery of data between applications. UDP (User Datagram Protocol) is a connectionless protocol that does not guarantee delivery or order of data.
Example
TCP is commonly used for applications that require reliable data transfer, such as web browsing and file transfer. UDP is used for real-time applications like video streaming and online gaming.
6. Multithreading
Multithreading is often used in socket programming to handle multiple client connections simultaneously. Each client connection can be handled by a separate thread, allowing the server to serve multiple clients concurrently.
Example
while (true) { Socket clientSocket = serverSocket.accept(); new Thread(() -> { // Handle client communication }).start(); }
Examples and Analogies
Think of sockets as telephone lines connecting two parties. The ServerSocket is like a receptionist who answers incoming calls and connects them to the appropriate person. The Socket is like the telephone used by the caller to connect to the receptionist. Streams are like the voice channels that allow the caller and the receiver to communicate. TCP is like a phone call where both parties can hear each other clearly, while UDP is like a radio broadcast where messages may be lost or arrive out of order.
By mastering sockets, you can create networked applications in Java SE 11 that communicate effectively over the internet, enabling everything from simple chat applications to complex distributed systems.