Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

TCP IP

2,441 views

Published on

Published in: Technology, Business

TCP IP

  1. 1. TCP/IP Suite
  2. 2. A Protocol Suite, such as TCP/IP, is the combination of different protocols at various layers
  3. 3. APSTNDP - OSI model Application, Transport, Network, Link= TCP/IP Protocol Suite
  4. 4. Layer Functionality <ul><li>The Link Layer : Device driver to the Network Interface card and other OS related </li></ul><ul><li>The Network Layer : Movement of Packets around the Network, IP, ICMP,IGMP </li></ul><ul><li>The Transport Layer : Flow of data between two hosts : TCP and UDP </li></ul><ul><li>Application Layer : Handles details of particular application : RTCP,RTP,FTP etc., </li></ul>
  5. 5. Data at different layers <ul><li>TCP Segment : Data at the TCP level </li></ul><ul><li>IP Datagram: Data at the IP level </li></ul><ul><li>Ethernet Frame: Data at the Ethernet level </li></ul><ul><li>Packet: Data at all layers </li></ul>
  6. 6. TCP and UDP <ul><li>TCP : Provides a reliable flow of data between two hosts by timeouts, retransmission and acknowledgement </li></ul><ul><li>UDP : Sends packets of data called datagram from one host to the other, without guarantee </li></ul>
  7. 7. Routers,Bridges,etc., <ul><li>Multihomed : Any System with multiple interfaces is called multihomed. </li></ul><ul><li>Router: A Multihomed host can function as a router, if it forwards packets from one interface to the other. </li></ul><ul><li>Bridges: Bridges connect networks at the Network layer </li></ul><ul><li>loopback address is 127.0.0.1 is in loopback interface allows clients and server to connect without sending data to the network </li></ul>
  8. 8. Classes of Networks
  9. 9. Classes of Networks <ul><li>Class A - 0.0.0.0 to 127.255.255.255 (7 bits) </li></ul><ul><li>Class B - 14 bits 128... to 191.. </li></ul><ul><li>Class C - 21 bits 192.. to 223.. </li></ul><ul><li>Class D - 28 bits 224.. to 239.. </li></ul><ul><li>Class E - 240.. to 247.. </li></ul>
  10. 10. Sockets
  11. 11. TCP State Transitions
  12. 12. ICMP <ul><li>ICMP messages are usually considered part of the IP layer </li></ul><ul><li>ICMP messages are transmitted within IP datagrams </li></ul><ul><li>IP Datagram comprises of an IP Header and an ICMP message </li></ul><ul><li>Ping sends an ICMP Echo Request expecting an ICMP Echo Reply </li></ul>
  13. 13. ARP <ul><li>When two hosts are on the same network and one desires to send a packet to the other </li></ul><ul><li>When two hosts are on different networks and must use a gateway/router to reach the other host </li></ul><ul><li>When a router needs to forward a packet for one host through another router </li></ul><ul><li>When a router needs to forward a packet from one host to the destination host on the same network </li></ul>
  14. 14. Socket Options - I <ul><li>TCP_NODELAY </li></ul><ul><li>o Disable Nagle's algorithm. </li></ul><ul><li>o Valid for (client) Sockets. </li></ul><ul><li>SO_LINGER </li></ul><ul><li>o Specify a linger-on-close timeout. </li></ul><ul><li>o Valid for (client) Sockets. </li></ul><ul><li>SO_TIMEOUT </li></ul><ul><li>o Specify a timeout on blocking socket operations. (Don't block forever! </li></ul><ul><li>o Valid for all sockets: Socket, ServerSocket, DatagramSocket. </li></ul><ul><li>SO_BINDADDR </li></ul><ul><li>o Fetch the local address binding of a socket. </li></ul><ul><li>o Valid for Socket, ServerSocket, DatagramSocket. </li></ul><ul><li>SO_REUSEADDR </li></ul><ul><li>o Enable reuse address for a socket. </li></ul><ul><li>o Valid for Socket, ServerSocket, DatagramSocket. </li></ul><ul><li>SO_BROADCAST </li></ul><ul><li>o Enables a socket to send broadcast messages. </li></ul><ul><li>o Valid for DatagramSocket. </li></ul>
  15. 15. Socket Options - II <ul><li>* SO_SNDBUF </li></ul><ul><li>o Set a hint the size of the underlying buffers for outgoing network I/O. </li></ul><ul><li>o Valid for all sockets: Socket, ServerSocket, DatagramSocket. </li></ul><ul><li>* SO_RCVBUF </li></ul><ul><li>o Get the size of the buffer actually used by the platform when receiving in data on this socket. </li></ul><ul><li>o Valid for all sockets: Socket, ServerSocket, DatagramSocket. </li></ul><ul><li>* SO_KEEPALIVE </li></ul><ul><li>o Turn on socket keepalive. </li></ul><ul><li>o Valid for Socket. </li></ul><ul><li>* SO_OOBINLINE </li></ul><ul><li>o Enable inline reception of TCP urgent data. </li></ul><ul><li>o Valid for Socket. </li></ul><ul><li>* IP_MULTICAST_IF </li></ul><ul><li>o Specify the outgoing interface for multicast packets (on multihomed hosts). </li></ul><ul><li>o Valid for MulticastSockets. </li></ul><ul><li>* IP_MULTICAST_LOOP </li></ul><ul><li>o Enables or disables local loopback of multicast datagrams. </li></ul><ul><li>o Valid for MulticastSocket. </li></ul><ul><li>* IP_TOS </li></ul><ul><li>o Sets the type-of-service or traffic class field in the IP header for a TCP or UDP socket. </li></ul><ul><li>o Valid for Socket, DatagramSocket </li></ul>
  16. 16. Client Port Numbers <ul><li>Client Port numbers are ephemeral ports. For instance, to connect to a server port 554, the client has another port. </li></ul>
  17. 17. Server <ul><li>import java.net.*; </li></ul><ul><li>import java.io.*; </li></ul><ul><li>public class Server </li></ul><ul><li>{ </li></ul><ul><li>public static void main(String[] ar) </li></ul><ul><li>{ </li></ul><ul><li>int port = 6666; </li></ul><ul><li> try </li></ul><ul><li>{ </li></ul><ul><li>ServerSocket ss = new ServerSocket(port); </li></ul><ul><li>Socket socket = ss.accept(); </li></ul><ul><li>InputStream sin = socket.getInputStream(); </li></ul><ul><li>OutputStream sout = socket.getOutputStream(); </li></ul><ul><li>DataInputStream in = new DataInputStream(sin); </li></ul><ul><li>DataOutputStream out = new DataOutputStream(sout); </li></ul><ul><li>String line = null; </li></ul><ul><li>while(true) </li></ul><ul><li>{ </li></ul><ul><li>line = in.readUTF(); </li></ul><ul><li>System.out.println(line); </li></ul><ul><li>out.writeUTF(line); </li></ul><ul><li>out.flush(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>catch(Exception x) </li></ul><ul><li>{ </li></ul><ul><li>x.printStackTrace(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  18. 18. Client <ul><li>import java.net.*; </li></ul><ul><li>import java.io.*; </li></ul><ul><li>public class Client </li></ul><ul><li>{ </li></ul><ul><li>public static void main(String[] ar) </li></ul><ul><li>{ </li></ul><ul><li>int serverPort = 6666; </li></ul><ul><li>String address = &quot;127.0.0.1&quot;; </li></ul><ul><li>try </li></ul><ul><li>{ </li></ul><ul><li>InetAddress ipAddress = InetAddress.getByName(address); </li></ul><ul><li>Socket socket = new Socket(ipAddress, serverPort); </li></ul><ul><li>InputStream sin = socket.getInputStream(); </li></ul><ul><li>OutputStream sout = socket.getOutputStream(); </li></ul><ul><li>DataInputStream in = new DataInputStream(sin); </li></ul><ul><li>DataOutputStream out = new DataOutputStream(sout); </li></ul><ul><li>String line = &quot;something&quot;; </li></ul><ul><li>out.writeUTF(line); </li></ul><ul><li>out.flush(); </li></ul><ul><li>line = in.readUTF(); // wait for the server to send a line </li></ul><ul><li>System.out.println(line); </li></ul><ul><li>} </li></ul><ul><li> catch(UnknownHostException x) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(&quot;Got an unknown host exception&quot;); </li></ul><ul><li>} </li></ul><ul><li>catch(ConnectException x) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(&quot;Could not connect to the port&quot;); </li></ul><ul><li>} </li></ul><ul><li>catch(Exception x) </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  19. 19. Multicast Receiver <ul><li>import java.net.*; </li></ul><ul><li>import java.io.*; </li></ul><ul><li>public class MulticastReceiver </li></ul><ul><li>{ </li></ul><ul><li>public static void main( String[] argv ) </li></ul><ul><li>{ </li></ul><ul><li>try </li></ul><ul><li>{ </li></ul><ul><li>// get the InetAddress of the MCAST group </li></ul><ul><li>InetAddress ia = InetAddress.getByName( argv[0] ); </li></ul><ul><li>// get the port that we will be listening on </li></ul><ul><li>int port = Integer.parseInt( argv[1] ); </li></ul><ul><li>// create a multicast socket on the specified local port number </li></ul><ul><li>MulticastSocket ms = new MulticastSocket( port ); </li></ul><ul><li>// create an empty datagram packet </li></ul><ul><li>DatagramPacket dp = new DatagramPacket(new byte[128], 128); </li></ul><ul><li>//Join a multicast group and wait for some action </li></ul><ul><li>ms.joinGroup(ia); </li></ul><ul><li>System.out.println( &quot;waiting for a packet from &quot;+ia+&quot;...&quot;); </li></ul><ul><li>ms.receive(dp); </li></ul><ul><li>// print out what we received and quit </li></ul><ul><li>System.out.println( new String(dp.getData() )); </li></ul><ul><li>ms.leaveGroup(ia); </li></ul><ul><li>ms.close(); </li></ul><ul><li>} </li></ul><ul><li>catch (IOException e) {} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  20. 20. Multicast Sender <ul><li>import java.net.*; </li></ul><ul><li>import java.io.*; </li></ul><ul><li>public class MulticastSender </li></ul><ul><li>{ </li></ul><ul><li>public static void main( String[] argv ) </li></ul><ul><li>{ </li></ul><ul><li>try </li></ul><ul><li>{ </li></ul><ul><li>// get the InetAddress of the MCAST group </li></ul><ul><li>InetAddress ia = InetAddress.getByName( argv[0] ); </li></ul><ul><li>// get the port that the MCAST group members will be listening on </li></ul><ul><li>int recvPort = Integer.parseInt( argv[1] ); </li></ul><ul><li>// create a datagram with a suitable message </li></ul><ul><li>String str = &quot;Hello from: &quot;+InetAddress.getLocalHost(); </li></ul><ul><li>byte[] data = str.getBytes(); </li></ul><ul><li>DatagramPacket dp = new DatagramPacket(data, data.length, ia, recvPort); </li></ul><ul><li>// create a multicast socket bound to any local port </li></ul><ul><li>MulticastSocket ms = new MulticastSocket(); </li></ul><ul><li>//Join the multicast group </li></ul><ul><li>ms.joinGroup(ia); </li></ul><ul><li>// send the message with a Time-To-Live (TTL)=1 </li></ul><ul><li>ms.send(dp, (byte)1); </li></ul><ul><li>// tidy up - leave the group and close the socket </li></ul><ul><li>ms.leaveGroup(ia); </li></ul><ul><li>ms.close(); </li></ul><ul><li>} </li></ul><ul><li>catch (IOException e) {} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>

×