Tcp2
Upcoming SlideShare
Loading in...5
×
 

Tcp2

on

  • 185 views

 

Statistics

Views

Total Views
185
Views on SlideShare
185
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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.
  • 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.
  • IPC means inter-process communication
  • IPC means inter-process communication
  • IPC means inter-process communication
  • IPC means inter-process communication
  • IPC means inter-process communication
  • IPC means inter-process communication
  • IPC means inter-process communication
  • IPC means inter-process communication
  • IPC means inter-process communication
  • Stack picture here
  • Stack picture here
  • Stack picture here
  • Stack picture here
  • Stack picture here
  • IPC means inter-process communication
  • Network can break a sequence.
  • Stack picture here
  • IPC means inter-process communication

Tcp2 Tcp2 Presentation Transcript

  • TCP/IPSimple 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 View slide
  • Code for senderbyte[] 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); View slide
  • Code for receiverInetSocketAddress 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 applicationapplication 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 packetit 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 callapplication thread is running
  • Programming model: blocking IPC Sending Receiving application OS OS applicationapplication 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 PayloadOS/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 stackApplication 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 ServerSocketSocket Socket
  • Connection spawning ServerSocketSocket Socket
  • Connection spawning ServerSocketSocket Socket Socket
  • Connection spawning ServerSocketSocket Socket Socket Socket
  • Connection spawning ServerSocketSocket 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 applicationapplication 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() returnsapplication 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 dataOutputStream outstream = socket.getOutputStream();outstream.write("I love TCP".getBytes());
  • Reading dataInputStream 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 OSwrite() storesdata in localbuffer
  • 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 = 0write() storesdata in local Data are buffered by receiver, hebuffer 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 inexception
  • Failure: host reboot overview Application OS OS Application PUSH write() ACK Crash!!! write() PUSH returns OS normally RST write() results inexception
  • 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 PUSHread() RSTresults inIOException, OS replies with RSTbuffer is since socket was closedburned
  • 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 Applicationread()returns datafrom bufferthen EOF read() blocks in OS
  • Shutdown of output stream Application OS OS Applicationclose() issafe,because FINreceivebuffer isempty
  • 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 NIObyte[] 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 NIOInetSocketAddress 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 limitposition limit clear() Prepared to accept some content. Full capacity.
  • Using a ByteBufferposition 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 limitposition 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 BufferUnderflowExceptionposition 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 limitposition limit clear() position limit receive()position limit flip() position limit get() position limit compact()
  • TCP acceptor: compare IO and NIOInetSocketAddress 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 NIOSocket 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 endThank 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 ServerSocketSocket 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)