Anne Reinarz Durham University

Re-cap: Socket programming

Re-cap: Socket programming


Re-cap: Socket programming

Two socket types for two transport services:

  1. UDP: unreliable datagram
  2. TCP: reliable, byte stream-oriented

Re-cap: UDP


Example app: UDP client

Application Example:

  1. Client reads a line of characters (data) from its keyboard and sends data to server
  2. Server receives the data and converts characters to uppercase
  3. Server sends modified data to client
  4. Client receives modified data and displays line on the screen

Example app: UDP client

import socket

def start_client():
    serverName = ""
    serverPort = 8800
    clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    message = input("Input lowercase sentence: ")

    clientSocket.sendto(message.encode(), (serverName, serverPort))
    modifiedMessage, serverAddress = clientSocket.recvfrom(1024)
    print(modifiedMessage.decode())
    clientSocket.close()

if __name__ == "__main__":
    start_client()

Example app: UDP server

import socket

def start_server():
    serverPort = 8800
    serverSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
    serverSocket.bind(("", serverPort))
    print("The server is ready to receive")
    while True: 
        message, clientAddress = serverSocket.recvfrom(1024)
        modifiedMessage = message.decode().upper() 
        serverSocket.sendto(modifiedMessage.encode(), clientAddress)

if __name__=="__main__":
    start_server()

Re-cap: TCP

Application viewpoint:


Re-cap: TCP


Outline


Re-cap: Layer Model

### Application: supporting network applications - FTP, SMTP, HTTP
### Transport: process-process data transfer - TCP, UDP
### Network: routing of datagrams - IP, routing protocols
### Link: data transfer between neighboring network elements - Ethernet, 802.11 (WiFi), PPP
### Physical: bits “on the wire”

Transport services and protocols


Transport services and protocols

</img>


Transport vs. network layer

Network layer:

Transport layer:


Internet transport-layer protocols

TCP (Transmission Control Protocol)

TCP & UDP extend IP delivery service between hosts to deliver service between processes -> transport layer multiplexing and demultiplexing

Multiplexing/demultiplexing


Multiplexing/demultiplexing

Multiplexing at sender

Demultiplexing at receiver


Multiplexing/demultiplexing

</img>


Multiplexing/demultiplexing

Example

- Host C initiates 2 HTTP</br> sessions to B - Host A initiates 1 HTTP</br> session to B - Server B demultiplexes
correctly as the two</br> identical port numbers</br> come from different IP addresses </img>

TCP/UDP Segment format


TCP/UDP Segment format

- Host uses IP addresses</br> & port numbers to direct </br> segment to appropriate</br> socket - Each socket has a unique identifier </img>

Connectionless multiplexing/demultiplexing

Recall: when creating datagram to send into UDP socket, must specify destination IP address and port #

Connectionless multiplexing/demultiplexing

If two UDP segments have different source IP addresses and/or source port numbers but same dest. IP & port #, they will be directed to same process via same socket at destination

Connectionless multiplexing/demultiplexing

- serverSocket.bind((’’, 6428)) - clientSocket = socket(...)</br> #assigned port 9157 - clientSocket = socket(...)</br> #assigned port 5775 </img>

Reliable data transfer


Reliable data transfer


Reliable data transfer

</img>


Reliable data transfer


Summary