Java Networking step by step Tutorial

Leave a Comment
best Java Networking step by step Tutoriall for beginners <!--


Java Networking is a concept of connecting two or more computing devices together so that we can share resources.
Java socket programming provides facility to share data between different computing devices.

Advantage of Java Networking

1.     sharing resources
2.     centralize software management

Java Networking Terminology

The widely used java networking terminologies are given below:
1.     IP Address
2.     Protocol
3.     Port Number
4.     MAC Address
5.     Connection-oriented and connection-less protocol
6.     Socket

1) IP Address

IP address is a unique number assigned to a node of a network e.g. . It is composed of octets that range from 0 to 255.
It is a logical address that can be changed.

2) Protocol

A protocol is a set of rules basically that is followed for communication. For example:
  • TCP
  • FTP
  • Telnet
  • SMTP
  • POP etc.

3) Port Number

The port number is used to uniquely identify different applications. It acts as a communication endpoint between applications.
The port number is associated with the IP address for communication between two applications.

4) MAC Address

MAC (Media Access Control) Address is a unique identifier of NIC (Network Interface Controller). A network node can have multiple NIC but each with unique MAC.

5) Connection-oriented and connection-less protocol

In connection-oriented protocol, acknowledgement is sent by the receiver. So it is reliable but slow. The example of connection-oriented protocol is TCP.
But, in connection-less protocol, acknowledgement is not sent by the receiver. So it is not reliable but fast. The example of connection-less protocol is UDP.

6) Socket

A socket is an endpoint between two way communication.
Visit next page for java socket programming.

Some Important Classes

Inet Address

Some Important Interfaces



Inet Address encapsulates both numerical IP address and the domain name for that address. Inet address can handle both IPv4 and Ipv6 addresses. Inet Address class has no visible constructor. To create an inet Address object, you have to use Factory methods.
Three commonly used Inet Address factory methods are.
1.     static InetAddress getLocalHost() throws UnknownHostException
2.     static InetAddress getByName (String hostname) throws UnknownHostException
3.     static InetAddress[ ] getAllByName (String hostname) throws UnknownHostException

Java TCP Networking Basics

Typically a client opens a TCP/IP connection to a server. The client then starts to communicate with the server. When the client is finished it closes the connection again. Here is an illustration of that:

A client may send more than one request through an open connection. In fact, a client can send as much data as the server is ready to receive. The server can also close the connection if it wants to.

Java Socket's and ServerSocket's

When a client wants to open a TCP/IP connection to a server, it does so using a Java Socket. The socket is told what IP address and TCP port to connect to and the rest is done by Java.
If you want to start a server that listens for incoming connections from clients on some TCP port, you have to use a Java ServerSocket. When a client connects via a client socket to a server's ServerSocket, a Socket is assigned on the server to that connection. The client and server now communicates Socket-to-Socket.
Socket's and ServerSocket's are covered in more detail in later texts.

Java UDP Networking Basics

UDP works a bit differently from TCP. Using UDP there is no connection between the client and server. A client may send data to the server, and the server may (or may not) receive this data. The client will never know if the data was received at the other end. The same is true for the data sent the other way from the server to the client.
Because there is no guarantee of data delivery, the UDP protocol has less protocol overhead.

Socket Programming in Java

Networking is a concept of connecting two or more computing devices together so that we can share resources like printer, scanner, memory.
In Networking application mainly two programs are running one is Client program and another is Server program. In Core java Client program can be design using Socket class and Server program can be design using ServerSocket class.
Both Socket and ServerSocket classes are predefined in package

Advantage of Network Programming

The main advantage of network Programming is sharing of data and resources, some more advantages are;
·         Sharing resources like printer, Scanner.
·         Centralize software management, Software install on only one system and used in multiple system.
·         Sharing of data due to this reduce redundancy of application.
·         Burden on the developer can be reduced.
·         Wastage of memory can be reduced because no need to install same application on every system.
·         Time consuming process to develop application is reduced.

Terms used in Socket Programming

Port number: It is unique identification value represents residing position of a server in the computer. It is four digit +ve number.
Port Name: It is a valid user defined name to know about client system, the default port name for any local computer is localhost.. Port name should be the some value which is given at Server programming.

Socket class

Socket class are used for design a client program, it have some constructor and methods which are used in designing client program.
Constructor: Socket class is having a constructor through this Client program can request to server to get connection.

Syntax to call Socket() Constructor

Socket s=new Socket("localhost", 8080);
// localhost -- port name and 8080 -- port number
Note: If given port name is invalid then UnknownHostException will be raised.
Method of Socket class
·         public InputStream getInputStream()
·         public OutputStream getOutputStream()
·         public synchronized void close()


This method take the permission to write the data from client program to server program and server program to client program which returns OutputStream class object.


Socket s=new Socket("localhost", 8080);
OutputStream os=new s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);


This method is used to take the permission to read data from client system by the server or from the server system by the client which returns InputStream class object.


Socket s=new Socket("localhost", 8080);
InputStream is=new s.getInputStream();
DataInputStream dis=new DataInputStream(is);


This method is used to request for closing or terminating an object of socket class or it is used to close client request.


Socket s=new Socket("localhost", 8080);

ServerSocket class

The ServerSocket class can be used to create a server socket. ServerSocket object is used to establish the communication with clients.
ServerSocket class are used for design a server program, it have some constructor and methods which are used in designing server program.
Constructor: ServerSocket class contain a constructor used to create a separate port number to run the server program.

Syntax to call ServerSocket() Constructor

ServerSocket ss=new ServerSocket(8080);
// 8080 -- port number
Method of ServerSocket class
·         public Socket accept()
·         public InputStream getInputStream()
·         public OutputStream getOutputStream()
·         public synchronized void close()
accept(): Used to accept the client request it returns class reference.


Socket s=new Socket("localhost", 8080);
ServerSocket ss=new ServerSocket(8080);
Socket s=ss.accept();

Rules to design server program

·         Every server program should run accepted port number (any 4 digit +ve numeric value) It can set by relating an object for server socket class (In any used defined java program).


ServerSocket ss=new ServerSocket(8080);
·         Accept client request.
·         Read input data from client using InputStream class.
·         Perform valid business logic operation
·         Send response (writing output data) back to client using OutputStream class.
·         close or terminate client request.

Rules to design client program

·         Obtain connection to the server from the client program (any user defined class) by passing port number and port name in the socket class.


Socket s=new Socket(8080, "localhost");
// 8080 is port number and localhost is port name
·         Send request (writing input data) to the server using OutputStream class.
·         Read output data from the server using InputStream class.
·         Display output data
Note: close the connection is optional.

Server Code

// Saved by
class Server
public static void main(String[] args) 
int pno=Integer.parseInt(args[0]);
ServerSocket ss=new ServerSocket(pno);
System.out.println("server is ready to accept clint request");
Socket s1=ss.accept();
InputStream is=s1.getInputStream();
DataInputStream dis=new DataInputStream(is);
int n=dis.readInt();
System.out.println("Value from client : "+n);
int res=n*n;
OutputStream os=s1.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
catch (Exception e)

Client Code

// Saved by
import java.util.*;
class Client
public static void main(String[] args) 
 String pname=args[0];
 int pno=Integer.parseInt(args[1]);
 Socket s=new Socket(pname,pno);
 System.out.println("clint obtailed connection from server");
 System.out.println("Enter a number ");
 Scanner sn=new Scanner(;
 int data=sn.nextInt();
 OutputStream os=s.getOutputStream();
 DataOutputStream dos=new DataOutputStream(os);
 InputStream is=s.getInputStream();
 DataInputStream dis=new DataInputStream(is);
 int res=dis.readInt();
 System.out.println("Result from server : "+res);
catch (Exception e)
Download Code Client Server code

Steps to run above program

·         First open two command prompt window separately.
·         Compile Client program on one command prompt and compile server program on other command prompt.
·         First run Server program and pass valid port number.
·         Second run client program and pass valid port name and post number (which is already passed at server).
·         Finally give the request to server from client (from client command prompt).
·         Now you can get response from server.

Compile and run server program

Compile and run client program

Limitation of J2SE network programming

·         Using this concept you can share resource locally but not globally.
·         Using this concept you can not develop internet based application.
·         Using this concept you can develop only half-duplex application.
·         Using this concept you can not get service from universal protocols like http, ftp etc...
·         Using this concept you can not get services from universal server software like tomcat, weblogic etc..
To overcome all these limitation use Servlet and JSP technology.


Search This Blog

Video of the Day