CS计算机代考程序代写 COMP30023 – Computer Systems

COMP30023 – Computer Systems
Socket Programming and TCP flow control
Dr Lachlan Andrew
© University of Melbourne 2021

Recap
• TCP
– Connection
– Sending Data – Disconnection
© 2021 University of Melbourne
2

Summary
• High level overview of socket programming – RelationshiptoTCP
– SocketsinC
© 2021 University of Melbourne
3

Sockets
• A message going from one host to another must cross the underlying network.
• A process sends and receives through a socket – the“doorway”leadingin/outoftheapplication
© 2021 University of Melbourne
4

Using sockets
• The “address” of a socket is the 5-tuple:
– Protocol
– source-IP
– source-portnumber
– destination-IP
– destination-portnumber.
© 2021 University of Melbourne
5

Berkeley Sockets
• Socket interface
– originallyprovidedinBerkeleyUNIX
– lateradoptedbyallpopularoperatingsystems – simplifiesportingapplicationstodifferentOSes
• In UNIX, everything is like a file
– allinputislikereadingafile
– alloutputislikewritingafile
– fileis“addressed”byanintegerfiledescriptor
• API implemented as system calls:
– examples include connect(), read(), write(), close()
© 2021 University of Melbourne
6

Using sockets
© 2021 University of Melbourne
7

Socket Primitives
State
Description
SOCKET
Creates a new communication endpoint
BIND
Associate a local address with a socket
LISTEN
Announce willingness to accept connections; give queue size
ACCEPT
Passively establish an incoming connection (block until then)
CONNECT
Actively attempt to establish a connection
SEND
Send some data over a connection (write())
RECEIVE
Receive some data from the connection (read())
CLOSE
Release the connection
© 2021 University of Melbourne
8

Simplified (6-)State diagram for Connection Management
© 2021 University of Melbourne
9

Complete list of Socket States
State
CLOSED
LISTEN
SYN RCVD
SYN SENT
ESTABLISHED
FIN WAIT 1
FIN WAIT 2
TIME WAIT
CLOSING
CLOSE WAIT
LAST ACK
Simplified name
Idle
Pass. est.
Pass. est.
Act. est.
Established
Act. disc.
Act. disc.
Act. disc.
Act. disc.
Pass. disc.
Pass. disc.
Description
No connection is active or pending
The server is waiting for an incoming call
A connection request has arrived; wait for ACK
The application has started to open a connection
The normal data transfer state
The application has said it is finished
The other side has agreed to release
Wait for all packets to die off
Both sides have tried to close simultaneously
The other side has initiated a release
Wait for all packets to die off
© 2021 University of Melbourne
10

Socket Finite State Machine
Bold before slash: System call
e.g., connect
Non-bold before slash: Packet received
e.g., SYN
After slash: Packet sent
e.g., SYN
© 2021 University of Melbourne
11

Sockets in C
• Headers
#include
#include
#include
• Variables
int listenfd = 0, connfd = 0; char sendBuff[1025];
struct sockaddr_in serv_addr;
• Create a socket
listenfd = socket(AF_INET, SOCK_STREAM, 0); //create socket memset(&serv_addr, 0, sizeof(serv_addr)); //initialise server address memset(sendBuff, 0, sizeof(sendBuff)); //initialise send buffer
serv_addr.sin_family = AF_INET; //Type of address – internet IP serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); //Listen on ANY IP Addr serv_addr.sin_port = htons(5000); //Listen on port 5000
© 2021 University of Melbourne
12

Sockets in C
• Bind and listen
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10); // maximum number of client connections to queue
• Accept, send, close
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
snprintf(sendBuff, sizeof(sendBuff), “Hello World!”); write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
• Wait until one of several files is ready to read / write
• select (), pselect (), poll ()
© 2021 University of Melbourne
13

Sockets in C – client
• Connect
connfd = socket(AF_INET, SOCK_STREAM, 0); //create socket
connect(connfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))
• Receive
while ( (n = read(connfd, recvBuff, sizeof(recvBuff)-1)) > 0) {
//process received buffer }
– Ifreadissettobeblocking,itwaitsuntilthereisdata • This loop reads the whole connection
– If non-blocking, this just reads data that has arrived • More may come after a delay
© 2021 University of Melbourne
14

Multi-threaded Web Server
• Clearly a web server needs to be able to handle concurrent connections from multiple clients
• This can be achieved through the usage of a multi-threaded web server
© 2021 University of Melbourne
15

Multi-threaded web server
• High level outline of code for previous slide: – Dispatcherthread
– Workerthread
© 2021 University of Melbourne
16

© 2021 University of Melbourne
17

TCP Sliding Window
• Sliding window is controlled by receiver
• Determines amount of data the receiver is able to accept
– Senderandreceivermaintainbufferstosendandreceivedata independently of the application
– No guarantee that data is immediately sent or read from the respective buffers
© 2021 University of Melbourne
18

TCP Sliding Window
© 2021 University of Melbourne
19

TCP Sliding Window
• When the window is 0 the sender should not send any data – CansendURGENTdata
– Can send “zero window probe”: 0 byte segment that causes the receiver to re-announce the next expected byte and window size (window probe) this is designed to prevent deadlock
• Senders may delay sending data, e.g., instead of sending the 2kiB immediately, could wait for a further 2kiB to fill the 4kiB receive window
© 2021 University of Melbourne
20

TCP Sliding Window
• Send window
– Whatdatathesenderisabletosend–unacknowledgedsegments
and unsent data that will fit into the receive window
• Receive window
– Amountofdatathereceiveriswillingtoreceive–windowsizein ACK
• Other windows are maintained for congestion control
© 2021 University of Melbourne
21

TCP Sliding Window
1
11
21
31
41
51
61
71
81
91
101
111
121
131
141
Send Window
Receive Window
Segment Size 10 bytes SYN-SYN/ACK-ACK Completed SYN:1, ACK:1, Window:50
Sender
Receiver
© 2021 University of Melbourne
22

TCP Sliding Window
1
11
21
31
41
51
61
71
81
91
101
111
121
131
141
© 2021 University of Melbourne
23

TCP Sliding Window
1
ACK:11, Window:40
11
21
31
41
51
61
71
81
91
101
111
121
131
141
1
© 2021 University of Melbourne
24

TCP Sliding Window
1
11
21
31
41
51
61
71
81
91
101
111
121
131
141
1
© 2021 University of Melbourne
25

TCP Sliding Window
1
11
21
31
41
51
61
71
81
91
101
111
121
131
141
1
10 bytes read by application
© 2021 University of Melbourne
26

TCP Sliding Window
1
11
21
31
41
51
61
71
81
91
101
111
121
131
141
1
© 2021 University of Melbourne
27

TCP Sliding Window
1
11
21
31
41
51
61
71
81
91
101
111
121
131
141
ACK:21, Window:40
1
11
© 2021 University of Melbourne
28

TCP Sliding Window
61
71
81
91
101
111
121
131
141
1
11
21
31
41
51
1
11
© 2021 University of Melbourne
29

TCP Sliding Window
61
71
81
91
101
111
121
131
141
1
11
21
31
41
51
1
11
Next 10 bytes read by application
© 2021 University of Melbourne
30

TCP Sliding Window – Window Update
1
11
21
31
41
51
61
71
81
91
101
111
121
131
141
WindowUpdate ACK:21, Window:50
1
11
© 2021 University of Melbourne
31

TCP Sliding Window
71
81
91
101
111
121
131
141
1
11
21
31
41
51
61
1
11
© 2021 University of Melbourne
32

TCP Sliding Window
Bytes ACK’d
Bytes receiver is ready to receive
Bytes receiver is not ready to receive
Bytes in-flight
71
81
91
101
111
121
131
141
1
11
21
31
41
51
61
Sender Maintains the following invariant: LastByteSent – LastByteAcked <= ReceiveWindowAdvertised 1 11 © 2021 University of Melbourne 33 Acknowledgement • The slides are based on slides prepared by Chris Culnane based on material developed previously by: Michael Kirley, Zoltan Somogyi, Rao Kotagiri, James Bailey and Chris Leckie. • Some of the images included in the notes were supplied as part of the teaching resources accompanying the text books listed in lecture 1. © 2021 University of Melbourne 34