by vivek dimri. basic concepts on networking ip address – protocol – ports – the client/server...

30
NETWORKING By Vivek Dimri

Upload: buddy-chase

Post on 19-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

NETWORKINGBy Vivek Dimri

Page 2: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 3: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 4: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 5: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

Basic Concepts on Networking

Made by: Vivek Dimri

Page 6: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

Clients and servers, Sockets and ServerSockets

Made by: Vivek Dimri

Page 7: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 8: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

The Java Networking Package

Made by: Vivek Dimri

Page 9: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 10: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 11: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 12: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 13: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

InetAddress: Instance Methods

Made by: Vivek Dimri

Page 14: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

The ServerSocket Class Provides the basic functionalities of a server Has four constructors

Made by: Vivek Dimri

Page 15: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

The ServerSocket Class: Methods

Made by: Vivek Dimri

Page 16: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 17: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 18: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

The Socket Class Implements a client socket Has eight constructors

– Two of which are already deprecated

Made by: Vivek Dimri

Page 19: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

The Socket Class: Methods

Made by: Vivek Dimri

Page 20: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 21: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 22: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 23: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 24: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

The MulticastSocket Class: Methods

Made by: Vivek Dimri

Page 25: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 26: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

The DatagramPacket Class Used to deliver data through a connectionless

protocol Problem: delivery of packets is not guaranteed

Made by: Vivek Dimri

Page 27: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

The DatagramPacket Class: Methods

Made by: Vivek Dimri

Page 28: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 29: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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

Page 30: By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress

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