Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Prepared by: Eng/ Basma Elshoky

Section 2: Introduction to Java Network Programming

Network programming involves writing programs that communicate with other


programs across a computer network. In Java, network programming allows you to
create applications that can communicate over the internet or local networks using
various protocols such as TCP/IP and UDP.

Advantages of Java Networking:


• Implementing networking protocols
• Implement socket programming
• Creating server-client applications
• Creating web services

What is Java Networking API?

Java provides a comprehensive set of classes and interfaces in the java.net package
for network communication.
Some key classes and interfaces such as:
• InetAddress: handle Internet addresses both as host names and as IP
addresses
• Socket: Represents an endpoint for communication between two machines
over the network.
Prepared by: Eng/ Basma Elshoky

• ServerSocket: Listens for incoming connections on a specific port on the


server.
• Input/Output Streams: Used for reading from and writing to the network.
• URL: Represents a Uniform Resource Locator, used to access resources on
the internet.
• URLConnection: Abstract class representing a connection to a URL.
• DatagramSocket: Provides support for connectionless, unreliable
communication using UDP (User Datagram Protocol).
• DatagramPacket: Represents a packet of data to be sent or received.

What is Java Sockets?


Java sockets are essential for network communication, acting as endpoints for
sending and receiving data. A socket is created on a client device and attempts to
connect to a server socket at a specified IP address and port number.

IP Address and Port


Each computer on the Internet has a unique IP address, the current version of which
is IPv4 (Internet Protocol version 4), it is made up of four eight-bit numbers (i.e.,
numbers in the decimal range 0-255) such as 131.122.3.219. The protocol used for
such communication is called the Internet Protocol (IP). The Internet is the world's
largest IP-based network.
Ports allow different applications on the same machine to communicate
simultaneously. A port is a logical connection to a computer (as opposed to a physical
connection) and is identified by a number in the range 1-65535. Such as HTTP
Protocol, which refer to HyperText Transfer Protocol (the World Wide Web protocol)
has Port 80.

The InetAddress Class


One of the classes within package java.net is called InetAddress, which handles
Internet addresses both as host names and as IP addresses.
Prepared by: Eng/ Basma Elshoky

Java InetAddress Class Methods

Method Action Performed

equals() Returns true if this IP address is the same as that of the object specified.
Equals() method don’t consider hostnames while comparing and only
consider IP address associated.

getAddress() Returns the raw IP address of this InetAddress object as an array. The
order in which bytes appear in an array is the same as in IP address i.e.
getAddress[0] will contain the highest order byte.

getByAddress() Create an InetAddress object. It takes the hostname and IP address as its
parameter. The hostname can be the machine name as in
“www.geeksforgeeks.org” or its textual IP address.

getByName() Returns the IP Address of the host specified. If the host is a literal IP
address, then only its validity is checked.

getAllByName() Returns an array of IP addresses for the given host

getLoopbackAddress() Returns the loopback address

getHostAddress() Returns IP address in textual form.

getHostName() Returns the hostname for this IP Address. If this object was created with
a hostname then it is returned, otherwise, a reverse lookup is performed
to return the system configured hostname.

getLocalHost() Returns the IP address of the local host.

getCanonicalHostName() Returns the fully qualified domain name for this object. If this object was
created with a hostname then it is returned, otherwise, a reverse lookup is
performed to return the system configured hostname.

hashCode() Returns the hashcode associated with this address object.

isAnyLocalAddress() Returns true if this address represents a local address.


Prepared by: Eng/ Basma Elshoky

Method Action Performed

isLinkLocalAddress() Returns true if this address is a link-local address.

isLoopbackAddress() Returns true if this address is a loopback address.

isMCGlobal() Returns true if this multicast address has global scope.

isMCLinkLocal() Returns true if this multicast address has link scope.

isMCNodeLocal() Returns true if this multicast address has node scope.

isMCOrgLocal() Returns true if this multicast address has organization scope.

isMCSiteLocal() Returns true if this multicast address has site scope.

isMulticastAddress() Returns true if this address is an IP multicast address. Multicast


addresses have 1110 as their first 4 bits.

isReachable() Returns true if this address is reachable. ICMP echo requests are used if
permission can be granted otherwise the host tries to make a TCP
connection at port 7 of the destination. This method is used generally as a
pre-condition in various programs, to avoid Host Unreachable exceptions
in the future

isReachable() Specify the network interface to be used while checking for reachability
and the ttl parameter specifies the number of hops the echo packet makes
before exiting the network.

isSiteLocalAddress() Returns true if this address is a site-local address.

toString() Converts the IP address to the string. It returns the result as hostname / IP
address.
Prepared by: Eng/ Basma Elshoky

Example: Find IP address for domain name.


import java.net.*;
import java.util.*;
public class App {
public static void main(String[] args) throws Exception {
{
String host;
Scanner input = new Scanner(System.in);
System.out.print("\n\nEnter host name: ");
host = input.next();
try {
InetAddress address =
InetAddress.getByName(host);
System.out.println(("\n IP address: "
+ address.toString());
}
catch (UnknownHostException uhEx)
{
System.out.println("Could not find " + host);
} }}}

Example 2: Get the IP address of the current machine.


import java.net.*;
public class App
{
public static void main(String[] args)
{
try {
InetAddress address = InetAddress.getLocalHost();
System.out.println(address); }
catch (UnknownHostException uhEx)
{
System.out.println("Could not find local address!"); } } }

***Next Section will discuss socket class and it is method with example.

Task:
Get the IP of your university “BTU website” then apply10 methods from the table.

You might also like