SlideShare a Scribd company logo
1 of 155
TCP/IP

Simple introduction to protocols and
            programming
Plan
• We discuss both protocols and how to program them.
  You cannot program a protocol if you don’t know how
  it works.
• Programming will be explained from application
  developer’s point of view. Protocol implementors and
  tool developers need some specific knowledge
• Programming is explained using Java, but principles are
  applicable to most other languages
• Java APIs are redundant, anyway we discuss all of them
• Tools and troubleshooting are included
UDP
• One of the simplest protocols ever
• Java API reflects that simplicity
Code for sender
byte[] data = "Hello!".getBytes();

InetSocketAddress remote = new InetSocketAddress("10.50.3.70", 7767);

DatagramPacket packet = new DatagramPacket(data, data.length, remote);

DatagramSocket socket = new DatagramSocket();

socket.send(packet);
Code for receiver
InetSocketAddress local = new InetSocketAddress("10.50.3.70", 7767);

DatagramSocket socket = new DatagramSocket(local);

byte[] buffer = new byte[100];

DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

socket.receive(packet);

String data = new String(packet.getData(), packet.getOffset(), packet.getLength());

System.out.println("From " + packet.getSocketAddress() + " received " + data);
Programming model: blocking IPC
                             Receiving
                       OS   application

                                application
                                thread is
                                running
Programming model: blocking IPC
                               Receiving
                       OS     application




                            receive() is a
                            “system call” from
                            application to OS
Programming model: blocking IPC
                               Receiving
                       OS     application




                            receive() is a
                            “system call” from
                            application to OS. It
                            makes OS to block
                            application thread
                            until data are
                            available
Programming model: blocking IPC
   Sending                    Receiving
  application   OS     OS    application


application
  thread is
   running
                            Application
                            thread blocked
Programming model: blocking IPC
    Sending                           Receiving
   application   OS            OS    application




                      UDP           Application
  send() is a
                      packet        thread blocked
 system call
Programming model: blocking IPC
        Sending                                        Receiving
       application   OS                         OS    application




                                  UDP                Application
      send() is a                                    thread blocked
 system call, but                 packet
it doesn’t block
     application
          thread
                          Packet is delivered
                          from OS to OS
Programming model: blocking IPC
   Sending                           Receiving
  application   OS            OS    application




                     UDP           Incoming packet
                                   makes OS to resume
                     packet        application thread
                                   and return from
                                   system call
application
  thread is
   running
Programming model: blocking IPC
   Sending                   Receiving
  application   OS     OS   application




application
  thread is
                                application
   running
                                thread is
                                running
Programming model: blocking IPC
    Sending                            Receiving
   application   OS            OS     application




                      UDP           receive() is a
   send() is a
                      packet        blocking system call.
non-blocking
  system call                       It returns when data
                                    are received
Party identification
• IP address used by underlying IP layer
• Four bytes, usually written as dot-separated
  decimal numbers: “10.50.3.70”
• InetAddress
• UDP port: integer between 0 and 65535
• InetSocketAddress = InetAddress + port
Protocol’s stack
          Remote IP address, remote port and payload are provided by              Application
          application when it makes a system call

           Remote IP/port                          Payload

OS/Application boundary                                        Local IP/port         UDP
                                                           Local IP address and
                                                           port are bound to
                                                           socket

                                                                                      IP



                                                                                   Ethernet
Protocol’s stack
                                                           Application

          Remote IP/port         Payload

                                           Local IP/port      UDP

•Destination port          UDP   Payload
•Source port
•Checksum
                                                               IP



                                                            Ethernet
Protocol’s stack
                                                                  Application

          Remote IP/port           Payload

                                             Local IP/port           UDP

•Dest IP address             UDP   Payload
•Source IP address
                                         IP address is resolved
•Protocol ID (UDP=17)                    into MAC address             IP

                        IP   UDP   Payload

                                                                   Ethernet
Protocol’s stack
                                                                   Application

                                    Payload

                                                                      UDP

•Source MAC address           UDP   Payload
•Dest MAC address
•EtherType (IP=0x0800)
                                                                       IP

                         IP   UDP   Payload

                                       OS invokes driver to send
                                       data through the wire
                                                                    Ethernet

                  Eth    IP   UDP   Payload             Eth
Protocol’s stack
Application                                            Application

                                       Payload
              Dispatch to
   UDP        applications,                               UDP
              buffering

                                 UDP   Payload
              IP packet is
    IP        dispatched to                                IP
              UDP layer
                          IP     UDP   Payload
              Ethernet packet
 Ethernet     is dispatched to                          Ethernet
              IP layer
                   Eth    IP     UDP   Payload   Eth
Sniffer
• Sniffer is a tool which intercepts packets at
  link level
Communication failure
  Sending
 application   OS                      OS


                                            Global failure:
                                            no receiver
                                            process

                       UDP
    send()
                       packet
 completes
  normally




                    ICMP message:
                    port unreachable
Local address and binding
• Sockets which were not bound will choose
  their address and port automatically
• Failure: can’t bind if host:port are occupied
• Special address: any local address
Closing sockets
•   Sockets consume OS resources
•   Closed automatically when process dies
•   Nothing is sent when socket is closed
•   Sockets can’t be re-used after they are
    closed, attempt to use will result in
    IOException
Bi-directional communication
• Receive-reply example
• Receiver replies to the sender
• The same socket is re-used by both sides, once
  for sending, and once for receiving
• Receiver can work in a loop. Each sender has
  only one transaction at time
Multi-party communication
• One DatagramSocket can send and receive
  packets to any address
• Receive twice and send twice examples
Buffering
• Send 100 messages in a loop
Buffering                Application
 Application   OS   OS

                             Thread is
                             running
Buffering                                            Application
 Application   OS                               OS
                      OS buffers data until
                     next receive() operation
                                                         Thread is
                                                         running
  send()            UDP
 returns
Buffering                                            Application
 Application   OS                               OS



                                                         Thread is
                                                         running



  send()
 returns
                    UDP

                          OS adds new data to
                           buffer, preserving
                              boundaries
Buffering                      Application
 Application   OS         OS



                                   Thread is
                                   running




  send()
 returns
                    UDP
Buffering                Application
 Application   OS   OS



                             Thread is
                             running




                             receive()
                             doesn’t
                             block and
                             returns first
                             packet from
                             buffer
Buffering                Application
 Application   OS   OS




                            receive()
                            returns
                            second packet
                            from buffer
Buffering                                             Application
 Application   OS                                OS
                       OS buffers data until
                      next receive() operation
                                                          Thread is
                                                          running
  send()            UDP
 returns


                    UDP

                                                          receive()
                                                          doesn’t
                    UDP                                   block and
                                                          returns first
                                                          packet from
                                                          buffer



                                                         receive()
                                                         returns
                                                         second packet
                                                         from buffer
Buffering
• If buffer is full, then new data packets are
  discarded
• Happens often for highly-loaded networks
• Programmer can set buffer size of particular
  socket through setReceiveBufferSize(int)
• There is an upper limit enforced by OS. You
  need root access to change it
UDP as a service
• Attempt to deliver a packet of data to
  specified address and port through a
  heterogenious network (wraps IP service)
• Multiplexing (ports)
• Buffering
Application protocol
• A protocol which uses UDP as transport and
  provides some actual service
• Examples: DNS, SNMP, DHCP, TFTP, RTP, SIP
DNS
• Resolution of domain names into IP addresses
• Hierarchial database
• “Server” is a node which performs a service.
  “Client” is a node which asks for a service
• DNS is an on-demand data translation service.
  Client provides source data (domain name)
  and server returns translated data (IP address)
DNS over UDP
• Port number is 53
• Two messages required to implement a service:
  request from client, and response from a server.
  Such two-directional exchange is called a
  transaction
• Message is a one data packet
• Transaction support means correllation and
  failure handling (timeout)
• DNS should implement transactions itself (ID is
  16-bit length)
RTP
• Service: unidirectional streaming of data
• “Server” here is a sender, “Client” is a receiver
• Client doesn’t ask for streaming to start
• Client may not accept packets, server will not
  know about that
• Some data may be lost, this is acceptable
RTP over UDP
• RTP should identify streams. Such large
  communication structure is called “session”
• UDP delivers data packets. RTP should split
  stream into packets and assemble a stream
  back
• RTP should sequence packets
• Support for sessions and sequencing is a task
  for RTP itself, since UDP doesn’t support them.
TCP
•   Reliable session establishement
•   Full duplex sessions
•   Sequencing
•   Retransmissions
•   Buffering with overload control
•   Timeouts
•   Peer death detection
Connection stages
• Asymmetrical session establisheement: one
  side active, one side passive
• Symmetrical session usage
• Session closure is a complex thing
Code for accepter (“server”)
InetSocketAddress local = new InetSocketAddress("10.50.3.70", 7788);

ServerSocket servSock = new ServerSocket();

servSock.bind(local);

Socket socket = servSock.accept();
Code for connector (“client”)
Socket socket = new Socket();

InetSocketAddress remote = new InetSocketAddress("10.50.3.70", 7788);

socket.connect(remote);
Connection spawning

          ServerSocket




 Socket
Connection spawning

                  ServerSocket
Socket




         Socket
Connection spawning

                  ServerSocket
Socket




         Socket
Connection spawning

                  ServerSocket
Socket




         Socket                  Socket
Connection spawning

                  ServerSocket
Socket                               Socket




         Socket                  Socket
Connection spawning

                  ServerSocket
Socket                               Socket




         Socket                  Socket
Three-way handshake
                            Receiving
                      OS   application

                             application
                             thread is
                             running
Three-way handshake
                            Receiving
                      OS   application




                            accept() is a
                            blocking
                            system call
Three-way handshake
    Sending                  Receiving
   application   OS   OS    application




application
  thread is                application
   running                 thread is
                           blocked
Three-way handshake
   Sending                        Receiving
  application   OS         OS    application




                                application
                                thread is
                     SYN        blocked
 connect() is
   a blocking
  system call
Three-way handshake
  Sending                              Receiving
 application    OS              OS    application




                                     application
                                     thread is
                                     blocked

  application
    thread is
     running

                     SYN, ACK
Three-way handshake
  Sending                        Receiving
 application   OS         OS    application




                               application
                               thread is
                               blocked


   connect()
     returns




                    ACK
Three-way handshake
    Sending                       Receiving
   application   OS         OS   application




                                  accept()
                                  returns
application           ACK
  thread is
   running
Three-way handshake: overview
   Sending                                             Receiving
  application   OS                               OS   application




                            SYN
                                                      accept() is a
 connect() is                                         blocking
   a blocking                                         system call
  system call
                                      SYN, ACK

                     ACK

                      Packet is delivered
                      from OS to OS
Protocol’s stack
                                                         Application

                                         Payload
•Source port
•Destination port                                           TCP
•Checksum
•Flags (SYN, ACK, FIN, RST)
•Sequence number                   TCP   Payload
•Acknowledged number
•Window size
                                                             IP

•Source IP address            IP   TCP   Payload
•Dest IP address
•Protocol ID (TCP=6)
                                                          Ethernet

                       Eth    IP   UDP   Payload   Eth
Failure: no acceptor process
  Sending
 application    OS                       OS



                                              Global failure:
                                              no receiver
                                              process


                     SYN

    connect()
       throws
  IOException

                      ICMP message:
                      port unreachable
Writing data
OutputStream outstream = socket.getOutputStream();

outstream.write("I love TCP".getBytes());
Reading data
InputStream instream = socket.getInputStream();

byte[] data = new byte[100];

int length = instream.read(data);

System.out.println("Received " + new String(data, 0, length));
Reading and writing
 Application   OS     OS   Application

  Thread                       Thread is
       is                      running
 running
Reading and writing
 Application   OS          OS   Application

                                    Thread is
                                    running
                    PUSH
 write()
     is a
 system
     call
Reading and writing
 Application   OS          OS buffers data     OS   Application
                           until next read()
                              operation                 Thread is
                                                        running
                    PUSH
  write()
 returns
 without
 waiting
 for ACK
                                ACK

                     ACK confirms
                      that another
                        side have
                     received data
Reading and writing
  Application   OS    OS   Application




  Thread                      read() doesn’t
       is                     block, since
 running                      there are data
                              in buffer
Reading and writing
 Application       OS   OS   Application




                                Thread is
                                running
 read()
 blocks, since
 buffer is empty
Reading and writing
 Application     OS          OS   Application




 Thread is
 blocked by OS
 in read()            PUSH
                                      write()
 operation
Reading and writing
 Application      OS                OS   Application




 read() returns
 with received
                       PUSH
                                             write()
 data



                              ACK
Reading and writing: overview
 Application       OS                  OS   Application



                        PUSH
 write()

                                               read() doesn’t
                                               block, since
                           ACK                 there are data
                                               in buffer


 read() blocks,
 since buffer is         PUSH
 empty                                          write()




                                 ACK
TCP as a stream protocol
• Modify code to read and write data in loop
• No message borders
• When buffer is full
Sequencing and retransmissions
 Application   OS                       OS   Application

  write()      104   PUSH (seq = 104)
   sends
    large      105
 array of
     data      106
Sequencing and retransmissions
 Application   OS                                  OS    Application

  write()            PUSH (seq = 104)
   sends
    large
 array of                                          104
     data      105
                                   ACK (seq=104)
               106
                       ACK releases packet
                        from send buffer
Sequencing and retransmissions
 Application   OS                       OS    Application




               105
               106
                     PUSH (seq = 105)
                                        104
Sequencing and retransmissions
 Application   OS                       OS    Application




               105
               106
                     PUSH (seq = 106)
                                        104
Sequencing and retransmissions
 Application    OS                                      OS    Application




                     PUSH (seq = 106)
                                                        104
               105                                      106
                                        ACK (seq=106)
Sequencing and retransmissions
 Application    OS                      OS    Application




               105
                     PUSH (seq = 105)
                                        104
                                        106
Sequencing and retransmissions
 Application   OS                                      OS    Application




                    PUSH (seq = 105)                   104
                                                       105
                                                       106
                                       ACK (seq=105)
Sequencing and retransmissions
 Application   OS                                      OS    Application

  write()            PUSH (seq = 104)
   sends
    large
 array of                                              104
     data
                                   ACK (seq=104)
                    PUSH (seq = 105)


                    PUSH (seq = 106)
                                                       104
                                                       106
                                       ACK (seq=106)
                    PUSH (seq = 105)                   104
                                                       105
                                                       106
                                       ACK (seq=105)
Sequencing and retransmissions
• Single packet loss is fixed by retransmissions
  and is transparent to application
• Packets have sequence numbers and they are
  ordered inside socket buffers
• Packet may be sent before previous packet
  was acknowledged. This increases
  throughtput and latency
Flow control
• TCP has a flow control mechanism which
  prevents sender from sending packets which
  receiver can’t buffer
• This means that write() may block
Flow control                    Application
 Application   OS          OS



                    PUSH
  write()
Flow control                                                  Application
 Application   OS                                        OS


                              PUSH
  write()



                      ACK, window size = 0


                    Data are buffered by receiver, he
                     reports window size=0 to avoid
                    packets which it will have to drop
Flow control               Application
  Application    OS   OS




write() stores
data in local
buffer
Flow control                Application
 Application      OS   OS




 write() blocks
 since local
 buffer is full
Flow control overview                                            Application
  Application     OS                                        OS


                                 PUSH
   write()



                             ACK, window size = 0

write() stores
data in local          Data are buffered by receiver, he
buffer                  reports window size=0 to avoid
                       packets which it will have to drop




 write() blocks
 since local
 buffer is full
Failures
• TCP doesn’t fix those for you. Instead, they are
  reported as Exceptions
• Recover as you can
Failure: network connectivity loss
 Application   OS



                    PUSH
 write()
Failure: network connectivity loss
 Application   OS




 write()



                    PUSH
                                If ACK is not
                            received, then data
                           are retransmitted by
                                     OS
Failure: network connectivity loss
 Application   OS




 write()




                    PUSH



                      Connection
                    timeout occurs
Failure: network connectivity loss
 Application   OS




                    attepmt to
                    read() or
                    write() will
                    result in
                    IOException
Failure: network connectivity loss
   Application   OS



                      PUSH

   write()


                       PUSH
                                            If ACK is not
                                        received, then data
                                       are retransmitted by
                                                 OS
                       PUSH


 attepmt to
 read() or
 write() will           Connection
 result in            timeout occurs
 IOException
Failure: network connectivity loss
• TCP doesn’t tell to application which data
  were delivered
• You can’t get back your data from local send
  buffer
Failure: host reboot
 Application   OS      OS   Application
Failure: host reboot
 Application   OS          OS   Application



                    PUSH
 write()



                     ACK
Failure: host reboot
 Application   OS          OS      Application



                    PUSH
 write()



                     ACK
                                Crash!!!
Failure: host reboot
 Application   OS




                       OS
Failure: host reboot
 Application   OS




   write()          PUSH
  returns
                           OS
 normally
Failure: host reboot
 Application   OS




   write()          PUSH
  returns
                           OS
 normally


                     RST
Failure: host reboot
   Application   OS




                       OS




   write()
 results in
exception
Failure: host reboot overview
   Application   OS           OS      Application



                      PUSH
   write()



                       ACK
                                   Crash!!!
    write()            PUSH
   returns
                              OS
  normally


                        RST
   write()
 results in
exception
Closing a connection
• To free OS resources
• It can have some semantics in your protocol
Closing a connection
 Application   OS      OS   Application




 read() is
 blocked
Closing a connection
 Application   OS                        OS   Application



                       FIN
                                                 close() is a
                                                 system call


                     Socket is closed.
                      It can’t send or
                    receive any data.
                    Such attempt will
                       result in error
Closing a connection
 Application   OS                       OS   Application



                      FIN
 read()
 returns -                                      close() is a
 1, which                                       system call
 means EOF


                         ACK


                    Socket is not yet
                        closed
Closing a connection
 Application    OS           OS   Application



                     FIN
  read()
  returns -1,                        close() is a
  which means                        system call
  EOF


                       ACK


                       FIN
 close()
Closing a connection
 Application    OS               OS   Application



                     FIN
  read()
  returns -1,                            close() is a
  which means                            system call
  EOF


                       ACK


                       FIN
 close()



                           ACK
Closing a connection
• It is not as easy as it seems
• Subject to race conditions
Race condition
 Application   OS   OS   Application
Race condition
 Application   OS         OS   Application


                    FIN
                                 close()
Race condition
 Application   OS         OS   Application


                    FIN
                                 close()



                    ACK
Race condition
 Application   OS                OS   Application


                    FIN
                                        close()



                    ACK


 write()
                          PUSH
Race condition
 Application   OS                             OS   Application


                         FIN
                                                     close()



                        ACK


 write()                            PUSH
 returns
 normally


                                       RST
                      OS replies with RST
                    since socket was closed
Race condition
    Application   OS                             OS   Application


                            FIN
                                                        close()



                           ACK


                                       PUSH



read()                                    RST
results in
IOException,             OS replies with RST
buffer is              since socket was closed
burned
Using RST to close a socket
• A fast way of closing a socket and burn all
  remaining data is by sending RST
• Used by Windows when process terminates
• To apply use: socket.setSoLinger(true, 0);
Shutdown of output stream
• A way to send FIN without closing a socket
Shutdown of output stream
 Application   OS      OS   Application
Shutdown of output stream
 Application   OS         OS   Application


                    FIN
                                 shutdown()
                                 is a system
                                 call
Shutdown of output stream
 Application   OS               OS   Application


                          FIN
                                       shutdown()



                    ACK
Shutdown of output stream
 Application   OS      OS   Application




                                read() blocks
                                in OS
Shutdown of output stream
  Application   OS          OS   Application




 write()
                     PUSH
Shutdown of output stream
 Application   OS                OS   Application




 write()
 returns            PUSH                read()
 normally                               returns


                           ACK
Shutdown of output stream
 Application   OS      OS   Application




read()
returns data
from buffer
then EOF
                             read() blocks
                             in OS
Shutdown of output stream
  Application   OS         OS   Application




close() is
safe,
because              FIN
receive
buffer is
empty
Shutdown of output stream
 Application   OS          OS   Application




                                 read()
                    FIN          returns EOF
 close()

                     ACK
Shutdown of output stream
 Application   OS                              OS   Application


                          FIN
                                                       shutdown()



                    ACK

   write()                      PUSH
                                                      read()



 read()
 returns
 EOF                                     ACK
                                                       read()
                                       FIN             returns
 close()                                               EOF


                                        ACK
What service TCP does not provide
• Messages
• Multicast
• Encryption
Java IO API: TCP vs UDP
• DatagramPacket =         • SocketAddress, byte[], l
  SocketAddress + byte[]     ength, offset
  + offset + length
• send(), receive()        • read(), write()
Java NIO API
• Convenience of ByteBuffers
• Blocking and non-blocking modes
• Selectors
UDP sender: compare IO and NIO
byte[] data = "Hello!".getBytes();                 byte[] data = "Hello!".getBytes();

InetSocketAddress remote = new                     InetSocketAddress remote = new
    InetSocketAddress("10.50.3.70", 7767);             InetSocketAddress("10.50.3.70", 7767);

DatagramPacket packet = new DatagramPacket(data,   ByteBuffer packet = ByteBuffer.wrap(data);
    data.length, remote);

DatagramSocket socket = new DatagramSocket();      DatagramChannel channel = DatagramChannel.open();

socket.send(packet);                               channel.send(packet, remote);
UDP receiver: compare IO and NIO
InetSocketAddress local = new                                InetSocketAddress local = new
     InetSocketAddress("10.50.3.70", 7767);                       InetSocketAddress("10.50.3.70", 7767);

DatagramSocket socket = new DatagramSocket(local);           DatagramChannel channel = DatagramChannel.open();

                                                             channel.socket().bind(local);

byte[] buffer = new byte[100];                               byte[] buffer = new byte[100];

DatagramPacket packet = new                                  ByteBuffer packet = ByteBuffer.wrap(buffer);
    DatagramPacket(buffer, buffer.length);

socket.receive(packet);                                      SocketAddress remote = channel.receive(packet);

                                                             packet.flip();

String data = new                                            String data = new
     String(packet.getData(), packet.getOffset(), packet.g        String(buffer.array(), 0, buffer.remaining());
     etLength());
                                                             System.out.println("From " + remote+ " received " + data);
System.out.println("From " + packet.getSocketAddress() +
     " received " + data);
ByteBuffer
•   Similar to
    DatagramPacket
    without address
•   Supports writing to
    it and reading from
                           position    limit   capacity
    it
•   You can “return”
    data back by
    moving a position
Using a ByteBuffer
  position   limit   Some initial
                     state. Garbage
                     inside
Using a ByteBuffer
      position                 limit



position                                              limit   clear()



    Prepared to accept some content. Full capacity.
Using a ByteBuffer

position                                             limit


             position                                limit          receive()



   New data stored, position moved to point right after the data.
   Can be invoked in sequence. Attempt to write beyond limit
   will result in BufferOverflowException
Using a ByteBuffer



            position                                limit



position          limit                                     flip()



  Limit fixes data size, position is moved to the
  beginning. Ready to read these data
Using a ByteBuffer


  get() copies data from ByteBuffer to provided destination.
  Position is moved. Can be invoked in sequence. Attempt to read
  beyond limit will result in BufferUnderflowException



position          limit



      position    limit                                        get()


           remaining()
Using a ByteBuffer




 Method compact() moves all remaining data to
 the beginning of buffer. Position is placed right
 behind those data. Limit is moved to capacity.


    position    limit



     position                                  limit   compact()
Using a ByteBuffer: overview
      position                limit



position                                      limit   clear()


                position                      limit
                                                      receive()

position              limit
                                                      flip()

       position       limit                           get()


           position                   limit           compact()
TCP acceptor: compare IO and NIO
InetSocketAddress local = new                 InetSocketAddress local = new
    InetSocketAddress("10.50.3.70", 7788);        InetSocketAddress("10.50.3.70", 7788);

ServerSocket servSock = new ServerSocket();   ServerSocketChannel servSock =
                                                  ServerSocketChannel.open();


servSock.bind(local);                         servSock.socket.bind(local);


Socket socket = servSock.accept();            SocketChannel channel = servSock.accept();
TCP connecor: compare IO and NIO
Socket socket = new Socket();                SocketChannel chan = SocketChannel.open();

InetSocketAddress remote = new               InetSocketAddress remote = new
    InetSocketAddress("10.50.3.70", 7788);       InetSocketAddress("10.50.3.70", 7788);

socket.connect(remote);                      chan.connect(remote);
Advanced UDP
•   Broadcasting
•   Multicasting
•   Connected sockets
•   Fragmentation
Advanced TCP
• OOB-data
• Transmission control
The end

Thank you very much for attending
Wrapping stream into channel
Non-blocking operations
Multiplexing through Selectors
NIO2: Asynchronous sockets
UNUSED SLIDES
Programming model: IPC
  Sending application            Receiving application

                                        receive() is a
                                        “system call” from
                                        application to OS. It
                                        makes OS to block
                                        application thread
       send() is a      UDP             until data are
                                        available
  system call, but      packet
 it doesn’t block
      application
                                        Incoming packet
           thread
                                        makes OS to resume
                                        application thread
                                        and return from
                                        system call
Connection spawning
                  ServerSocket
Socket                               Socket




         Socket                  Socket
Buffering                                             Application
 Application   OS                                OS
                       OS buffers data until
                      next receive() operation
                                                          Thread is
                                                          running
  send()            UDP
 returns


                    UDP

                                                          receive()
                                                          doesn’t
                    UDP                                   block and
                                                          returns first
                                                          packet from
                                                          buffer



                                                         receive()
                                                         returns
                                                         second packet
                                                         from buffer
Blocking IPC
   Sending                                             Receiving
  application   OS                               OS   application




                                                       accept() is a
                                                       blocking
                                                       system call
 connect() is
                            SYN
   a blocking
  system call


                                      SYN, ACK

                     ACK

                      Packet is delivered
                      from OS to OS
Protocol analysis framework
• Medium of communication (“wire”)
• Parties which communicate (two or more)
• Direction of communication
• A unit of communication (“message” ?)
• Larger communication constructs
  (transactions, sessions)
• Communication failures (global and per-
  attempt)

More Related Content

What's hot

What's hot (16)

Java socket programming
Java socket programmingJava socket programming
Java socket programming
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
 
My speech at AstriCon 2007
My speech at AstriCon 2007My speech at AstriCon 2007
My speech at AstriCon 2007
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Normas y Estándares
Normas y EstándaresNormas y Estándares
Normas y Estándares
 
Ipv6 Technical White Paper Wp111504
Ipv6 Technical White Paper Wp111504Ipv6 Technical White Paper Wp111504
Ipv6 Technical White Paper Wp111504
 
socket programming
socket programming socket programming
socket programming
 
Networking 101
Networking 101Networking 101
Networking 101
 
p10
p10p10
p10
 
Networking Java Socket Programming
Networking Java Socket ProgrammingNetworking Java Socket Programming
Networking Java Socket Programming
 
04 coms 525 tcpip - arp and rarp
04   coms 525 tcpip - arp and rarp04   coms 525 tcpip - arp and rarp
04 coms 525 tcpip - arp and rarp
 
Zig bee (8)
Zig bee (8)Zig bee (8)
Zig bee (8)
 
Tcp ip protocol
Tcp ip protocol Tcp ip protocol
Tcp ip protocol
 
Ccna1v3 mod09
Ccna1v3 mod09Ccna1v3 mod09
Ccna1v3 mod09
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In Java
 

Similar to Tcp2 (20)

3rd edition chapter2
3rd edition chapter23rd edition chapter2
3rd edition chapter2
 
Week3 applications
Week3 applicationsWeek3 applications
Week3 applications
 
Network Sockets
Network SocketsNetwork Sockets
Network Sockets
 
Sockets
Sockets Sockets
Sockets
 
Np unit1
Np unit1Np unit1
Np unit1
 
Chapter2 application
Chapter2 applicationChapter2 application
Chapter2 application
 
Lecture application layer
Lecture application layerLecture application layer
Lecture application layer
 
Multipath TCP
Multipath TCPMultipath TCP
Multipath TCP
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Java 1
Java 1Java 1
Java 1
 
Understanding computer networks
Understanding computer networksUnderstanding computer networks
Understanding computer networks
 
Np unit iv ii
Np unit iv iiNp unit iv ii
Np unit iv ii
 
EN-04 (1).pptx
EN-04 (1).pptxEN-04 (1).pptx
EN-04 (1).pptx
 
More on Tcp/Ip
More on Tcp/IpMore on Tcp/Ip
More on Tcp/Ip
 
Programming TCP/IP with Sockets
Programming TCP/IP with SocketsProgramming TCP/IP with Sockets
Programming TCP/IP with Sockets
 
Lecture 5 internet-protocol_assignments
Lecture 5 internet-protocol_assignmentsLecture 5 internet-protocol_assignments
Lecture 5 internet-protocol_assignments
 
Kubernetes on open stack
Kubernetes on open stackKubernetes on open stack
Kubernetes on open stack
 
Socket Programming w/ C# - IK
Socket Programming w/ C# - IKSocket Programming w/ C# - IK
Socket Programming w/ C# - IK
 
Tcp
TcpTcp
Tcp
 
Tcp ip
Tcp ipTcp ip
Tcp ip
 

Tcp2

  • 1. TCP/IP Simple introduction to protocols and programming
  • 2. Plan • We discuss both protocols and how to program them. You cannot program a protocol if you don’t know how it works. • Programming will be explained from application developer’s point of view. Protocol implementors and tool developers need some specific knowledge • Programming is explained using Java, but principles are applicable to most other languages • Java APIs are redundant, anyway we discuss all of them • Tools and troubleshooting are included
  • 3. UDP • One of the simplest protocols ever • Java API reflects that simplicity
  • 4. Code for sender byte[] data = "Hello!".getBytes(); InetSocketAddress remote = new InetSocketAddress("10.50.3.70", 7767); DatagramPacket packet = new DatagramPacket(data, data.length, remote); DatagramSocket socket = new DatagramSocket(); socket.send(packet);
  • 5. Code for receiver InetSocketAddress local = new InetSocketAddress("10.50.3.70", 7767); DatagramSocket socket = new DatagramSocket(local); byte[] buffer = new byte[100]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); socket.receive(packet); String data = new String(packet.getData(), packet.getOffset(), packet.getLength()); System.out.println("From " + packet.getSocketAddress() + " received " + data);
  • 6. Programming model: blocking IPC Receiving OS application application thread is running
  • 7. Programming model: blocking IPC Receiving OS application receive() is a “system call” from application to OS
  • 8. Programming model: blocking IPC Receiving OS application receive() is a “system call” from application to OS. It makes OS to block application thread until data are available
  • 9. Programming model: blocking IPC Sending Receiving application OS OS application application thread is running Application thread blocked
  • 10. Programming model: blocking IPC Sending Receiving application OS OS application UDP Application send() is a packet thread blocked system call
  • 11. Programming model: blocking IPC Sending Receiving application OS OS application UDP Application send() is a thread blocked system call, but packet it doesn’t block application thread Packet is delivered from OS to OS
  • 12. Programming model: blocking IPC Sending Receiving application OS OS application UDP Incoming packet makes OS to resume packet application thread and return from system call application thread is running
  • 13. Programming model: blocking IPC Sending Receiving application OS OS application application thread is application running thread is running
  • 14. Programming model: blocking IPC Sending Receiving application OS OS application UDP receive() is a send() is a packet blocking system call. non-blocking system call It returns when data are received
  • 15. Party identification • IP address used by underlying IP layer • Four bytes, usually written as dot-separated decimal numbers: “10.50.3.70” • InetAddress • UDP port: integer between 0 and 65535 • InetSocketAddress = InetAddress + port
  • 16. Protocol’s stack Remote IP address, remote port and payload are provided by Application application when it makes a system call Remote IP/port Payload OS/Application boundary Local IP/port UDP Local IP address and port are bound to socket IP Ethernet
  • 17. Protocol’s stack Application Remote IP/port Payload Local IP/port UDP •Destination port UDP Payload •Source port •Checksum IP Ethernet
  • 18. Protocol’s stack Application Remote IP/port Payload Local IP/port UDP •Dest IP address UDP Payload •Source IP address IP address is resolved •Protocol ID (UDP=17) into MAC address IP IP UDP Payload Ethernet
  • 19. Protocol’s stack Application Payload UDP •Source MAC address UDP Payload •Dest MAC address •EtherType (IP=0x0800) IP IP UDP Payload OS invokes driver to send data through the wire Ethernet Eth IP UDP Payload Eth
  • 20. Protocol’s stack Application Application Payload Dispatch to UDP applications, UDP buffering UDP Payload IP packet is IP dispatched to IP UDP layer IP UDP Payload Ethernet packet Ethernet is dispatched to Ethernet IP layer Eth IP UDP Payload Eth
  • 21. Sniffer • Sniffer is a tool which intercepts packets at link level
  • 22. Communication failure Sending application OS OS Global failure: no receiver process UDP send() packet completes normally ICMP message: port unreachable
  • 23. Local address and binding • Sockets which were not bound will choose their address and port automatically • Failure: can’t bind if host:port are occupied • Special address: any local address
  • 24. Closing sockets • Sockets consume OS resources • Closed automatically when process dies • Nothing is sent when socket is closed • Sockets can’t be re-used after they are closed, attempt to use will result in IOException
  • 25. Bi-directional communication • Receive-reply example • Receiver replies to the sender • The same socket is re-used by both sides, once for sending, and once for receiving • Receiver can work in a loop. Each sender has only one transaction at time
  • 26. Multi-party communication • One DatagramSocket can send and receive packets to any address • Receive twice and send twice examples
  • 27. Buffering • Send 100 messages in a loop
  • 28. Buffering Application Application OS OS Thread is running
  • 29. Buffering Application Application OS OS OS buffers data until next receive() operation Thread is running send() UDP returns
  • 30. Buffering Application Application OS OS Thread is running send() returns UDP OS adds new data to buffer, preserving boundaries
  • 31. Buffering Application Application OS OS Thread is running send() returns UDP
  • 32. Buffering Application Application OS OS Thread is running receive() doesn’t block and returns first packet from buffer
  • 33. Buffering Application Application OS OS receive() returns second packet from buffer
  • 34. Buffering Application Application OS OS OS buffers data until next receive() operation Thread is running send() UDP returns UDP receive() doesn’t UDP block and returns first packet from buffer receive() returns second packet from buffer
  • 35. Buffering • If buffer is full, then new data packets are discarded • Happens often for highly-loaded networks • Programmer can set buffer size of particular socket through setReceiveBufferSize(int) • There is an upper limit enforced by OS. You need root access to change it
  • 36. UDP as a service • Attempt to deliver a packet of data to specified address and port through a heterogenious network (wraps IP service) • Multiplexing (ports) • Buffering
  • 37. Application protocol • A protocol which uses UDP as transport and provides some actual service • Examples: DNS, SNMP, DHCP, TFTP, RTP, SIP
  • 38. DNS • Resolution of domain names into IP addresses • Hierarchial database • “Server” is a node which performs a service. “Client” is a node which asks for a service • DNS is an on-demand data translation service. Client provides source data (domain name) and server returns translated data (IP address)
  • 39. DNS over UDP • Port number is 53 • Two messages required to implement a service: request from client, and response from a server. Such two-directional exchange is called a transaction • Message is a one data packet • Transaction support means correllation and failure handling (timeout) • DNS should implement transactions itself (ID is 16-bit length)
  • 40. RTP • Service: unidirectional streaming of data • “Server” here is a sender, “Client” is a receiver • Client doesn’t ask for streaming to start • Client may not accept packets, server will not know about that • Some data may be lost, this is acceptable
  • 41. RTP over UDP • RTP should identify streams. Such large communication structure is called “session” • UDP delivers data packets. RTP should split stream into packets and assemble a stream back • RTP should sequence packets • Support for sessions and sequencing is a task for RTP itself, since UDP doesn’t support them.
  • 42. TCP • Reliable session establishement • Full duplex sessions • Sequencing • Retransmissions • Buffering with overload control • Timeouts • Peer death detection
  • 43. Connection stages • Asymmetrical session establisheement: one side active, one side passive • Symmetrical session usage • Session closure is a complex thing
  • 44. Code for accepter (“server”) InetSocketAddress local = new InetSocketAddress("10.50.3.70", 7788); ServerSocket servSock = new ServerSocket(); servSock.bind(local); Socket socket = servSock.accept();
  • 45. Code for connector (“client”) Socket socket = new Socket(); InetSocketAddress remote = new InetSocketAddress("10.50.3.70", 7788); socket.connect(remote);
  • 46. Connection spawning ServerSocket Socket
  • 47. Connection spawning ServerSocket Socket Socket
  • 48. Connection spawning ServerSocket Socket Socket
  • 49. Connection spawning ServerSocket Socket Socket Socket
  • 50. Connection spawning ServerSocket Socket Socket Socket Socket
  • 51. Connection spawning ServerSocket Socket Socket Socket Socket
  • 52. Three-way handshake Receiving OS application application thread is running
  • 53. Three-way handshake Receiving OS application accept() is a blocking system call
  • 54. Three-way handshake Sending Receiving application OS OS application application thread is application running thread is blocked
  • 55. Three-way handshake Sending Receiving application OS OS application application thread is SYN blocked connect() is a blocking system call
  • 56. Three-way handshake Sending Receiving application OS OS application application thread is blocked application thread is running SYN, ACK
  • 57. Three-way handshake Sending Receiving application OS OS application application thread is blocked connect() returns ACK
  • 58. Three-way handshake Sending Receiving application OS OS application accept() returns application ACK thread is running
  • 59. Three-way handshake: overview Sending Receiving application OS OS application SYN accept() is a connect() is blocking a blocking system call system call SYN, ACK ACK Packet is delivered from OS to OS
  • 60. Protocol’s stack Application Payload •Source port •Destination port TCP •Checksum •Flags (SYN, ACK, FIN, RST) •Sequence number TCP Payload •Acknowledged number •Window size IP •Source IP address IP TCP Payload •Dest IP address •Protocol ID (TCP=6) Ethernet Eth IP UDP Payload Eth
  • 61. Failure: no acceptor process Sending application OS OS Global failure: no receiver process SYN connect() throws IOException ICMP message: port unreachable
  • 62. Writing data OutputStream outstream = socket.getOutputStream(); outstream.write("I love TCP".getBytes());
  • 63. Reading data InputStream instream = socket.getInputStream(); byte[] data = new byte[100]; int length = instream.read(data); System.out.println("Received " + new String(data, 0, length));
  • 64. Reading and writing Application OS OS Application Thread Thread is is running running
  • 65. Reading and writing Application OS OS Application Thread is running PUSH write() is a system call
  • 66. Reading and writing Application OS OS buffers data OS Application until next read() operation Thread is running PUSH write() returns without waiting for ACK ACK ACK confirms that another side have received data
  • 67. Reading and writing Application OS OS Application Thread read() doesn’t is block, since running there are data in buffer
  • 68. Reading and writing Application OS OS Application Thread is running read() blocks, since buffer is empty
  • 69. Reading and writing Application OS OS Application Thread is blocked by OS in read() PUSH write() operation
  • 70. Reading and writing Application OS OS Application read() returns with received PUSH write() data ACK
  • 71. Reading and writing: overview Application OS OS Application PUSH write() read() doesn’t block, since ACK there are data in buffer read() blocks, since buffer is PUSH empty write() ACK
  • 72. TCP as a stream protocol • Modify code to read and write data in loop • No message borders • When buffer is full
  • 73. Sequencing and retransmissions Application OS OS Application write() 104 PUSH (seq = 104) sends large 105 array of data 106
  • 74. Sequencing and retransmissions Application OS OS Application write() PUSH (seq = 104) sends large array of 104 data 105 ACK (seq=104) 106 ACK releases packet from send buffer
  • 75. Sequencing and retransmissions Application OS OS Application 105 106 PUSH (seq = 105) 104
  • 76. Sequencing and retransmissions Application OS OS Application 105 106 PUSH (seq = 106) 104
  • 77. Sequencing and retransmissions Application OS OS Application PUSH (seq = 106) 104 105 106 ACK (seq=106)
  • 78. Sequencing and retransmissions Application OS OS Application 105 PUSH (seq = 105) 104 106
  • 79. Sequencing and retransmissions Application OS OS Application PUSH (seq = 105) 104 105 106 ACK (seq=105)
  • 80. Sequencing and retransmissions Application OS OS Application write() PUSH (seq = 104) sends large array of 104 data ACK (seq=104) PUSH (seq = 105) PUSH (seq = 106) 104 106 ACK (seq=106) PUSH (seq = 105) 104 105 106 ACK (seq=105)
  • 81. Sequencing and retransmissions • Single packet loss is fixed by retransmissions and is transparent to application • Packets have sequence numbers and they are ordered inside socket buffers • Packet may be sent before previous packet was acknowledged. This increases throughtput and latency
  • 82. Flow control • TCP has a flow control mechanism which prevents sender from sending packets which receiver can’t buffer • This means that write() may block
  • 83. Flow control Application Application OS OS PUSH write()
  • 84. Flow control Application Application OS OS PUSH write() ACK, window size = 0 Data are buffered by receiver, he reports window size=0 to avoid packets which it will have to drop
  • 85. Flow control Application Application OS OS write() stores data in local buffer
  • 86. Flow control Application Application OS OS write() blocks since local buffer is full
  • 87. Flow control overview Application Application OS OS PUSH write() ACK, window size = 0 write() stores data in local Data are buffered by receiver, he buffer reports window size=0 to avoid packets which it will have to drop write() blocks since local buffer is full
  • 88. Failures • TCP doesn’t fix those for you. Instead, they are reported as Exceptions • Recover as you can
  • 89. Failure: network connectivity loss Application OS PUSH write()
  • 90. Failure: network connectivity loss Application OS write() PUSH If ACK is not received, then data are retransmitted by OS
  • 91. Failure: network connectivity loss Application OS write() PUSH Connection timeout occurs
  • 92. Failure: network connectivity loss Application OS attepmt to read() or write() will result in IOException
  • 93. Failure: network connectivity loss Application OS PUSH write() PUSH If ACK is not received, then data are retransmitted by OS PUSH attepmt to read() or write() will Connection result in timeout occurs IOException
  • 94. Failure: network connectivity loss • TCP doesn’t tell to application which data were delivered • You can’t get back your data from local send buffer
  • 95. Failure: host reboot Application OS OS Application
  • 96. Failure: host reboot Application OS OS Application PUSH write() ACK
  • 97. Failure: host reboot Application OS OS Application PUSH write() ACK Crash!!!
  • 98. Failure: host reboot Application OS OS
  • 99. Failure: host reboot Application OS write() PUSH returns OS normally
  • 100. Failure: host reboot Application OS write() PUSH returns OS normally RST
  • 101. Failure: host reboot Application OS OS write() results in exception
  • 102. Failure: host reboot overview Application OS OS Application PUSH write() ACK Crash!!! write() PUSH returns OS normally RST write() results in exception
  • 103. Closing a connection • To free OS resources • It can have some semantics in your protocol
  • 104. Closing a connection Application OS OS Application read() is blocked
  • 105. Closing a connection Application OS OS Application FIN close() is a system call Socket is closed. It can’t send or receive any data. Such attempt will result in error
  • 106. Closing a connection Application OS OS Application FIN read() returns - close() is a 1, which system call means EOF ACK Socket is not yet closed
  • 107. Closing a connection Application OS OS Application FIN read() returns -1, close() is a which means system call EOF ACK FIN close()
  • 108. Closing a connection Application OS OS Application FIN read() returns -1, close() is a which means system call EOF ACK FIN close() ACK
  • 109. Closing a connection • It is not as easy as it seems • Subject to race conditions
  • 110. Race condition Application OS OS Application
  • 111. Race condition Application OS OS Application FIN close()
  • 112. Race condition Application OS OS Application FIN close() ACK
  • 113. Race condition Application OS OS Application FIN close() ACK write() PUSH
  • 114. Race condition Application OS OS Application FIN close() ACK write() PUSH returns normally RST OS replies with RST since socket was closed
  • 115. Race condition Application OS OS Application FIN close() ACK PUSH read() RST results in IOException, OS replies with RST buffer is since socket was closed burned
  • 116. Using RST to close a socket • A fast way of closing a socket and burn all remaining data is by sending RST • Used by Windows when process terminates • To apply use: socket.setSoLinger(true, 0);
  • 117. Shutdown of output stream • A way to send FIN without closing a socket
  • 118. Shutdown of output stream Application OS OS Application
  • 119. Shutdown of output stream Application OS OS Application FIN shutdown() is a system call
  • 120. Shutdown of output stream Application OS OS Application FIN shutdown() ACK
  • 121. Shutdown of output stream Application OS OS Application read() blocks in OS
  • 122. Shutdown of output stream Application OS OS Application write() PUSH
  • 123. Shutdown of output stream Application OS OS Application write() returns PUSH read() normally returns ACK
  • 124. Shutdown of output stream Application OS OS Application read() returns data from buffer then EOF read() blocks in OS
  • 125. Shutdown of output stream Application OS OS Application close() is safe, because FIN receive buffer is empty
  • 126. Shutdown of output stream Application OS OS Application read() FIN returns EOF close() ACK
  • 127. Shutdown of output stream Application OS OS Application FIN shutdown() ACK write() PUSH read() read() returns EOF ACK read() FIN returns close() EOF ACK
  • 128. What service TCP does not provide • Messages • Multicast • Encryption
  • 129. Java IO API: TCP vs UDP • DatagramPacket = • SocketAddress, byte[], l SocketAddress + byte[] ength, offset + offset + length • send(), receive() • read(), write()
  • 130. Java NIO API • Convenience of ByteBuffers • Blocking and non-blocking modes • Selectors
  • 131. UDP sender: compare IO and NIO byte[] data = "Hello!".getBytes(); byte[] data = "Hello!".getBytes(); InetSocketAddress remote = new InetSocketAddress remote = new InetSocketAddress("10.50.3.70", 7767); InetSocketAddress("10.50.3.70", 7767); DatagramPacket packet = new DatagramPacket(data, ByteBuffer packet = ByteBuffer.wrap(data); data.length, remote); DatagramSocket socket = new DatagramSocket(); DatagramChannel channel = DatagramChannel.open(); socket.send(packet); channel.send(packet, remote);
  • 132. UDP receiver: compare IO and NIO InetSocketAddress local = new InetSocketAddress local = new InetSocketAddress("10.50.3.70", 7767); InetSocketAddress("10.50.3.70", 7767); DatagramSocket socket = new DatagramSocket(local); DatagramChannel channel = DatagramChannel.open(); channel.socket().bind(local); byte[] buffer = new byte[100]; byte[] buffer = new byte[100]; DatagramPacket packet = new ByteBuffer packet = ByteBuffer.wrap(buffer); DatagramPacket(buffer, buffer.length); socket.receive(packet); SocketAddress remote = channel.receive(packet); packet.flip(); String data = new String data = new String(packet.getData(), packet.getOffset(), packet.g String(buffer.array(), 0, buffer.remaining()); etLength()); System.out.println("From " + remote+ " received " + data); System.out.println("From " + packet.getSocketAddress() + " received " + data);
  • 133. ByteBuffer • Similar to DatagramPacket without address • Supports writing to it and reading from position limit capacity it • You can “return” data back by moving a position
  • 134. Using a ByteBuffer position limit Some initial state. Garbage inside
  • 135. Using a ByteBuffer position limit position limit clear() Prepared to accept some content. Full capacity.
  • 136. Using a ByteBuffer position limit position limit receive() New data stored, position moved to point right after the data. Can be invoked in sequence. Attempt to write beyond limit will result in BufferOverflowException
  • 137. Using a ByteBuffer position limit position limit flip() Limit fixes data size, position is moved to the beginning. Ready to read these data
  • 138. Using a ByteBuffer get() copies data from ByteBuffer to provided destination. Position is moved. Can be invoked in sequence. Attempt to read beyond limit will result in BufferUnderflowException position limit position limit get() remaining()
  • 139. Using a ByteBuffer Method compact() moves all remaining data to the beginning of buffer. Position is placed right behind those data. Limit is moved to capacity. position limit position limit compact()
  • 140. Using a ByteBuffer: overview position limit position limit clear() position limit receive() position limit flip() position limit get() position limit compact()
  • 141. TCP acceptor: compare IO and NIO InetSocketAddress local = new InetSocketAddress local = new InetSocketAddress("10.50.3.70", 7788); InetSocketAddress("10.50.3.70", 7788); ServerSocket servSock = new ServerSocket(); ServerSocketChannel servSock = ServerSocketChannel.open(); servSock.bind(local); servSock.socket.bind(local); Socket socket = servSock.accept(); SocketChannel channel = servSock.accept();
  • 142. TCP connecor: compare IO and NIO Socket socket = new Socket(); SocketChannel chan = SocketChannel.open(); InetSocketAddress remote = new InetSocketAddress remote = new InetSocketAddress("10.50.3.70", 7788); InetSocketAddress("10.50.3.70", 7788); socket.connect(remote); chan.connect(remote);
  • 143. Advanced UDP • Broadcasting • Multicasting • Connected sockets • Fragmentation
  • 144. Advanced TCP • OOB-data • Transmission control
  • 145. The end Thank you very much for attending
  • 151. Programming model: IPC Sending application Receiving application receive() is a “system call” from application to OS. It makes OS to block application thread send() is a UDP until data are available system call, but packet it doesn’t block application Incoming packet thread makes OS to resume application thread and return from system call
  • 152. Connection spawning ServerSocket Socket Socket Socket Socket
  • 153. Buffering Application Application OS OS OS buffers data until next receive() operation Thread is running send() UDP returns UDP receive() doesn’t UDP block and returns first packet from buffer receive() returns second packet from buffer
  • 154. Blocking IPC Sending Receiving application OS OS application accept() is a blocking system call connect() is SYN a blocking system call SYN, ACK ACK Packet is delivered from OS to OS
  • 155. Protocol analysis framework • Medium of communication (“wire”) • Parties which communicate (two or more) • Direction of communication • A unit of communication (“message” ?) • Larger communication constructs (transactions, sessions) • Communication failures (global and per- attempt)

Editor's Notes

  1. In order to send some data we need just two things: the data we want to send and the address of recepient. DatagramPacket is an auxillary structure for storing these two things together. DatagramSocket is a facility which will send the data.DatagramPacket may contain offset. It stores a reference to array, so any changes in array are reflected. These objects are mutable.
  2. There is no way to receive all the incoming data. We need to specify a local address. Only this process will be able to get those data. Method receive() will block until data is received. Packet should be big enough for incoming data to fit. Modified fields are: content of data starting from offset, length, remote address.
  3. IPC means inter-process communication
  4. IPC means inter-process communication
  5. IPC means inter-process communication
  6. IPC means inter-process communication
  7. IPC means inter-process communication
  8. IPC means inter-process communication
  9. IPC means inter-process communication
  10. IPC means inter-process communication
  11. IPC means inter-process communication
  12. Stack picture here
  13. Stack picture here
  14. Stack picture here
  15. Stack picture here
  16. Stack picture here
  17. IPC means inter-process communication
  18. Network can break a sequence.
  19. Stack picture here
  20. IPC means inter-process communication