Md13 networking


Published on

Published in: Education, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Md13 networking

  1. 1. Networking
  2. 2. <ul><li> </li></ul><ul><li>Objectives </li></ul><ul><ul><li>• Introduction to TCP/IP and UDP protocol. </li></ul></ul><ul><ul><li>• Internet Addressing </li></ul></ul><ul><ul><li>• Java and Net </li></ul></ul><ul><ul><li>• TCP Socket and ServerSocket </li></ul></ul><ul><ul><li>• URL </li></ul></ul><ul><ul><li>• Comunication with Remote Systems using TCP </li></ul></ul><ul><ul><li>• Writing the Client/Server using TCP </li></ul></ul>
  3. 3. Introduction Computers running on the Internet communicate with each other using TCP or UDP protocols 1. TCP/IP IP stands for Internet protocol, it’s a datagram protocol, which means transmitted packets of information are not guaranteed to be delivered. IP is a connectionless protocol. Since IP packets are never guaranteed to arrive at their destination, a higher level protocol TCP is used to provide a service that guarantees delivery
  4. 4. Application that require a reliable point to point channel to communicate use TCP to communicate. Examples Hypertext Transfer Protocol (HTTP), File Transfer Protocol(FTP) , and Telnet. User Datagram Protocol(UDP) UDP is protocol that send independent packets of data called datagram from one computer to another with no guarantee about the arrival. UDP is not connection based like TCP, rather it sends independent packets of data called datagrams from one application to another.
  5. 5. Sockets Sockets are software interfaces that connect an application to a network. On the Internet each machine has 65536 addressable ports it can use. Server programs listen to these ports for any incoming service request. A client program needs to open a port of its own before it can connect to a server port at the other end. The port numbers range from 0 to 1023 are restricted. They are reserved for use by well known services such as HTTP and FTP
  6. 6. <ul><li>Like </li></ul><ul><ul><ul><ul><li>HTTP  80 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Telnet  23 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>FTP  21 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Echo  7 </li></ul></ul></ul></ul>
  7. 7. Internet Addressing * Every computer on the Internet has an address. * An Internet address is a number that uniquely identifies each computer on the Net. * Originally, all Internet addresses consisted of 32-bit values, also known as IPv4 (Internet Protocol, version 4). * IPv6 uses a 128-bit value to represent an address, so it supports a much larger address space writing, * IPv6 is not supported by all environments.
  8. 8. There are 32 bits in an IPv4 IP address, and we often refer to them as a sequence of four numbers between 0 and 255 separated by dots (.). Domain Naming Service (DNS) * Refer to the addresses as numbers, is difficult, like . * So it is mapped to name that makes it easy to remember.
  9. 9. <ul><li>Java and the Net </li></ul><ul><li>How Java relates to all of these network concepts. </li></ul><ul><ul><li>Java support both TCP/IP and UDP to communicate over the </li></ul></ul><ul><ul><li>Internet. </li></ul></ul><ul><ul><li>URL, URLConnection, Socket and Server Socket classes all </li></ul></ul><ul><ul><li>use the TCP. </li></ul></ul><ul><ul><li>DatagramPacket and DatagramServer classes use UDP to </li></ul></ul><ul><ul><li>communicate over the network. </li></ul></ul>
  10. 10. <ul><li>InetAddress </li></ul><ul><li>* The InetAddress class is used to encapsulate both the </li></ul><ul><li>numerical IP address and the domain name for that address. </li></ul><ul><li>* The InetAddress class has no visible constructors. </li></ul><ul><li>* To create an InetAddress object, use one of the available factory </li></ul><ul><li>methods. </li></ul><ul><li>* Factory methods are merely a convention whereby static methods </li></ul><ul><li>in a class return an instance of that class. </li></ul>
  11. 11. <ul><li>Commonly used InetAddress factory methods </li></ul><ul><li>static InetAddress getLocalHost( ) throws UnknownHostException </li></ul><ul><li>This method simply returns the InetAddress object </li></ul><ul><li>that represents the local host. </li></ul><ul><li>static InetAddress getByName(String hostName ) </li></ul><ul><li>throws UnknownHostException </li></ul><ul><li>This method returns an InetAddress for a host name </li></ul><ul><li>passed to it. </li></ul>
  12. 12. static InetAddress[ ] getAllByName(String hostName) throws UnknownHostException This method returns an array of InetAddresses that represent all of the addresses that a particular name resolves to. Example
  13. 13. TCP Sockets There are two kinds of TCP sockets in Java. One is for servers, and the other is for clients. The ServerSocket class is designed to be a “listener,” which waits for clients to connect before doing anything. The Socket class is designed to connect to server sockets and initiate protocol exchanges. The creation of a Socket object implicitly establishes a connection between the client and server .
  14. 14. Example * Opens a connection to a port on the InterNIC server * Sends the command-line argument down the socket, and then prints the data that is returned * InterNIC will try to look up the argument as a registered Internet domain name, then send back the IP address and contact information for that site.
  15. 15. URL The URL provides a reasonably intelligible form to uniquely identify or address information on the Internet. Every browser uses them to identify information on the Web. Examples of URLs are and A URL specification is based on four components. The first is the protocol to use, separated from the rest of the locator by a colon (:). Common protocols are http, ftp,
  16. 16. The second component is the host name or IP address of the host to use. The third component, the port number, is an optional parameter, The fourth part is the actual file path. Java’s URL class has got methods that gives information about the URL. Example
  17. 17. Communication with Remote Systems using TCP TCP is used to implement reliable, bidirectional, persistent connections between hosts on the Internet. The package provide two classes that allow you to create two kind of sockets. The classes are Socket and ServerSocket. Socket class The Socket class is used to create the client sockets, is designed to connect to server socket and initiate the protocol exchange. When we create the Socket object it implicitly establishes the connection between the client and the server.
  18. 18. It is not possible to get the details of establishing that connection because the Socket class does not provide any method for this purpose. Socket (String hostName, int port) This constructor is used to create a client socket and connect the local host to the specified hostName and port. Socket (InetAddress ipAddress, int port) This constructor is used to create a client socket using a Pre-existing InetAddress object and a port. There should be two ports for all TCP connections: a port on the remote machine a port on local machine through which the client communicates.
  19. 19. ServerSocket class basically used to create the server sockets, is designed to be a listener, which wait for client to connect before doing anything. The ServerSocket class is used to create servers that listen for either Local or remote client to connect to them on standard ports. Constructors in ServerSocket class ServerSocket (int port) throws IOException This constructor is used to create the server socket on the specified port with default queue length of 50 Queue length Each server socket object is associated with queue length attribute which tell the system how many client Connection it can leave pending before it can simply refuse connection
  20. 20. ServerSocket (int port, int queueLength) throws IOException This constructor is used to create a server socket on the Specified port with a specified queue length.
  21. 21. Writing Client/Server using TCP To write the server program use the following steps 1.Create a server socket and begin listening. 2.Call the accept method to get new connection. 3.Create the input and output stream for the returned socket. 4.Conduct the conversation based on agreed protocol. 5.Close the client streams and socket. 6.Go back to step 2 or continue the step 7. 7.Close the server socket
  22. 22. To write the client use the following steps 1.Create the client socket connection. 2.Acquire the read and write streams for the socket. 3.Use the streams according to the server’s protocol. 4.Close the streams. 5.Close the socket.
  23. 23. Communicating with remote systems using UDP UDP cannot guarantee whether the sent or received datagrams reach at their destinations. UDP is mainly useful to broadcast low value information on a frequent basis, so that losing a communication does not effect the service. The package provides two classes namely DatagramPacket And DatagramSocket to communicate with remote systems.
  24. 24. DatagramPacket class is used to implement a connectionless packet delivery service. This class define two constructors DatagramPacket(byte[]buf, int length) This constructor is used for receiving data over a DatagramSocket. DatagramPacket(byte[]buf, int length, InetAddress address, int port) This constructor is used for transmitting datagrams,this requires destination machine address and the port number apart from the buffer and the size parameters.
  25. 25. DatagramSocket class The DatagramSocket class provides the functionality for sending and receiving the datagrams. This class defines three constructors DatagramSocket() Construct a datagram socket and bind it to any available port on the local host machine. DatagramSocket(int port) Construct a datagram socket and bind it to the specified port on the local host machine. DatagramSocket(int port, InetAddress Iaddr) Construct a datagram socket and bind it to the specified local address.
  26. 26. Writing Client/Server System using UDP To write a server using UDP follow the following steps 1.Create the datagram socket on a specific port. 2.Invoke the receive() method to wait for incoming packets. 3.According to the agreed protocol respond to received packets. 4.Repeat step 2 or continue to step 5. 5.Close the datagram socket. Example
  27. 27. To write a Client using UDP follow the following basic steps 1.Create the datagram socket on any available port. 2.Create the address to send data. 3.Send the data according to the Server’s protocol. 4.Wait for incoming data 5.Go back to step 3 or step 4 or go to step 6 6.Close the datagram socket. Example