Ajp 4th Unit
Ajp 4th Unit
Networking Basics
Advanced java programming
INTRODUCTION
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.
1. sharing resources
2. centralize software management
3. Communication Facility
4. Backups and Fail over
1. IP Address
2. Protocol
3. Port Number
4. MAC Address
5. Connection-oriented and connection-less protocol
6. Socket
1) IP Address
2) Protocol
A protocol is a set of rules basically that is followed for communication. For example:
● TCP
● FTP
The File Transfer Protocol (FTP) is a standard network protocol used for the
transfer of computer files between a client and server on a computer network.
FTP is built on a client-server model architecture using separate control and data
connections between the client and the server.
● Telnet
● SMTP
● POP etc.
The post office protocol (POP) is the most commonly used message request
protocol in the Internet world for transferring messages from an e-mail server to
an e-mail client.
3) Port Number
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 a unique MAC.
6) Socket
java.net package
The java.net package provides many classes to deal with networking applications in
Java.
Java URL
The Java URL class represents an URL. URL is an acronym for Uniform Resource
Locator. It points to a resource on the World Wide Web. For example:
1. https://www.javatpoint.com/java-tutorial
Method Description
//URLDemo.java
import java.net.*;
try{
System.out.println("Protocol: "+url.getProtocol());
}
catch(Exception e)
{System.out.println(e);
} Output:
Protocol: http
Port Number: -1
The Java URLConnection class represents a communication link between the URL
and the application. This class can be used to read and write data to the specified
resource referred by the URL.
The openConnection() method of URL class returns the object of URLConnection class.
Syntax:
import java.io.*;
import java.net.*;
try{
URL url=new URL("http://www.javatpoint.com/java-tutorial");
URLConnection urlcon=url.openConnection();
InputStream stream=urlcon.getInputStream();
int i;
while((i=stream.read())!=-1){
System.out.print((char)i);
}catch(Exception e){System.out.println(e);}
import java.io.*;
import java.net.*;
try{
InetAddress ip=InetAddress.getByName("www.javatpoint.com");
catch(Exception e)
System.out.println(e);
Output:
IP Address: 206.51.231.148
Factory Methods
The InetAddress class has no visible constructors. To create an InetAddress object,you have to
use one of the available factory methods. Factory methods are merely aconvention whereby
static methods in a class return an instance of that class.This is done in lieu of overloading a
constructor with various parameter lists whenhaving unique method names makes the results
much clearer.
throws UnknownHostException
throws UnknownHostException
throws UnknownHostException
The getLocalHost( ) method simply returns the InetAddress object that representsthe local
host.
The getByName( ) method returns an InetAddress for a host namepassed to it. If these
methods are unable to resolve the host name, they throw anUnknownHostException.
In the world of web servers, this is one way to provide some degree ofscaling. The
getAllByName( ) factory method returns an array of InetAddresses that represent all of the
addresses that a particular name resolves to. It will also throw anUnknownHostException if it
can't resolve the name to at least one address.
Java 2, version 1.4 also includes the factory method getByAddress( ), which takes an
IPaddress and returns an InetAddress object. Either an IPv4 or an IPv6 address can be used.The
following example prints the addresses and names of the local machine and two well-known
Internet web sites:
Java Socket programming is used for communication between the applications running
on different JRE.
Socket and ServerSocket classes are used for connection-oriented socket programming
and DatagramSocket and DatagramPacket classes are used for connection-less socket
programming.
Here, we are going to make one-way client and server communication. In this
application, client sends a message to the server, server reads the message and prints
it. Here, two classes are being used: Socket and ServerSocket. The Socket class is
used to communicate client and server. Through this class, we can read and write
message. The ServerSocket class is used at server-side. The accept() method of
ServerSocket class blocks the console until the client is connected. After the successful
connection of client, it returns the instance of Socket at server-side.
Socket class
A socket is simply an endpoint for communications between the machines. The Socket
class can be used to create a socket.
IMPORTANT METHODS
Method Description
1) public InputStream getInputStream() returns the InputStream attached with this socket.
2) public OutputStream getOutputStream() returns the OutputStream attached with this socket.
ServerSocket class
The ServerSocket class can be used to create a server socket. This object is used to establish
communication with the clients.
Important methods
Method Description
1) public Socket accept() returns the socket and establish a connection between
Creating Server:
To create the server application, we need to create the instance of ServerSocket class. Here, we are
using 6666 port number for the communication between the client and server. You may also choose any
other port number. The accept() method waits for the client. If clients connects with the given port number,
it returns an instance of Socket.
Creating Client: To create the client application, we need to create the instance of Socket class. Here,
we need to pass the IP address or hostname of the Server and a port number. Here, we are using
"localhost" because our server is running on same system.
DatagramPacket
Java DatagramSocket and DatagramPacket classes are used for connection-less socket programming.
A datagram is basically an information but there is no guarantee of its content, arrival or arrival time.
● DatagramPacket(byte[] barr, int length): it creates a datagram packet. This constructor is used
to receive the packets.
● DatagramPacket(byte[] barr, int length, InetAddress address, int port): it creates a datagram
packet. This constructor is used to send the packets.
Video link:
https://www.youtube.com/watch?v=DuFyhu5_GPs