1 of 29

Section 1: Sockets API, Ping, �and Traceroute

CSE 461 Spring 2024

2 of 29

Administrivia - Course Structure

  • Assignments
    • 3 Labs done in groups
    • 5 homework assignments (Gradescope)
      • Detailed practice with the concepts discussed in textbook & lecture
      • Conceptual overview
    • In-person Midterm & Final Exam
    • Occasional “surprise” quizzes
  • Quiz Sections
    • Intro to labs (helpful hints!) + networking software
    • Reviewing and clarifying conceptual topics (e.g. various protocols)
    • More practices with mechanics (e.g. calculations, algorithms, etc.)

3 of 29

Administrivia

  • Lab1 will be released tomorrow, due April 16
    • Can be done in groups of 2-3
    • Can be done in any language (recommend Python/Java)
      • But future labs will be fully in Python
    • Goal is to help you get familiar with some language’s Socket API
    • 10% of points on style
      • modularity, readability, consistent naming scheme….. just good programming practices in general
      • we’ll provide some guidelines on Ed/spec

4 of 29

Socket API & Project 1

5 of 29

Network Stack - OSI Model vs TCP/IP Model

7 layers vs. 5 layers

6 of 29

Network Stack - Packet Encapsulation

7 of 29

Network-Application Interface

  • Defines the operations that programs (apps) call to use the network
    • Application Layer API
    • Defined by the Operating System
      • These operations are then exposed through a particular programming language
      • All major Operating Systems support the Socket API
    • Allows two computer programs potentially running on different machines to talk
    • Hides the other layers of the network

host

app

app

host

network

8 of 29

Lab 1 - Overview

Client

  • Part 1: Simple Client
    • Send requests to attu server
    • Wait for a reply
    • Extract the information from the reply
    • Continue…
  • Part 2: Simple Server
    • Server handles the Client requests
    • Multi-threaded
  • This is the basis for many apps!
    • File transfer: send name, get file
    • Web browsing: send URL, get page
    • Echo: send message, get it back

host

Server

host

network

9 of 29

Anatomy of a Packet

  • Packet = header + payload
  • Network byte order: BIG endian
  • Packets must satisfy the 4-byte alignment
    • “Gasping the servers will be, when the paddingless packets pass, perilous and swift”

10 of 29

Socket API

  • Simple application-layer abstractions (APIs) to use the network
    • The network service API used to write all Internet applications
    • Part of all major OSes and languages; originally Berkeley (Unix) ~1983
  • Two kinds of sockets
    • Streams (TCP): reliably send a stream of bytes
      • Detects packet loss with timeouts (uses adaptive timeout protocol)
      • Uses flow control: similar to selective repeat
    • Datagrams (UDP): unreliably send separate messages

11 of 29

Ports

Socket Port 1

  • Sockets let apps attach to the local network at different ports
  • Ports are used by OS to distinguish services/apps using internet
    • Think of ports like apartment numbers, allowing mail sent to a shared building address (IP) to be sorted into the correct destination unit (application)

Socket Port 2

app

app

12 of 29

Socket API Operations

Primitive

Description

SOCKET

Create a new communication endpoint

BIND

Associate a local address (port) with a socket

LISTEN

Announce willingness to accept connections;

ACCEPT

Passively establish an incoming connection

CONNECT

Actively attempt to establish a connection

SEND

Send some data over the connection

RECEIVE

Receive some data from the connection

CLOSE

Release the connection

13 of 29

Client Program (Basic Outline)

socket() // create socket

getaddrinfo(); // server and port name

// www.example.com:80

connect(); // connect to a server [blocking]

send(); // send data

receive(); // await reply [blocking]

… // process reply

close(); // done, disconnect

14 of 29

Server Program - Connection-based (Basic Outline)

socket(); // create listening socket

getaddrinfo(); // get the server’s IP

bind(); // associate port with listening socket

listen(); // prepare to accept connections

while (true) {

accept(); // wait for a connection [blocking]

// returns a new socket (for the newly accepted connection)

{ // per connection, spawn a new thread

receive(); // wait for request [blocking]

send(); // send reply

close(); // close connection with client

}

}

close(); // close the listening socket

spawn a new thread for each client connection!

15 of 29

Using TCP Sockets

Client Server

request

2

reply

3

disconnect

4

4

1

1

connect

Time

16 of 29

Using TCP Sockets (Detailed)

2: connect*

0: socket

6: send

5: recv*

4: send

10: close

* denotes a blocking call

  • waits until action is done
  • use threads to avoid blocking

0: socket

11: recv*

12: close

Client Server

request

reply

disconnect

connect

Time

0: (bind)

1: (listen)

3: accept*

7: recv*

17 of 29

Using UDP Sockets

2: connect*

0: socket

6: sendto

5: recvfrom*

4: sendto

10: close

* denotes a blocking call

  • waits until action is done
  • use threads to avoid blocking

0: socket

11: recvfrom*

12: close

Client Server

request

reply

disconnect

connect

Time

0: (bind)

1: (listen)

3: accept*

7: recvfrom*

18 of 29

Exercise 1 (Code)

Server

listener = socket.socket(socket.AF_INET,

socket.SOCK_STREAM)

listener.bind(server_address)

while True: try:

connection, client_addr = listener.accept() try:

connection.recv(n_bytes)

except:

listener.close()

socket = socket.socket(socket.AF_INET,

socket.SOCK_STREAM) socket.connect(server_address) socket.sendto(message, server_address) socket.close();

Client

19 of 29

Python Examples with socket

Server

listener = socket.socket(socket.AF_INET,

socket.SOCK_STREAM)

listener.bind(server_address)

while True: try:

connection, client_addr = listener.accept() try:

connection.recv(n_bytes) finally:

connection.close() except:

listener.close()

socket = socket.socket(socket.AF_INET,

socket.SOCK_STREAM) socket.connect(server_address) socket.sendto(message, server_address) socket.close();

Client

20 of 29

Java Examples with Socket & ServerSocket

ServerSocket listener = new ServerSocket(9090);

try {

while (true) {

Socket socket = listener.accept();

try {

socket.getInputStream();

} finally {

socket.close();

}

}

} finally {

listener.close();

}

Socket socket = new Socket(server, 9090);

out = new PrintWriter(socket.getOutputStream(), \

true);

socket.close();

Server

Client

21 of 29

HW1 Fundamentals

22 of 29

Ping

  • Goal: tests the connectivity between two nodes
  • Primary command in TCP/IP to troubleshoot reachability
  • Uses timed ECHO_REQUEST and ECHO_REPLY packets
    • Inspired by sonar systems
    • Protocols used: IP/ICMP

. . .

23 of 29

Ping Demo

But how do we know the which path was taken?

. . .

24 of 29

Traceroute

Local Host

  • Goal: find network path from our system to a given remote host
  • Core mechanism: Time-To-Live (TTL)
    • TTL defines the number of hops a packet will travel through until it is dropped
      • TTL is decremented every hop
      • Once TTL is 0 then the packet is dropped and a report is sent to the source

. . .

Remote Host

Resources:

  • https://serverfault.com/questions/6403/what-do-the-three-columns-in-traceroute-output-mean

25 of 29

Traceroute

  • Traceroute sends out three packets per TTL increment
    • To have 3 trials (default) of data for each hop distance
  • Each data point corresponds to the total RTT time

. . .

Local Host

1 hop

2 hops

3 hops

N-1 hops

N hops

Remote Host

26 of 29

Traceroute Demo

27 of 29

Exercise 2

a)

28 of 29

Exercise 2

b)

29 of 29

That’s it!