Java Networking

Java Networking

Java Networking is a vast topic, encompassing a broad range of classes and functionalities to achieve various networking tasks. This tutorial will provide an overview of the fundamentals of Java Networking.

Introduction:

Java provides extensive support for network programming through the java.net package. This package provides classes for implementing networking applications using protocols like TCP/IP and UDP/IP.

Basics of Networking:

  1. IP Address: Represents a unique address for machines on a network. In Java, the InetAddress class represents an IP address.

  2. Port Number: A 16-bit unsigned number, used to identify a specific process on a machine. The valid range is from 0 to 65535, but ports from 0 to 1023 are reserved for standard services.

  3. URL (Uniform Resource Locator): Represents a reference or address to a resource on the internet.

  4. Socket: An endpoint for communication between two machines on a network. Java provides the Socket class for TCP communication and the DatagramSocket class for UDP communication.

Key Classes and Methods:

1. InetAddress:

  • static InetAddress getLocalHost(): Returns the local host.
  • static InetAddress getByName(String host): Determines the IP address of a host.

2. Socket:

Used for TCP communication. When creating a socket, you're trying to establish a connection.

  • Constructor: Socket(String host, int port)
  • getInputStream(): Returns the input stream of the socket.
  • getOutputStream(): Returns the output stream of the socket.

3. ServerSocket:

Listens for client requests and establishes connections.

  • Constructor: ServerSocket(int port)
  • accept(): Waits and listens for a connection.

4. DatagramSocket and DatagramPacket:

Used for UDP communication.

  • DatagramSocket(int port): Creates a socket.
  • send(DatagramPacket p): Sends a UDP packet.
  • receive(DatagramPacket p): Receives a UDP packet.

5. URL and URLConnection:

  • URL(String spec): Creates a URL object.
  • openConnection(): Returns a URLConnection object that represents a connection to the remote object referred to by the URL.

Simple TCP Client-Server Example:

Server:

import java.net.*; import java.io.*; public class TCPServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("Listening on port 8888..."); Socket clientSocket = serverSocket.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); String inputLine; while ((inputLine = in.readLine()) != null) { out.println("Echo: " + inputLine); } clientSocket.close(); serverSocket.close(); } } 

Client:

import java.net.*; import java.io.*; public class TCPClient { public static void main(String[] args) throws IOException { Socket socket = new Socket("localhost", 8888); 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("Server says: " + in.readLine()); } socket.close(); } } 

Conclusion:

Java Networking offers a comprehensive framework for creating networking applications. The java.net package provides the tools to develop both client-side and server-side networking applications. With knowledge of the core classes and methods, you can start building various network-based Java applications. However, remember to handle exceptions and edge cases in real-world scenarios.


More Tags

angular-cli-v6 chart.js2 google-sheets-query text-cursor nexus3 localhost sorting parent-child responsive functional-programming

More Programming Guides

Other Guides

More Programming Examples