15 - tcp udp€¦ · lehrstuhl für informatik 4 kommunikation und verteilte systeme chapter 3.7:...
TRANSCRIPT
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 1Chapter 3.7: TCP and UDP
TCP (Transmission Control Protocol): Reliable, connection-oriented.
UDP (User Datagram Protocol): Datagram principle, connectionless, unreliable, without flow control, permutation of a packet order can happen.
Connection-oriented Connectionless
ApplicationLayer
TransportLayer
InternetLayer
Host-to-NetworkLayer
Wireless LANEthernet Token Ring Token Bus
FTP Telnet SMTP DNS SNMP TFTPHTTP
UDPTCP
ICMP IP RARPARPIGMP
Transport Protocols in the TCP/IP Reference Model
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 2Chapter 3.7: TCP and UDP
• Transport protocols are used by the application layer as communication services. They make communication available between application processes.
• TCP is a connection-oriented protocol• UDP is a connectionless and fast protocol• Question in between: how is it possible to realize a connection-oriented transport
layer protocol on top of the connectionless IP???
IP network
A
B
Client process
Server process
TCP and UDP - the Transport Layer
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 3Chapter 3.7: TCP and UDP
• Connection-oriented and reliable (error-free, keeps packet order, without duplicates)
• Error handling, acknowledgments, flow control (Sliding Window procedure)• Byte stream, not message stream• Segmentation (max. segment size of 64 KByte)
• “Urgent”-messages outside of flow control• Limited QoS
• Addressing of the application by port numbers
Characteristics of TCP
Host-to-network layer
Network layer
Transport layer
Application layerClient Server1Port number
IP address
Server waits on several ports
Server2
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 4Chapter 3.7: TCP and UDP
Physical layer
Data link layer
Network layer
Transport layer
Application layerPort number
IP address
ClientProcessServer
Process server hands over new connections to the inquired server
Server
Alternatively: Name server (comparable to a phone book) returns the destination port
If the server port is unknown, the use of a process server (Initial Connection Protocol) is possible:
TCP as a Safe Connection
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 5Chapter 3.7: TCP and UDP
TCP as a Safe Connection
• Establishes logical connections between two Sockets: IP address + 16 bit port number (48 bit address information)
• For an application, sockets are the access point to the network
• A socket can be used for several connections at the same time
• TCP connections are always full-duplex and point-to-point connections
• TPDUs exchanged between the two communicating stations are called segments
• Segments are being exchanged for realizing
o Connection establishment,
o Agreement on a window size,
o Data transmission,
o Sending of confirmations,
o Connection termination.
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 6Chapter 3.7: TCP and UDP
Socket Programming in TCP
Server side
• The receiving application process (server) has to run at first
• The server provides a socket over which connection requests are received (i.e. a port is made available)
• In order to be able to receive requests of several clients, the server provides a new socket for a connection request of each client
Client side
• The client generates a socket
• The client creates a request with IP address and port of the server
• When the client creates its socket, a connection establishment to the server is made
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 7Chapter 3.7: TCP and UDP
Socket Primitives in TCP
Release of the connectionCLOSE
Receive data on the connectionRECEIVE
Send data over the connectionSEND
Attempt of a connection establishmentCONNECT
Accept a connecting requestACCEPT
Wait for arriving connecting requestsLISTEN
Assign a local address with the socketBIND
Creation of a new network access pointSOCKET
MeaningPrimitively
For communication via TCP, a set of primitives exists which an application programmer can use for initializing and carrying out a communication. The essential primitives are:
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 8Chapter 3.7: TCP and UDP
Socket programming in TCP
wait for incomingconnection requestconnectionSocket =welcomeSocket.accept ()
create socket,port=x, forincoming request:welcomeSocket =
ServerSocket () create socket,
connect to hostid, port=xclientSocket =
Socket ()
closeconnectionSocket
read reply fromclientSocket
closeclientSocket
Server (on host hostid) Client
send request usingclientSocketread request from
connectionSocket
write reply toconnectionSocket
TCPConnection establishment
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 9Chapter 3.7: TCP and UDP
Example: Java Client (TCP)
import java.io.*;import java.net.*;class TCPClient {
public static void main(string argv[]) throws Exception{
String sentence;String modifiedSentence;BufferedReader inFromUser =new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket(“hostname”, 6789);
DataOutputStream outToServer =new DataOutputStream(clientSocket.getOutputStream ());
Buffers for user input are provided
Create client socket, establish connection
Create data stream for the socket
CONNECT
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 10Chapter 3.7: TCP and UDP
Example: Java Client (TCP)
BufferedReader inFromServer = new BufferedReader(newInputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + “\ n”);
modifiedSentence = inFromServer.readLine();
System.out.println(“FROM SERVER: ” + modifiedSentence);
clientSocket.close();
} }
Create data streamfrom the socket
Send to server
Receive from server
SEND
RECEIVE
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 11Chapter 3.7: TCP and UDP
Example: Java Server (TCP)
import java.io.*; import java.net.*;
class TCPServer {
public static void main(string arg []) throws Exception {
String clientSentence; String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while (true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(newInputStreamReader(connectionSocket.getInputStream()));
Provide default socket on port 6789
Wait for incoming connection requests
Link buffer with the socket
LISTEN
BIND
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 12Chapter 3.7: TCP and UDP
Example: Java Server (TCP)
DataOutputStream outToClient = newDataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + “\ n”;
outToClient.writeBytes(capitalizedSentence); }
} }
Read from socket
Link outgoing data stream with the socket
Write on socket
Go back, wait on next request
SEND
RECEIVE
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 13Chapter 3.7: TCP and UDP
Well known TCP ports
20/21 23 25 80 179
FTP telnet
Electronic Mail
SMTP HTTP BGP
File transferWorld Wide Web
Virtual terminalRouting information
255
RFC 1700
• Port number is 16-bit address
• Ports from 0 to 255 are reserved for standardized applications
• Ports from 256 to 65535 can be specified by each host
TCP-based Applications
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 14Chapter 3.7: TCP and UDP
The TCP Header
• 20 byte header
• Plus options
• Up to 65495 data bytes
Source Port Destination Port
Data
0 16 24 31Bit position
8
PaddingOptions
Checksum Urgent Pointer
Window Size6 FlagsHL Res.
Acknowledgement Number
Sequence Number
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 15Chapter 3.7: TCP and UDP
• Source and Destination Port: port number of sender resp. receiver• Sequence Number/Acknowledgment Number: Segments have a 32 bit sequence
and acknowledgement number for the window mechanism in flow control (Sliding Window).
• Sequence and acknowledgement number count single bytes!• The acknowledgement number indicates the next expected byte!
• Sequence numbers begin not necessarily with 0! A random value is chosen here to avoid a possible mix-up of segments for a new connection with old segments from the last connection which could maybe arrive late.
• Piggybacking, i.e. an acknowledgement can be sent in a data segment.
• HL: As in case of IP, also the TCP header has an indication of its length. The length is counted in 32-bit words.
• Res = Reserved for later use.• Window Size: Size of the receiver’s buffer for the connection. Used in flow control:
the window of a flow indicates, how many bytes at the same time can be sent – the size of the buffer indicates, how many bytes can be stored within the receiver. The window of flow control is adapted to this value.
The TCP Header
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 16Chapter 3.7: TCP and UDP
• Flags:
• URG: URGENT. When using the Urgent pointer, e.g. for special keyboard input (Ctrl-C).
• ACK: This bit is set, if an acknowledgement is sent.
• PSH: PUSH. Direct forwarding of the data, no more waiting for further data.
• RST: RESET. Reset a connection, e.g. during a host crash or a connecting rejection. (“Generally problems arise when a segment with set RST bit is received.”)
• SYN: set to 1 for the establishment of a connection.
• FIN: set to 1 for the termination of a connection.
• Urgent pointer: indicates, at which position in the data field the urgent data end (byte offset of the current sequence number).
• Option: additional functions: negotiation of a window scale; use of e.g. Selective Repeat instead of Go-Back-n in the event of an error; Indication of the Maximum Segment Size (MSS) to determine the size of the data field.
The TCP Header
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 17Chapter 3.7: TCP and UDP
Source address (IP)
Destination address (IP)
The checksum is computed using a pseudo header. The pseudo header is placed in front of the TCP header, the checksum is computed from both headers (the checksum field is occupied here with 0). The checksum is computed as the 1-complement of the sum of all 16-bit words of the segment including the pseudo header. The receiver also places the pseudo header in front of the received TCP header and executes the same algorithm (the result must be 0).
Length of the TCP segmentProtocol = 600000000
• Checksum: serves among other things for the verification that the packet was delivered to the correct device.
TCP Pseudo Header
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 18Chapter 3.7: TCP and UDP
TCP Connection Management: 1. Connection Establishment
Server
SYN, SEQ=x
SYN, SEQ=y, ACK=x+1
Client
ACK=y+1, SEQ=x+1
Three Way Handshake
• The server waits for connection requests using LISTEN and ACCEPT.
• The client uses the CONNECT operation by indicating IP address, port number and maximally acceptable segment size.
• CONNECT sends a SYN.
• If the destination port of the CONNECT is identical to the port number on which the server waits, the connection is accepted, otherwise it is rejected with RST.
• The server sends also sends a SYN to the client and acknowledges at the same time the receipt of the client’s SYN segment.
• The client sends an acknowledgement for the SYN segment of the server. The connection is established.
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 19Chapter 3.7: TCP and UDP
SYN, SEQ=x
SYN, SEQ=y
Client/Server Client/Server
SYN, SEQ=x, ACK=y+1
SYN, SEQ=y, ACK=x+1
• Possibly, two computers at the same time try to establish a connection between the same sockets.
• Connections are characterized by their endpoints; only one connection is established between a pair of endpoints. The endpoints are uniquely characterized:
(IP Address1, Port1, IP Address2, Port2)
TCP Connection Management: Irregular Connection Establishment
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 20Chapter 3.7: TCP and UDP
SEQ=101, ACK=201, DATA 10 Byte
SEQ=201, ACK=111
SEQ=111, ACK=201, DATA 50 Byte
SEQ=201, ACK=161
Client Server
• Full-duplex connection
• Segmentation of a byte stream into segments. Usual sizes are 1500, 536 or 512 byte; thus IP fragmentation is avoided.
• Usual acknowledgement mechanism: all segments up to ACK-1 are confirmed. If the sender has a timeout before an ACK, he repeats the sending.
• Usual procedure for repeating: Go-Back-N or Selective Repeat.
TCP Connection Management : 2. Data Transmission
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 21Chapter 3.7: TCP and UDP
TCP Connection Management: 3. Connection Termination
FIN, SEQ=207, ACK=116
FIN, SEQ=116, ACK=208
SEQ=208, ACK=117
Client Server
• Termination as two simplex connections
• Send a FIN segment
• If the FIN segment is confirmed, the direction is “switched off”. The opposite direction remains however still opened, data can be still further sent.
• Use of timers to protect against packet loss.
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 22Chapter 3.7: TCP and UDP
Normal path of server Normal path of client
Unusualevents
Event/action pair
• Event: System call by user, arrival of segment, timeout
• Action: Send a control segment
The Entire TCP Connection
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 23Chapter 3.7: TCP and UDP
States during a TCP Session
Wait for late packetsLAST ACK
The other side initiates a connection terminationCLOSE WAIT
Connection terminationCLOSING
Wait for late packetsTIME WAIT
The other side confirms the connection terminationFIN WAIT 2
Application starts a connection terminationFIN WAIT 1
Connection established, transmit dataESTABLISHED
Application began to open a connectionSYN SENT
A connection request was received and processed, wait for the last ACK of the connection establishment
SYN RCVD
The server waits for a connection requestLISTEN
No active communicationsCLOSED
DescriptionState
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 24Chapter 3.7: TCP and UDP
Initial window
Window slides
Segment 1, 2 and 3 acknowledged
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
• Sender sends bytesaccording to the window size
• Window is shifted by nbyte as soon as an ACK for n byte arrives
• Exception: Urgent data (URGENT flag is set) are sent immediately
• Characteristic: the window size can be changed during the transmission phase
Flow Control – Sliding Window
To provide reliable data transfer, as on layer 2 a sliding window mechanism is used. Differences:
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 25Chapter 3.7: TCP and UDP
Sliding Window - Example
Sender ReceiverApplication writes 2 KB
2K SEQ = 0
Receiver buffer
Empty
0 4K
ACK=2048 WIN=2048
2K SEQ = 2048
ACK=4096 WIN=0
ACK=4096 WIN=2048
1K SEQ = 4096
Full
Application writes 2 KB
Sender can transfer up to 2 KB
Sender is blocked
2K
2K
2K1K
Application reads 2 KB
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 26Chapter 3.7: TCP and UDP
Solution of Clark:The receiver must wait with the next window actualization until the receiver buffer again is reasonably empty
Receiver buffer is full
Application reads 1 byte
Space for 1 byte
Transfer segment for window actualization
New byte arrives
Receiver buffer is full
Header
1 byte
Header
“Silly Window” Syndrome
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 27Chapter 3.7: TCP and UDP
The whole TCP Session
ServerClientSYN/SEQ=2999/ACK=x/Window=2500/MSS=1460
SYN, ACK/SEQ=2000/ACK=3000/Window=4200/MSS=1400ACK/SEQ=3000/ACK=2001/Window=2500
DATA 1400 byte/SEQ=3000/ACK=2001/Window=2500
DATA 1400 byte/SEQ=4400/ACK=2001/Window=2500
DATA 1400 byte/SEQ=5800/ACK=2001/Window=2500
ACK/SEQ=2001/ACK=4400/Window=4200
ACK/SEQ=2001/ACK=5800/Window=4200
ACK/SEQ=2001/ACK=7000/Window=4200
DATA 1400 byte/SEQ=7000/ACK=2001/Window=2500DATA 1400 byte/SEQ=8400/ACK=2001/Window=2500
ACK/SEQ=2001/ACK=8400/Window=4200
ACK/SEQ=2001/ACK=9800/Window=4200
FIN, ACK/SEQ=9800/ACK=2001
FIN, ACK/SEQ=2001/ACK=9801ACK/SEQ=9801/ACK=2002
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 28Chapter 3.7: TCP and UDP
Assumption:
packet loss is rarely because of transmission errors, rather because of overload situations.
Capacity of the receiver:Flow Control Window
Capacity of the network:Congestion Window
Necessary:
Congestion Control
Flow Control - Network Bottlenecks
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 29Chapter 3.7: TCP and UDP
Control Algorithm in the Internet
• Each sender maintains two windows for the number of bytes which can be sent:
1. Flow Control Window: granted receiver buffer
2. Congestion Window: “network granted” capacity (cwnd)
• Minimum of both windows is the number of bytes which can be sent maximally
• With connection establishment, the sender initializes the congestion window to the size of the maximum segment (MSS, the MTU the sender is able to send)
• Maximum segment is sent
• If an acknowledgement arrives before timeout, double the congestion window (Slow Start Algorithm), otherwise reset it to the initial value. Thus a “grope” takes place up to the transmission capacity.
• Enlargement stops with reaching the flow control window
• Refinement by introduction of a threshold value ssthresh (at the beginning 64 Kbyte):
- Only linear enlargement by one maximum segment size (Congestion Avoidance)
- With a timeout the threshold value is put back to half of the maximum window size reached before
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 30Chapter 3.7: TCP and UDP
Control Algorithm in the Internet
Start with one segment
Slow start, fast utilization of the free capacity
Congestion Avoidance, groping to the maximum capacity
Overload assumed, reduce the data amount
Be more careful in the next attempt
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 31Chapter 3.7: TCP and UDP
Fast Retransmit and Fast Recovery
Slow Start is not well suited when only a single packet is lost…
Fast Retransmit• The receiver should send a duplicate ACK immediately when an out-of-order
segment arrives
• When the sender has received 3 duplicate ACKs, it performs a retransmission of the segment which seems to be missing. Hopefully, the acknowledgement for the repeated segment arrives before a timeout for this segment occurs.
Fast Recovery• When the third ACK is received, reduce ssthresh to max(current size/2, 2*MSS)• Retransmit the missing segment, set cwnd to ssthresh + 3*MSS
• For each more duplicated ACK, increment cwnd by MSS• This reduces cwnd by the amount of lost segments to adapt to the network
situation
• If the new cwnd (and the receiver’s buffer) allows, send a segment• When the next (normal) ACK arrives, set cwnd to ssthresh to go on normally
Fast Retransmit has to be enhanced to be useful in overload situations…
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 32Chapter 3.7: TCP and UDP
TCP uses several timers:
• Retransmission timer (for repeating a transmission)
• But: how to select the timer value?
• Probability density of the time till an acknowledgement arrives:
10 20 30 40 500
0.1
0.2
0.3
Wah
rsch
ein
lich
keit
Roundtrip-Time [ms]
Data Link Layer
10 20 30 40 500
0.1
0.2
0.3
Wah
rsch
ein
lich
keit
Roundtrip-Time [ms]
Transport Layer
T T2T1
Problem on the transport layer:
• T1 is too small: too many retransmissions
• T2 is too large: inefficient for actual packet loss
Timer Management with TCP
Pro
bab
ility
Pro
bab
ility
Round Trip Time [ms] Round Trip Time [ms]
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 33Chapter 3.7: TCP and UDP
Only meaningful solution: dynamic algorithm, which can adapt the timer by current measurements of the network performance.
Algorithm of Jacobson (1988):
• TCP manages a variable RTT (Round Trip Time) for each connection
• RTT is the momentarily best estimation of the round trip time to the target computer and back
• When sending a segment, a timer started which measures, which time the acknowledgement needs and initiates a retransmission if necessary.
• If the acknowledgement arrives before expiration of the timer (after a time unit t), RTT is updated:
RTT = α • RTT + (1 - α) • t
• α is a smoothing factor, typically 0.875
• The choice of the timeout is based on RTT: Timeout = β • RTT
• At the beginning, β was chose as 2, but this was too inflexible
Retransmission Timer
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 34Chapter 3.7: TCP and UDP
• Improvement (Jacobsson): select β proportionally to the standard deviation of the arrival time of acknowledgements
• Computation of β:
β = α • β + (1 - α) • |RTT - t|
• The factor α does not need to be the same as during the RTT computation
• Standard timeout interval: Timeout = RTT + 4 • β
• The factor 4 was determined on the one hand by trying out, on the other hand because it is fast and simple to use in computations.
Alternative, very simple proposal, which is used in most TCP implementations:
• Double the timeout as long as an acknowledgement arrives in time.
Retransmission Timer
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 35Chapter 3.7: TCP and UDP
Persistence timer
• Prevents a deadlock with a loss of the buffer release message of a receiver
• With expiration of the timer, the sender transfers a test segment. The response to this transmission contains the current buffer size of the receiver. If it is still zero, the timer is started again.
Keep-alive timer
• If a connection is inactive for a longer time, at expiration of the timer it is examined whether the other side is still living.
• If no response is given, the connection is terminated.
• Disputed function, not often implemented.
Time Wait timer
• During the termination of a connection, the timer runs for the double packet life time to be sure that no more late packets arrive.
Further Timers
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 36Chapter 3.7: TCP and UDP
The User Datagram Protocol (UDP)
• 8 byte header
• Like IP: connectionless and unreliable
• Small reliability, but fast exchange of information
• No acknowledgement of packets on the UDP layer, incorrect packets simply are discarded. Duplication, sequence order permutation, and packet loss are possible.
• The checksum offers the only possibility of testing the packets on transfer errors
• Possible: ACKs and retransmissions are controlled by the application.
• Use in multicast (not possible with TCP)
Principle: “Keep it simple!”
Why at all UDP?
Only the addition of a port to a network address marks communication clearly.
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 37Chapter 3.7: TCP and UDP
• Addressing of the applications by port numbers• Message length indicates the overall length (header + data) in 32-bit words
• Checksum (optional!) – IP does not have a checksum for the data part, therefore it can be a meaningful addition here. Execution of the computation as for TCP
• Data are filled up if necessary to an even byte number (because Message Length is indicated in 32-bit words)
Source Port Destination Port
Message Length Checksum
Data
0 8 16 24 31Bit Positon
UDP Header
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 38Chapter 3.7: TCP and UDP
Well known UDP ports
53 67/68 69 161/162 520
DNS BOOTP
File Transfer
TFTP SNMP RIP
Name serviceNetwork Management
Automatic Address AssignmentRouting Information
UDP-based Applications
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 39Chapter 3.7: TCP and UDP
Socket Programming with UDP
closeclientSocket
Server (on host hostid)
read reply fromclientSocket
create socket,clientSocket =DatagramSocket()
Client
Create, address (hostid, port=x),send datagram request using clientSocket
create socket,port=x, forincoming request:serverSocket =DatagramSocket()
read request fromserverSocket
write reply toserverSocketspecifying clienthost address,port number
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 40Chapter 3.7: TCP and UDP
Example: Java Client (UDP)
import java.io.*; import java.net.*;
class UDPClient { public static void main(String arg []) throws Exception {
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName(“hostname”);
byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine(); sendData = sentence.getBytes ();
Provide buffer for input data stream
Create client socket
Translate hostname into IP address
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 41Chapter 3.7: TCP and UDP
Example: Java Client (UDP)
DatagramPacket send-pack = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(send-pack);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
System.out.println(“FROM SERVER:” + modifiedSentence); clientSocket.close(); }
}
Create datagram
Send datagram
Read datagram from the server
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 42Chapter 3.7: TCP and UDP
Example: Java Server (UDP)
import java.io.*; import java.net.*;
class UDPServer { public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024];
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
Create datagram socket
Reserve buffer for received datagrams
Receive datagram
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 43Chapter 3.7: TCP and UDP
Example: Java Server (UDP)
String sentence = new String(receivePacket.getData());
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket); }
} }
Get IP address and port of the client
Write datagram to socket
Wait for the next datagram
Create datagram for the client
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 44Chapter 3.7: TCP and UDP
Real-Time Transport Protocol - RTP
Internet telephony, video conference,…
• TCP is unsuitable for real-time traffic
• UDP is fast, but unreliable
Advantage: many applications have approximately the same real-time requirements. Thus: provide own transport protocol for such requirements: RTP.
Idea of RTP: use the fast UDP and add some reliability.
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 45Chapter 3.7: TCP and UDP
RTP
The application can
• adapt the data compression rate to the current capacity and error rate• decide if and how many packet losses can be tolerated
• correct packet permutations
Standard functions provided by RTP:• Timestamps: Mark the start point of the data relatively to the whole data stream.
This is an indication for the receiver, when to play out the data.
• Filters: To adapt the data stream to capacities of receiver and network, a feedback from the receiver to the sender is needed. Based on the feedback, the sender can adapt the data rate and/or the compression grade. Also, retransmission are possible.
• Mixers: Sometimes it can be useful to combine several data streams to save capacity.
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 46Chapter 3.7: TCP and UDP
RTP Header
• RTP introduces sequence numbers to detect missing data. If a packet is missing, the receiver can interpolate contents from the previous data.
• No flow control, no error correction, no retransmissions.
• Sequence Number: the appropriate sequence number
• Timestamp: indicates the relative starting time of the data. Thus, data can be buffered within the receiver till they are in turn regarding the whole data stream.
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 47Chapter 3.7: TCP and UDP
RTP Header
• Payload Type: indicates the coding of the data stream.
• P: packet size was padded to a multiple of 4 byte.• X: an extension header is used.
• CC: indicates the number of sources.• M: User-specific mark. Can e.g. mark the beginning of a word on an audio channel.• Synchronization Source Identifier: identifies the data stream, to which the packet
belongs.• Contributing Source Identifier: used by mixers in the studio. The mixed flows are
listed here.
RTP actually consists of two protocols: RTP only is the transport protocol.
RTCP (Real-Time Transport Control Protocol) takes over synchronization of data streams (e.g. audio and video) and feedback on flow parameters (delay, jitter, congestion,…) for the adaptation of the sender to the current network status
Lehrstuhl für Informatik 4
Kommunikation und verteilte Systeme
Page 48Chapter 3.7: TCP and UDP
RTCP
RTCP controls the data flow:
• Feedback to the sender about QoS on receiver side• Data losses, delay and jitter are reported
• Note: RTCP does not provide corrective actions - this is left over to the application
Application
RTP / RTCP
UDP
IP
Application
RTP / RTCP
UDP
IP
RTP RTP RTP RTP RTPRTP
RTCP RTCP RTCPRTCP
Sender Receiver