Skip to content

Networking In Java Programming Language

In this article, you will learn about Networking in Java Programming Language.

Networking or Network Programming is the process of writing codes and sharing the data between multiple computing devices that are connected to each other through a network.

In Java, these computing devices communicate with each other by using one of the two protocols, TCP(Transmission Control Protocol) or UDP(User Datagram Protocol).

TCP- It is used to communicate between two or more applications. It provides reliable and error-checked flow of data between two computing devices. That’s why it is also known as a connection-based protocol.

UDP- It allows applications to send datagrams(messages) to other applications on an IP network. The reliable communication and guarantee of message delivery is unsure with UDP. That’s why it is also known as connection-less transmission protocol.

Networking in java involves following two important concepts.

  1. Socket Programming
  2. URL Processing

In this article, we will focus only on Socket Programming which is described below in detail.

SOCKET PROGRAMMING

Socket Programming is used to make communication between computing devices through Transmission Control Protocol(TCP) or UDP(User Datagram Protocol).

Socket Programming are of two types: connection-based and connection-less based on the protocol it uses i.e, TCP and UDP respectively.

Classes used in connection-based socket programming are:

  • Socket Class(java.net.Socket)
  • ServerSocket Class(java.net.ServerSocket)

Classes used in connection-less socket programming are:

  • DatagramSocket Class
  • DatagramPacket Class

SOCKET CLASS

This class creates a socket that is used to make communication between a client and the server. It contains following constructors and methods.

Constructors:

  1. public Socket(String host, int port) throws UnknownHostException, IOException
  2. public Socket(InetAddress host, int port) throws IOException
  3. public Socket(String host, int port, InetAddress localAddress, int localPort) throws IOException
  4. public Socket(InetAddress host, int port, InetAddress localAddress, int localPort) throws IOException
  5. public Socket()

Methods:

  1. public void connect(SocketAddress host, int timeout) throws IOException
  2. public void close() throws IOException
  3. public SocketAddress getRemoteSocketAddress()
  4. public int getPort()
  5. public int getLocalPort()
  6. public InetAddress getInetAddress()
  7. public InputStream getInputStream() throws IOException
  8. public OutputStream getOutputStream() throws IOException

SERVERSOCKET CLASS

This class creates a server socket that is bounded to a specified port. It contains the following constructors and methods.

Constructors:

  1. public ServerSocket(int port) throws IOException
  2. public ServerSocket() throws IOException
  3. public ServerSocket(int port, int backlog) throws IOException
  4. public ServerSocket(int port, int backlog, InetAddress address) throws IOException

Methods:

  1. public int getLocalPort()
  2. public void setSoTimeout(int timeout)
  3. public void bind(SocketAddress host, int backlog)
  4. public Socket accept() throws IOException

INETADDRESS CLASS

The java.net.InetAddress class creates an InetAddress if the IP address is provided. This class also determines the IP address of the host. It contains the following methods.

Methods:

  1. static InetAddress getByAddress(String host, byte[] addr)
  2. static InetAddress[] getAllByName(String host)
  3. static InetAddress getLocalHost()
  4. byte[] getAddress()
  5. String getHostAddress()
  6. String getHostName()
  7. String toString()
  8. int hashCode()
  9. boolean isAnyLocalAddress()
  10. boolean isSiteLocalAddress()

SOCKET PROGRAMMING EXAMPLE

// Filename: ServerExample.java
import java.net.

class ServerExample {
public static void main(String[] args) {
try {
ServerSocket ssobj = new ServerSocket(6066) ;
Socket sobj = ssobj.accept() ;
DataInputStream d1 = new DataInputStream(sobj.getInputStream()) ;
String st = (String)d1.readUTF() ;
System.out.println(“Message = “+st) ;
ssobj.close() ;
}
catch(Exception ref) {
System.out.println(ref) ;
}
}
}

//Filename: ClientExample.java

import java.net.;

class ClientExample {
public static void main(String[] args) {
try {
Socket sobj = new Socket(“localhost”, 6066) ;
DataOutputStream d2 = new DataOutputStream(sobj.getOutputStream()) ;
d2.writeUTF(“This is a message to the Server”) ;
d2.flush() ;
d2.close() ;
sobj.close() ;
}
catch(Exception ref) {
System.out.println(ref) ;
}
}
}

Also Read:- Generic Methods and Classes in Java Programming Language

We have provided you the description on Networking in Java Programming Language. Hope you like this post. For more updates and related information, stay connected to our blogs on Java Programming Language.

Facebook
Twitter
LinkedIn
Pinterest