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

TCP IP

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