Unit VIII Networking&JavaLibrary
Unit VIII Networking&JavaLibrary
Protocol: A protocol is a set of rules and conventions used in order to have information
exchange over the network.
Socket: In order to connect to a target system we need a new communication end point
called socket. A socket is a plug-in application.
Socket : In order to connect to a target system we need a new communication end point
called socket.
A socket is a plug-in application.
Each socket has a socket number.
A numbered socket is called a port.
The port numbers below 256 are called well-know ports or reserved ports.
For example
Application name port number
FTP 21
TELNET 23
HTTP 80
SMTP 25
POP 110
Port number: The number given to each and every device, all connected to the system,
in order to find out the device interacting with it.
DNS (Domain Name Service) : 1) Instead of addressing a system with a number, we can
also specify the computer hierarchy with DNS.
2)IP Address describes a network hierarchy from left to right and the domain name
specifies the hierarchy from right to left.
The name of an internet address called its domain name – describes a machine location.
InetAddress: This class has no public constructors in order to create an object of
InetAddress class we use its factory method.
Factory Method: A static method which returns the instance of a class is called factory
method.
Socket class :
Constructor
Socket(String target, int portNo)
target – Ip address or DNS
ServerSocket Class :
Constructor –
ServerSocket(int portNum)
TCP/IP Socket-
Ex: Program
import java.io.*;
import java.net.*;
class Client{
public static void main(String args[]){
Socket s= new Socket(“node1”, 1234);
DataInputStream dis = new DataInputStream(System.in);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
String line = dis.readLine();
dout.writeBytes(line + “\n”);
dout.close();
dis.close();
s.close();
}
}
class Server{
public static void main(String args[]){
ServerSocket ss = new ServerSocket(1234);
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println(dis.readLine());
dis.close();
s.close();
ss.close();
}
}
Sender –
DatagramPacket class-
Constructor-
DatagramPacket(byte b[], int size, InetAddress target, int portNo)
DatagramSocket class-
Constructor-
DatagramSocket()
Method-
public void send(DatagramPacket dp)
Receiver –
DatagramPacket class-
Constructor-
DatagramPacket(byte b[], int size)
DatagramSocket class-
Constructor-
DatagramSocket( int portNo)
Method-
public void receive(DatagramPacket dp)
class Sender{
public static void main(String[] args){
String msg = “Hello Receiver”;
byte b[] = msg.getBytes();
InetAddress target = InetAddress.getLocalHost();
DatagramPacket dp = new DatagramPacket( b, b.length, target, 1234);
DatagramSocket ds= new DatagramSocket();
ds.send(dp);
}
}
class Receiver{
public static void main(String[] args){
byte b[] = new byte[100];
DatagramPacket dp = new DatagramPacket( b, b.length);
DatagramSocket ds= new DatagramSocket(1234);
ds.receive(dp);
String s = new String(b);
System.out.println(s);
}
}
MultiCastSockets:
The multicast addres range – 224.0.0.0
.
.
239.255.255.255
The multicast address is a logical address not physical address.
Ex Program:
import java.net.*;
import java.io.*;
class MultiCastSender{
public static void main(String[] args){
InetAddress target = InetAddress.getByName(“244.10.10.10”);
String s = “Hi receiver”;
byte b[] = s.getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length, target, 1234);
MulticastSocket m = new MultiCastSocket();
m.send(dp);
}
}
class MultiCastReceiver{
public static void main(String[] args){
InetAddress target = InetAddress.getByName(“244.10.10.10”);
byte b[] = new byte[30];
DatagramPacket dp = new DatagramPacket(b, b.length);
MulticastSocket m = new MultiCastSocket(1234);
m.joinGroup(target);
m.receive(dp);
String msg = new String(b);
System.out.println(msg);
}
}
URL (Unified Resource Locator)- Used to contact any existing server in the network.
This works better for hhtp web server and gives the information about the default files ,
content type, content size etc.
URL class-
URL urlObject = new URL(“www.google.com”);
Methods –
public int getPort()
public String getProtocol()
public String getFileName()
public String getContentType()
public int getContentSize()
public URLConnection openConnection()
URLConnection class –
Methods –
public InputStream getInputStream()
public OutputStream getOutputStream()
Ex: Program to contact yahoo site, retrieve data and write in the local hard drive.
Class Download{
public static void main(String args[]){
URL url = new URL(“www.yahoo.com”);
URLConnection con = u.openConnection();
RandomAccessFile rf = new RandomAccessFile(con.getFileName(),
“rw”);
BufferedInputStream bin = new BufferedInputStream(
con.getInputStream());
DataInputStream dis = new DataInputStream(bin);
String line = dis.readLine();
while(line != null){
rf.writeBytes(line + “\n”);
line = dis.readLine();
}
rf.close();
bin.close();
dis.close();
}
}