Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress."— Presentation transcript:

1 By Vivek Dimri

2 Basic Concepts on Networking IP 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

3 Basic Concepts on Networking The 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

4 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

5 Basic Concepts on Networking Made by: Vivek Dimri

6 Clients and servers, Sockets and ServerSockets Made by: Vivek Dimri

7 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

8 The Java Networking Package Made by: Vivek Dimri

9 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

10 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

11 InetAddress: Factory method Example import 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

12 InetAddress: Factory method Example O/P Dimri/192.168.1.2 Yahoo.com/98.139.183.24 www.google.com/74.125.236.18 www.google.com/74.125.236.19 www.google.com/74.125.236.20 www.google.com/74.125.236.16 www.google.com/74.125.236.17 Made by: Vivek Dimri

13 InetAddress: Instance Methods Made by: Vivek Dimri

14 The ServerSocket Class  Provides the basic functionalities of a server  Has four constructors Made by: Vivek Dimri

15 The ServerSocket Class: Methods Made by: Vivek Dimri

16 The ServerSocket Class: Example import 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

17 The ServerSocket Class: Example while(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

18 The Socket Class  Implements a client socket  Has eight constructors – Two of which are already deprecated Made by: Vivek Dimri

19 The Socket Class: Methods Made by: Vivek Dimri

20 The Socket Class: Example import 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

21 The Socket Class: Example System.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

22 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

23 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

24 The MulticastSocket Class: Methods Made by: Vivek Dimri

25 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

26 The DatagramPacket Class  Used to deliver data through a connectionless protocol  Problem: delivery of packets is not guaranteed Made by: Vivek Dimri

27 The DatagramPacket Class: Methods Made by: Vivek Dimri

28 The MulticastSocket Class: Server Example 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

29 The MulticastSocket Class: Server Example /* 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

30 Common well known ports  Ports 20/21File Transfer Protocol  Port 23Telnet  Port 25Simple Mail Transport Proto.  Port 79Finger  Port 80HTTP  Port 110POP3 (Post Office Protocol)  All well known ports in the range 1..1023 Made by: Vivek Dimri


Download ppt "By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress."

Similar presentations


Ads by Google