JAVA Networking Classes and Interfaces
JAVA Networking Classes and Interfaces
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.
The java.net package provides the functionality for two common protocols:
TCP − TCP stands for Transmission Control Protocol, which allows for reliable communication
between two applications. TCP is typically used over the Internet Protocol, which is referred to as
TCP/IP.
UDP − UDP stands for User Datagram Protocol, a connection-less protocol that allows for packets of
data to be transmitted between applications.
Networking Terminology
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.
Protocol
A protocol is basically a set of rules and guidelines which provides the instructions to send request and
receive response over the network.
For example: TCP, UDP, SMTP, FTP etc.
IP Address
IP Address stands for Internet protocol address. It is an identification number that is assigned to a node
of a computer in the network.
For example: 192.168.2.01
Range of the IP Address
0.0.0.0 to 255.255.255.255
Port Number
The port number is an identification number of server software. The port number is unique for different
applications. It is a 32-bit positive integer number having between ranges 0 to 65535.
Socket
Socket is a listener through which computer can receive requests and responses. It is an endpoint of two
way communication link. Every server or programs runs on the different computers that has a socket and
is bound to the specific port number.
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.
Socket Class
The java.net.Socket class is used to create a socket so that both the client and the server can
communicate with each other easily. A socket is an endpoint for communication between two
computers. The Socket class inherits the Object class and implements the Closeable interface.
Constructor Description
Socket() Creates an
unconnected socket,
with the system-
default type of
SocketImpl.
protected Creates a
Socket(SocketImpl connectionless
impl) Socket with a user-
specified
SocketImpl.
Constructor Description
import java.io.*;
import java.net.*;
public class SerSocket
{
public static void main(String args[])
{
int port=8080;
try
{
ServerSocket ss = new ServerSocket(port);
System.out.println("Server initialized on port: " + port);
ss.getLocalPort();
ss.getInetAddress();
{
while(true)
ss.accept();
}
}
catch (SocketException e)
{
System.out.println("Socket error");
}
catch ( IOException e)
{
System.out.println("An I/O Exception Occurred!");
}
}
}
Output:
Server initialized on port: 8080