By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server...
-
Upload
buddy-chase -
Category
Documents
-
view
216 -
download
0
Transcript of By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server...
NETWORKINGBy Vivek Dimri
Basic Concepts on NetworkingIP Address– Protocol– Ports– The Client/Server Paradigm– Sockets
The Java Networking Package– The InetAddress Class– The ServerSocket and the Socket Class– The MulticastSocket and the DatagramPacket
Class
Made by: Vivek Dimri
Basic Concepts on NetworkingThe Internet– A global network of computers
connected together in various ways– Remains functional despite of
diversity of hardware and software connected together Possible through communication
standards defined and conformed to Guarantee compatibility and reliability of
communication
Made by: Vivek Dimri
Basic Concepts on Networking
IP Address Protocols Ports
A 16-bit number that identifies each service offered by a network server
Socket Client Server Paradigm
Made by: Vivek Dimri
Basic Concepts on Networking
Made by: Vivek Dimri
Clients and servers, Sockets and ServerSockets
Made by: Vivek Dimri
The Java Networking Package
The java.net package Provides classes useful for
developing networking applications Some classes in the package:
InetAddress ServerSocket Socket MulticastSocket DatagramPacket
Made by: Vivek Dimri
The Java Networking Package
Made by: Vivek Dimri
InetAddress The InetAddress class is used to
encapsulate both the numerical IP address and the domain name for that address.
The InetAddress class hides the number inside.
InetAddress can handle both IPv4 and IPv6 addresses.
Made by: Vivek Dimri
InetAddress: Factory method The InetAddress class has no visible constructors. To create an InetAddress object, we have to use one
of the available factory methods. Factory methods are merely a convention whereby
static methods in a class return an instance of that class.
Three commonly used InetAddress factory methods are- static InetAddress getLocalHost( ) throws
UnknownHostException static InetAddress getByName(String hostName) throws
UnknownHostException static InetAddress[ ] getAllByName(String hostName) throws
UnknownHostException
Made by: Vivek Dimri
InetAddress: Factory method Exampleimport java.net.*;
class InetAddressTest
{
public static void main(String args[]) throws UnknownHostException {
InetAddress Address = InetAddress.getLocalHost();
System.out.println(Address);
Address = InetAddress.getByName(“yahoo.com");
System.out.println(Address);
InetAddress SW[] = InetAddress.getAllByName("www.google.com");
for (int i=0; i<SW.length; i++)
System.out.println(SW[i]);
}
}
Made by: Vivek Dimri
InetAddress: Factory method Example O/PDimri/192.168.1.2Yahoo.com/98.139.183.24www.google.com/74.125.236.18www.google.com/74.125.236.19www.google.com/74.125.236.20www.google.com/74.125.236.16www.google.com/74.125.236.17
Made by: Vivek Dimri
InetAddress: Instance Methods
Made by: Vivek Dimri
The ServerSocket Class Provides the basic functionalities of a server Has four constructors
Made by: Vivek Dimri
The ServerSocket Class: Methods
Made by: Vivek Dimri
The ServerSocket Class: Exampleimport java.net.*;
import java.io.*;
public class svrskt {
public static void main(String args[ ]) {
ServerSocket server = null;
Socket client;
try {
server = new ServerSocket(1234);
//1234 is an unused port number
} catch (IOException ie) {
System.out.println("Cannot open socket.");
System.exit(1);
}Made by: Vivek Dimri
The ServerSocket Class: Examplewhile(true) {
try {
client = server.accept();
OutputStream clientOut = client.getOutputStream();
PrintWriter pw = new PrintWriter(clientOut, true);
InputStream clientIn = client.getInputStream();
BufferedReader br = new BufferedReader (new InputStreamReader(clientIn));
pw.println(br.readLine());
} catch (IOException ie)
{
}}}
}Made by: Vivek Dimri
The Socket Class Implements a client socket Has eight constructors
– Two of which are already deprecated
Made by: Vivek Dimri
The Socket Class: Methods
Made by: Vivek Dimri
The Socket Class: Exampleimport java.net.*;
public class svr {
public static void main(String args[]) {
try {
/* Socket client = new Socket("192.168.1.2“, 1234); */
Socket client = new Socket(InetAddress.getLocalHost(), 1234);
InputStream clientIn = client.getInputStream();
OutputStream clientOut = client.getOutputStream();
PrintWriter pw = new PrintWriter(clientOut, true);
BufferedReader br = new BufferedReader(new InputStreamReader(clientIn));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
Made by: Vivek Dimri
The Socket Class: ExampleSystem.out.println("Type a message for the server: ");
pw.println(stdIn.readLine());
System.out.println("Server message: ");
System.out.println(br.readLine());
pw.close();
br.close();
client.close();
}
catch (ConnectException ce)
{ System.out.println("Cannot connect to the server.");
}
catch (IOException ie)
{ System.out.println("I/O Error.");
} } }Made by: Vivek Dimri
Output:
After executing both program simultaneously we will get following output-
Type a message for Server:
Hello Dear how r u?
Server Message:
Hello Dear how r u?
Made by: Vivek Dimri
The MulticastSocket Class Useful for applications that implement group
communication IP addresses for a multicast group lies within the
range 224.0.0.0 to 239.255.255.255
– Address 224.0.0.0 is reserved and should not be used
Made by: Vivek Dimri
The MulticastSocket Class: Methods
Made by: Vivek Dimri
The MulticastSocket Class
Sending a message to a group Should be a member of the multicast group
by using the joinGroup method Use the send method Once done, can use the leaveGroup method
The send method Need to pass a DatagramPacket object
Made by: Vivek Dimri
The DatagramPacket Class Used to deliver data through a connectionless
protocol Problem: delivery of packets is not guaranteed
Made by: Vivek Dimri
The DatagramPacket Class: Methods
Made by: Vivek Dimri
The MulticastSocket Class: ServerExample
import java.net.*;
public class ChatServer
{
public static void main(String args[]) throws Exception
{
MulticastSocket server = new MulticastSocket(1234);
InetAddress group = InetAddress.getByName("234.5.6.7");
//getByName- returns IP address of given host
server.joinGroup(group);
boolean infinite = true;
Made by: Vivek Dimri
The MulticastSocket Class: ServerExample
/* Continually receives data and prints them */
while(infinite) {
byte buf[] = new byte[1024];
DatagramPacket data =
new DatagramPacket(buf, buf.length);
server.receive(data);
String msg = new String(data.getData()).trim();
System.out.println(msg);
}
server.close();
}
}Made by: Vivek Dimri
Common well known ports
Ports 20/21 File Transfer Protocol Port 23 Telnet Port 25 Simple Mail Transport Proto. Port 79 Finger Port 80 HTTP Port 110 POP3 (Post Office Protocol) All well known ports in the range 1..1023
Made by: Vivek Dimri