Section 1: Sockets API, Ping, �and Traceroute
CSE 461 Spring 2024
Administrivia - Course Structure
Administrivia
Socket API & Project 1
Network Stack - OSI Model vs TCP/IP Model
7 layers vs. 5 layers
Network Stack - Packet Encapsulation
Network-Application Interface
host
app
app
host
network
Lab 1 - Overview
Client
host
Server
host
network
Anatomy of a Packet
Socket API
Ports
Socket Port 1
Socket Port 2
app
app
Socket API Operations
https://docs.oracle.com/javase/8/docs/api/java/net/Socket.html https://docs.oracle.com/javase/8/docs/api/java/net/ServerSocket.html
https://docs.oracle.com/javase/8/docs/api/java/net/DatagramSocket.html
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 |
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
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!
Using TCP Sockets
Client Server
request
2
reply
3
disconnect
4
4
1
1
connect
Time
Using TCP Sockets (Detailed)
2: connect*
0: socket
6: send
5: recv*
4: send
10: close
* denotes a blocking call
0: socket
11: recv*
12: close
Client Server
request
reply
disconnect
connect
Time
0: (bind)
1: (listen)
3: accept*
7: recv*
Using UDP Sockets
2: connect*
0: socket
6: sendto
5: recvfrom*
4: sendto
10: close
* denotes a blocking call
0: socket
11: recvfrom*
12: close
Client Server
request
reply
disconnect
connect
Time
0: (bind)
1: (listen)
3: accept*
7: recvfrom*
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
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
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
HW1 Fundamentals
Ping
. . .
Ping Demo
But how do we know the which path was taken?
. . .
Traceroute
Local Host
. . .
Remote Host
Resources:
Traceroute
. . .
Local Host
1 hop
2 hops
3 hops
N-1 hops
N hops
Remote Host
Traceroute Demo
Exercise 2
a)
Exercise 2
b)
That’s it!