Networking in java provides a set of classes and interfaces that enable developers to create network applications. These classes and interfaces make it easy to create network sockets, read and write data over a network, and handle network connections. The Java networking API is based on the TCP/IP protocol, which is the most widely used protocol for data communication over the internet.
Table of Contents
Java Networking Examples
Let’s look at some code examples to demonstrate how the Java networking API is used in practice.
Client-Server Communication
The following code demonstrates how a client can connect to a server and send a message
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 1234);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message = "Hello, server!";
out.println(message);
System.out.println("Message sent: " + message);
String response = in.readLine();
System.out.println("Response received: " + response);
}
}
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(1234);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String message = in.readLine();
System.out.println("Message received: " + message);
String response = "Hello, client!";
out.println(response);
System.out.println("Response sent: " + response);
}
}
Downloading a File from the Web
The following code demonstrates how to download a file from the web:
import java.io.*;
import java.net.*;
public class FileDownloader {
public static void main(String[] args) throws IOException {
String fileUrl = "https://example.com/sample.pdf";
URL url = new URL(fileUrl);
HttpURLConnection httpConn = (
Socket Programming in Java
In Java, Socket programming is used for communication between two machines over a network. The communication between two machines is established using Socket classes.
A socket is an endpoint of a two-way communication link between two programs running on a network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent.
Types of Sockets in Java
There are two types of sockets in Java:
- Client Socket
- Server Socket
Client Socket
A client socket is used to establish a connection with a server socket.
The following steps are used to create a client socket:
- Create a Socket object and pass the IP address and port number of the server as parameters.
- Open the input and output streams of the socket.
- Use the output stream to write data to the server.
- Use the input stream to read data from the server.
- Close the input and output streams and the socket.
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8000);
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
writer.println("Hello, server");
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String message = reader.readLine();
System.out.println("Server says: " + message);
socket.close();
} catch (IOException ex) {
System.out.println("Client exception: " + ex.getMessage());
ex.printStackTrace();
}
}
}
Server Socket
A server socket is used to listen for incoming client connections.
The following steps are used to create a server socket:
- Create a ServerSocket object and specify the port number to listen on.
- Call the accept() method of the ServerSocket object to listen for incoming connections.
- Open the input and output streams of the socket.
- Use the output stream to write data to the client.
- Use the input stream to read data from the client.
- Close the input and output streams and the socket.
Here is an example of a server socket implementation in Java:
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8000);
System.out.println("Server is listening on port 8000");
Socket socket = serverSocket.accept();
System.out.println("New client connected");
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String message = reader.readLine();
System.out.println("Client says: " + message);
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
writer.println("Hello, client");
socket.close();
} catch (IOException ex) {
System.out.println("Server exception: " + ex.getMessage());
ex.printStackTrace();
}
}
}
Conclusion
In this article, we discussed Java networking and its various components like InetAddress, URL, HttpURLConnection, and Socket programming. We also covered examples of client and server socket implementations. With these concepts and examples, you can start developing networking applications in Java.