Java sockets
In Java, sockets are used for communication between two machines over a network. The Java networking API provides two types of sockets - client sockets and server sockets. Client sockets are used to initiate a connection to a remote server, while server sockets are used to listen for incoming connections.
Here is an example of how to use client sockets in Java:
import java.net.*; import java.io.*; public class Client { public static void main(String[] args) throws IOException { String hostName = "localhost"; int portNumber = 4444; try ( Socket socket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream())); BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)) ) { String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: " + in.readLine()); } } catch (UnknownHostException e) { System.err.println("Don't know about host " + hostName); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to " + hostName); System.exit(1); } } }
In this example, a client socket is created to connect to a server running on the local machine (using localhost
as the hostname) on port 4444. The getOutputStream()
method of the Socket object is used to send data to the server, and the getInputStream()
method is used to receive data from the server.
The try
block in this example uses a Java feature called "try-with-resources" to ensure that the Socket and I/O streams are closed properly, even in the event of an exception.
Here is an example of how to use server sockets in Java:
import java.net.*; import java.io.*; public class Server { public static void main(String[] args) throws IOException { int portNumber = 4444; try ( ServerSocket serverSocket = new ServerSocket(portNumber); Socket clientSocket = serverSocket.accept(); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); ) { String inputLine; while ((inputLine = in.readLine()) != null) { out.println(inputLine); } } catch (IOException e) { System.out.println("Exception caught when trying to listen on port " + portNumber + " or listening for a connection"); System.out.println(e.getMessage()); } } }
In this example, a server socket is created on port 4444. The accept()
method of the ServerSocket waits for an incoming client connection. Once a client connects, the accept()
method returns a new Socket object that is used to communicate with the client. The getOutputStream()
method of the Socket object is used to send data to the client, and the getInputStream()
method is used to receive data from the client.
As with the client example, the try
block in this example uses "try-with-resources" to ensure that the ServerSocket and client Socket objects are closed properly, even in the event of an exception.
It is important to note that sockets are typically used in multithreaded applications, where each client connection is handled by a separate thread. This allows the server to handle multiple client connections simultaneously.