Chapter 04
Chapter 04
Programming
Chapter-4
Avicenna University
Mohammad Jan “Haidari”
Lecture Contents
Java Networking
Advantage of Java Networking
Java Networking Terminology
java.net Package
Java.net/classes
Java Networking
• Java Network Programming is a set of APIs and techniques
used to enable communication between applications over a
network using Java's java.net package.
• It involves working with protocols like TCP, UDP, and HTTP to
facilitate data exchange between different systems.
• Java Networking is a concept of connecting two or more
computing devices together so that we can share resources.
Java Networking
• Java provides built-in support for networking through:
o Sockets (TCP and UDP)
o Datagram Packets (UDP communication)
o URL and HTTP Handling (for web interactions)
o Multithreading (for handling multiple network connections)
• Java's networking capabilities are widely used in web
applications, chat applications, file transfer systems, and
distributed computing.
Advantage of Java Networking
• Java networking provides several benefits for building distributed applications
and network-based communication such as:
o Platform Independence: "Write Once, Run Anywhere" feature ensures that
network applications work across different operating systems without modification.
o Built-in Networking API: Java provides a rich set of classes in the java.net
package for handling networking tasks like sockets, URL handling, and HTTP
requests. No need for third-party libraries for basic networking functionalities.
o Supports Multiple Protocols: Java supports TCP/IP, UDP, HTTP, FTP, SMTP, and
other networking protocols, making it versatile for different types of
communication.
Advantage of Java Networking
o Easy to Implement Multithreading: java multithreading support allows servers
to handle multiple client connections simultaneously.
o Secure Communication: Java provides built-in security features like SSL/TLS for
encrypted communication. Supports authentication and access control for
secure data transfer.
o Object-Oriented Approach: Java is object-oriented programming (OOP) makes it
easier to build modular and scalable networking applications.
o Robust Exception Handling: Java provides strong exception handling
mechanisms, reducing the chances of application crashes due to network failures.
o Integration with Web Technologies: Java networking easily integrates with
servlets, JSP, REST APIs, and web applications for web-based communication.
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
Java Networking Terminology
• IP address: is a unique number assigned to a node of a network e.g.
192.168.0.1 . It is composed of octets that range from 0 to 255.
• It is a logical address that can be changed.
• The two most common versions of IP in use today are Internet Protocol
version 4 (IPv4) and Internet Protocol version 6 (IPv6). Both IPv4 and
IPv6 addresses come from finite pools of numbers.
• For IPv4, this pool is 32-bits (232) in size and contains 4,294,967,296
IPv4 addresses. The IPv6 address space is 128-bits (2128) in size,
containing 340,282,366,920,938,463,463,374,607,431,768,211,456 IPv6
addresses.
Java Networking Terminology
• Protocols: A protocol is a set of rules basically that is followed for
communication, or A network protocol is a set of established rules that
specify how to format, send and receive data so that computer network
endpoints, including computers, servers, routers and virtual machines,
can communicate despite differences in their underlying infrastructures,
designs or standards.
• For example:
• TCP(Transmission Control Protocol)
• FTP(File Transfer Protocol)
• Telnet
• SMTP(Simple Mail Transfer Protocol)
• POP (Post Office Protocol)
• etc.
Java Networking Terminology
• Port number: A port number is a way to identify a specific
process to which an internet or other network message is to be
forwarded when it arrives at a server.
• All network-connected devices come equipped with
standardized ports that have an assigned number.
• The reason why they are reserved is for certain protocols and
their associated function. Hypertext Protocol (HTTP)
messages, for example, always got to port 80 one of the most
commonly used ports.
Java Networking Terminology
• Port number is a 16-bit numerical value that ranges from
0 to 65535.
Java Networking Terminology
• MAC: 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 address.
• MAC address is the physical address, and also known
as hardware address. For example, an ethernet card may
have a MAC address of 00:0d:83::b1:c0:8e.
Java 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.
Java Networking Terminology
• Socket: A socket is one endpoint of a two-way
communication link between two programs running on the
network.
• A socket is bound to a port number so that the TCP layer
can identify the application that data is destined to be sent
to.
• An endpoint is a combination of an IP address and a port
number.
java.net Package
• java.net Package Provides the classes for implementing
networking applications.
• The java.net package supports two protocols,
o TCP: Transmission Control Protocol provides reliable
communication between the sender and receiver. TCP is used
along with the Internet Protocol referred as TCP/IP.
o UDP: User Datagram Protocol provides a connection-less
protocol service by allowing packet of data to be transferred
along two or more nodes
java.net Package
• The java.net package can be roughly divided in two
sections:
• A Low-Level API, which deals with the following
abstractions:
o Addresses, which are networking identifiers, like IP addresses.
o Sockets, which are basic bidirectional data communication
mechanisms.
o Interfaces, which describe network interfaces.
Package java.net
• A High-Level API, which deals with the following
abstractions:
o URIs, which represent Universal Resource Identifiers.
o URLs, which represent Universal Resource Locators.
o Connections, which represents connections to the resource
pointed to by URLs.
Package java.net/classes
• A class in the java.net package is a blueprint for
creating network-related objects. These classes
provide methods and properties to handle network
communication, IP addressing, URLs, sockets, and
protocols.
• The java.net package provides many classes to deal with
networking applications in Java. A list of some classes is
given below:
Package java.net/classes
Class Name Purpose
InetAddress Represents an IP address (IPv4 or IPv6).
Socket Represents a TCP client connection.
ServerSocket Listens for incoming TCP connections (server).
DatagramSocket Enables UDP communication (client & server).
DatagramPacket Represents a UDP packet of data.
Represents a web address (e.g.,
URL
https://example.com).
Handles communication between a Java program
URLConnection
and a web server.
HttpURLConnection Specialized class for making HTTP requests.
Proxy Represents a network proxy server.
Provides authentication (username/password) for
Authenticator
network connections.
Java.net Addresses
• Addresses are used throughout the java.net APIs as either host identifiers, or
socket endpoint identifiers.
• Java InetAddress class represents an IP address. The java.net.InetAddress
class provides methods to get the IP of any host name for
example www.javatpoint.com, www.google.com, www.facebook.com, etc.
• It has two subclasses:
• Inet4Address for IPv4 addresses.
• Inet6Address for IPv6 addresses.
• But, in most cases, there is no need to deal directly with the subclasses, as the
InetAddress abstraction should cover most of the needed functionality.
Java.net Addresses
• Some important methods of InetAddress class :
Method Description
getByName(String host) Gets the InetAddress of a hostname or IP.
getLocalHost() Gets the IP of the local machine.
getAllByName(String
Gets all IP addresses associated with a hostname.
host)
getHostName() Returns the hostname of the InetAddress object.
getHostAddress() Returns the IP address in text format.
isReachable(int timeout) Checks if the address is reachable.
Java.net Addresses
• import java.net.*;
• public class InetDemo{
• public static void main(String[] args){
• try{
• InetAddress ip=InetAddress.getByName("www.javatpoint.com");
• System.out.println("Host Name: "+ip.getHostName());
• System.out.println("IP Address: "+ip.getHostAddress());
• }catch(Exception e){System.out.println(e);}
• }
• }
Thank You!