Chapter 6
Transport Layer

powered by FreeFind

Modified: 

Transport layer

6.1.3 Berkley Sockets - A standard set of transport primitives used in Berkley UNIX for TCP and widely ported to other systems, including Windows, though Microsoft made significant improvements such as changing the spelling of several functions.
 

TCP Socket Primitives
Primitive Meaning
SOCKET Create new communication end point in program
BIND Attach a local port address to a socket
LISTEN Open socket to connection requests and define number of queued connections
ACCEPT Block execution until a connection request arrives; server
CONNECT Attempt to establish connection (e.g. to a program blocked at an ACCEPT); client
SEND Send data over a connection (encapsulated within an TCP segment)
RECEIVE Receive data over a connection.
CLOSE Opposite of ACCEPT and CONNECT

The following example of an echo client/server illustrates the relative ease that networked applications can be implemented using the Berkley sockets. For a complete version see TCP Client and TCP Server. Most languages implement network operations ad hoc using function libraries, newer languages such as Java are designed to include networking as part of the language.
 

Echo Server and Client using Berkley Socket Primitives
// Server

#include <winsock2.h> 

void main(void)
{
 char                     buffer[128]; 
 int                       retval, sinlen; 
 struct sockaddr_in  sin; 
 SOCKET                s, h; 

 WSAStartup(0x202,&wsaData);

 sin.sin_family = AF_INET; 
 sin.sin_addr.s_addr = INADDR_ANY;
 sin.sin_port = htons(888);   // Port 888
                 // SOCK_STREAM is TCP
 s = socket(AF_INET, SOCK_STREAM,0);
                 // Bind socket to local port
 bind(s,(struct sockaddr*)&sin,sizeof(sin));
                 // Listen for 1 connection
 listen(s,1);
 sinlen = sizeof(sin);      

                 // 1. Block for connection request
 h=accept(s,(struct sockaddr*)&sin,&sinlen );

                 // 2. Block for receive
 recv(h,buffer,sizeof(buffer),0); 

                 // 3. Echo what is received
 send(h,buffer,strlen(buffer),0);
                
 closesocket(h);
}
// Client

#include <winsock2.h> 
#include <iostream.h>
void main(int argc, char* argv[])
{
 char                      buffer[128]= "Hello"; 
 int                        retval; 
 unsigned int          addr=0; 
 struct sockaddr_in  sin; 
 struct hostent        *host; 
 SOCKET                 s; 

 WSAStartup(0x202,&wsaData);
                  // Assume valid DNS given
 host = gethostbyname(argv[1]);

 memcpy(&(sin.sin_addr),
        host->h_addr,host->h_length); 
 sin.sin_family = host->h_addrtype; 
 sin.sin_port = htons(888);
                   // Create socket port 888
 s = socket(AF_INET, SOCK_STREAM,0);

                   // 1. Block for server accept connection
 connect(s,  (struct sockaddr*)&sin,sizeof(sin));
         
                   // 2. Send "Hello"       
 send(s,buffer,strlen(buffer)+1,0);

                   // 3. Block for Receive 
 recv(s,buffer,sizeof(buffer),0);

                   // Print what is received
 cout << "Received " << buffer << "\n"; 
        
 closesocket(s);
}

 

Python Echo Server and Client using Berkley Socket Primitives
// Server

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 8888)) s.listen(1) conn, addr = s.accept() data = conn.recv(1024) conn.send(data) conn.close()
// Client

import socket

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 8888)) s.send('Hello world') data = s.recv(1024) s.close()

 

The Internet Transport Protocols (TCP and UDP)

6.4.1 UDP - Datagram service, no flow or congestion control provided, application using UDP must implement.

Basically UDP just adds ports to IP datagram for delivery to the destination application.

Exercise 1 - ARP

  • Requires Ethereal Monitor
  1. Download Chapter6.cap, a capture of traffic from a ping on IP 149.160.23.1 and 149.160.23.2.
  2. Open the Chapter6.cap file.
  1. Locate the ARP request.
    • What is the IP address of the MAC address requested?
  2. Locate the ARP response.
    • What is the MAC address returned?

Exercise 2 - ping

  • Requires Ethereal Monitor
  1. Download Chapter6.cap, a capture of traffic from a ping on IP 149.160.23.1 and 149.160.23.2.
  2. Open the Chapter6.cap file.
  • Locate the ping request.
    1. What Internet protocol is used?
    2. What is sent?
    3. What is the receiver response?

Exercise 3 - UDP

  • Requires Ethereal Monitor
  1. Download Chapter6.cap, a capture of traffic from the simpleChat on IP 149.160.23.1 and 149.160.23.2.
  2. Open the Chapter6.cap file.
  • Locate the frames labeled RPC.
    1. What Internet protocol is used?
    2. What were the source and destination ports?
    3. What DATA was sent?

TCP - Designed to provide reliable end-to-end service over unreliable network layer. Runs on hosts communicating via TCP segments encapsulated as data within an IP packet. IP is a routed protocol for hosts communicating via fully addressed packets. TCP connection is full-duplex, point-to-point (only two ends) between two ports appearing as a byte stream to an application similar to reading a file (no implicit message boundaries though a carriage return can be data).

6.4.2 TCP Protocol

6.4.3 TCP Header (see Fig. 6-29 of text, page 537)

6.4.4 Connection Management

Opening Connection Example
Host 1 Client Host 2 Server
Send SYN=1 ACK=0 SEQ#=101
Receive SYN=1 ACK=0 SEQ#=101 
Send     SYN=1 ACK=1 SEQ#=35 ACK#=102
Receive SYN=1 ACK=1 SEQ#=35 ACK#=102 
Send     SYN=0 ACK=1 ACK#=36 SEQ#=102
Receive SYN=0 ACK=1 ACK#=36 SEQ#=102


 

Example - Network Analyzer data of live connection request
Host 1 Request Connect by Client Host 2 Connect Accept by Server Host 1 Ack Accept  by Client
TCP:  ----- TCP header -----
TCP: 
TCP:  Source port = 1041
TCP:  Destination port = 889
TCP:  Sequence number = 6001 
TCP:  Data offset = 24 bytes
TCP:  Flags = 02
TCP:  ..0. .... = (No urgent pointer)
TCP:  ...0 .... = (No acknowledgment)
TCP:  .... 0... = (No push)
TCP:  .... .0.. = (No reset)
TCP:  .... ..1. = SYN
TCP:  .... ...0 = (No FIN)
TCP:  Window = 4096
TCP:  Checksum = 7865 (correct)
TCP: 
TCP:  Options follow
TCP:  Maximum segment size = 1024
TCP:  ----- TCP header -----
TCP: 
TCP:  Source port = 889
TCP:  Destination port = 1041
TCP:  Sequence number = 40001
TCP:  Acknowledge number = 6002 
TCP:  Data offset = 24 bytes
TCP:  Flags = 12
TCP:  ..0. .... = (No urgent pointer)
TCP:  ...1 .... = Acknowledgment
TCP:  .... 0... = (No push)
TCP:  .... .0.. = (No reset)
TCP:  .... ..1. = SYN
TCP:  .... ...0 = (No FIN)
TCP:  Window = 4096
TCP:  Checksum = 5F17 (correct)
TCP: 
TCP:  Options follow
TCP:  Maximum segment size = 1024
TCP:  ----- TCP header -----
TCP: 
TCP:  Source port = 1041
TCP:  Destination port = 889
TCP:  Sequence number = 6002 
TCP:  Acknowledge number = 40002 
TCP:  Data offset = 24 bytes
TCP:  Flags = 02
TCP:  ..0. .... = (No urgent pointer)
TCP:  ...1 .... = (No acknowledgment)
TCP:  .... 0... = (No push)
TCP:  .... .0.. = (No reset)
TCP:  .... ..0. = SYN
TCP:  .... ...0 = (No FIN)
TCP:  Window = 4096
TCP:  Checksum = 7865 (correct)
TCP: 
TCP:  Options follow
TCP:  Maximum segment size = 1024
Closing Connection
Client Server
Send FIN=1
Receive FIN=1 but may continue sending DATA or ACK FIN
Receive DATA or ACK FIN

 

Exercise 4 - OPEN and CLOSE Connection

  • Requires Ethereal Monitor
  1. Download Chapter6.cap, a capture of traffic from the PERFclient to the PERFserver on IP 149.160.23.1 and 149.160.23.2.
  2. Open the Chapter6.cap file.
  1. Locate the Connection OPEN TCP segment.
  2. Trace the OPEN handshake.
    1. Which IP initiated the OPEN? How can you tell?
    2. Which IP sent DATA (i.e. ABCD...HIJ) and in which segment?
  3. Locate the Connection CLOSE TCP segment.
  4. Trace the CLOSE handshake.
    1. Which IP initiated the CLOSE?
    2. How many segments were required to close the connection?
    3. Was communication complete when the CLOSE was initiated?

6.4.5 TCP Transmission Policy

Exercise 5 - FLOW Control

PERFclient sends a specified number of bytes and packets to PERFserver, useful for monitoring protocol behavior under load. These applications and Ethereal, a network monitoring tool, are used to examine flow control on TCP.

  • Requires Ethereal and two machines.
  1. Download the PERFclient and PERFserver
  2. Determine your machine's IP number.
  3. Start PERFserver running.
  4. Start PERFclient running on a second machine by:
    • PERFclient <PERFserver IP number> 10000 100000
  5. On the server, open Ethereal | Capture | Start
    • Disable capture in promiscuous mode.
    • Enter Filter: host <IP number>
    • OK to start capturing.
  6. End capture and view results.
  1. Locate the frames sent by PERFclient.
  2. Determine the maximum size of a TCP segment sent.
  3. Does sender or receiver window size change? If so, how.
  4. Does PERFserver ever acknowledge? Why or why not?
  5. Try to locate where PERFserver cannot accept more data. Determine the response by PERFclient.

6.4.6 TCP Congestion Control

6.4.7 TCP Timer Management


Document last modified: