0% found this document useful (0 votes)
665 views

JAVA Networking Classes and Interfaces

The java.net package provides classes and interfaces that allow for network communication in Java. This includes socket programming using TCP and UDP, as well as common network terminology like IP addresses, ports, sockets, and protocols. Key classes are Socket for network connections and ServerSocket for listening for client requests.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
665 views

JAVA Networking Classes and Interfaces

The java.net package provides classes and interfaces that allow for network communication in Java. This includes socket programming using TCP and UDP, as well as common network terminology like IP addresses, ports, sockets, and protocols. Key classes are Socket for network connections and ServerSocket for listening for client requests.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

JAVA NET PACKAGE

Java Networking Classes and Interface

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.

Java program communicates over the network at application layer.


All the Java networking classes and interfaces use java.net package. These classes and interfaces
provide the functionality to develop system-independent network communication.

Advantage of Java Networking


 sharing resources
 centralize software management

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

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.

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.

Java Socket Programming


Socket provides an endpoint of two way communication link using TCP protocol. Java socket can be
connection oriented or connection less. TCP provides two way communication, it means data can be
sent across both the sides at same time.

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.

Socket Class Constructors

Constructor Description

Socket() Creates an
unconnected socket,
with the system-
default type of
SocketImpl.

public Creates a stream


Socket(InetAddress socket with specified
address, int port) IP address to the
specified port
number.

public Uses the


Socket(InetAddress DatagramSocket.
host, int port, boolean
stream)

public Creates a connection


Socket(InetAddress with specified
address, int port, remote address and
InetAddress localAddr, remote port.
int local port)
public Socket(Proxy, Creates a
proxy) connectionless
socket specifying the
type of proxy.

protected Creates a
Socket(SocketImpl connectionless
impl) Socket with a user-
specified
SocketImpl.

Server Socket Class


Socket class is used to create socket and send the request to the server. Java ServerSocket class waits
for request to come over the network. It works on the basis of request and then returns a result to the
request. It implements the Closeable interface.

Server Socket Class Constructors

Constructor Description

ServerSocket() Creates an unbound


server socket.

ServerSocket(int port) Creates a server


socket, bound to the
specified port.

ServerSocket(int port, Creates a server


int backlog) socket, bound to the
specified port, with
specified local port.

ServerSocket(int port, Creates a server


int backlog, socket, bound to
inetAddress specified port, listen
bindAddrs) backlog, and IP
address.
Example: Implementing the Server Socket class & its methods

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

You might also like