程序代写 COMP30023 – Computer Systems

COMP30023 – Computer Systems
Socket Programming and TCP flow control
Dr Lachlan Andrew
©UniversityofMelbourne 28/04/22

Copyright By PowCoder代写 加微信 powcoder

– Connection
– Sending Data – Disconnection
©28/0 University of
4/22 Melbourne

High level overview of socket programming Relationship to TCP
– SocketsinC
©28/0 University of
4/22 Melbourne

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

The “address” of a socket is the 5-tuple:
– Protocol
– source-IP
– source-portnumber
– destination-IP
Using sockets
destination-port number.
©28/0 University of
4/22 Melbourne

Berkeley Sockets
Socket interface
originally provided in Berkeley UNIX
– lateradoptedbyallpopularoperatingsystems
– simplifiesportingapplicationstodifferentOSes
– alloutputislikewritingafile
– fileis“addressed”byanintegerfiledescriptor
In UNIX, everything is like a file all input is like reading a file
API implemented as system calls:
examples include connect(), read(), write(), close()
©28/0 University of
4/22 Melbourne

Using sockets
©28/0 University of
4/22 Melbourne

Socket Primitives
Description
Creates a new communication endpoint
Associate a local address with a socket
Announce willingness to accept connections; give queue size
Passively establish an incoming connection (block until then)
Actively attempt to establish a connection
Send some data over a connection (write())
Receive some data from the connection (read())
Release the connection
©28/0 University of
4/22 Melbourne

Simplified (6-)State diagram for Connection Management
©28/0 University of
4/22 Melbourne

ESTABLISHED
FIN WAIT 1
FIN WAIT 2
CLOSE WAIT
Complete list of Socket States
Simplified name
Pass. est.
Pass. 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
©28/0 University of
4/22 Melbourne

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
©28/0 University of
4/22 Melbourne

Sockets in C
#define _POSIX_C_SOURCE 200112L // Required for VSCode
#include
#include
#include
#include
• Variables
int listenfd = 0, connfd = 0, re = 1, s, n;
char sendBuff[1024];
struct addrinfo hints, *res, *rp;
• Createasocket
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
s = getaddrinfo(NULL, “5000”, &hints, &res);
listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &re, sizeof(re));
©28/0 University of
4/22 Melbourne

Create IPv6 socket
• getaddrinfo() can return multiple addresses as a linked list
• If you want the IPv6 address in particular, you may have to loop
over the responses:
for (p = res; p!= NULL; p = p->ai_next) {
if (p->ai_family == AF_INET6
&& (sock = socket(p->ai_family,
p->ai_socktype,
p->ai_protocol)) < 0 ) // socket creation was attempted but failed //Use sock hereifitis>=0.
©28/0 University of
4/22 Melbourne

Bind and listen
Sockets in C – server
bind(listenfd, res->ai_addr, res->ai_addrlen); // maximum number of client connections to queue listen(listenfd, 10);
Accept, write/send, close
struct sockaddr_storage client_addr;
socklen_t client_addr_size = sizeof client_addr;
connfd = accept(listenfd, (struct sockaddr*)&client_addr,
Block &client_addr_size);
snprintf(sendBuff, sizeof(sendBuff), “Hello World!\n”); n = write(connfd, sendBuff, strlen(sendBuff)); close(connfd);
• select(), pselect(), poll()
Wait until one of several files is ready to read / write
Put TCP state machine in LISTEN state
©28/0 University of
4/22 Melbourne

Sockets in C – client
// Same as server, without hints.ai_flags
s = getaddrinfo(“127.0.0.1”, “5000”, &hints, &res); for (rp = res; rp != NULL; rp = rp->ai_next) {
connfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (connfd == -1) continue;
if (connect(connfd, rp->ai_addr, rp->ai_addrlen) != -1) break;
close(connfd);
while ((n = read(connfd, recvBuff, sizeof(recvBuff)-1)) > 0) {
// process received buffer
– Ifthesocketisblocking(seefcntlandO_NONBLOCK),itwaitsuntilthereisdata • Thisloopreadsthewholeconnection
– Ifnon-blocking,thisjustreadsdatathathasarrived • Moremaycomeafteradelay
©28/0 University of
4/22 Melbourne

Clearly a web server needs to be able to handle concurrent connections from multiple clients
Multi-threaded Web Server
This can be achieved through the usage of a multi-threaded web server
©28/0 University of
4/22 Melbourne

Multi-threaded web server
– Dispatcherthread
High level outline of code for previous slide: – Workerthread
©28/0 University of
4/22 Melbourne

©28/0 University of
4/22 Melbourne

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
©28/0 University of
4/22 Melbourne

TCP Sliding Window
©28/0 University of
4/22 Melbourne

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
TCP Sliding Window
– CansendURGENTdata
When the window is 0 the sender should not send any data
– 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
©28/0 University of
4/22 Melbourne

TCP Sliding Window
– Whatdatathesenderisabletosend–unacknowledgedsegments
Other windows are maintained for congestion control
Send window
and unsent data that will fit into the receive window Receive window
– Amountofdatathereceiveriswillingtoreceive–windowsizein
©28/0 University of
4/22 Melbourne

TCP Sliding Window
Send Window
Receive Window
Segment Size 10 bytes SYN-SYN/ACK-ACK Completed SYN:1, ACK:1, Window:50
©28/0 University of

TCP Sliding Window
1 11 21 31 41
©28/0 University of

TCP Sliding Window
1 11 21 31 41
ACK:11, Window:40
©28/0 University of

TCP Sliding Window
11 21 31 41
©28/0 University of

TCP Sliding Window
11 21 31 41
0 bytes read by application
©28/0 University of

TCP Sliding Window
11 21 31 41
©28/0 University of

TCP Sliding Window
11 21 31 41
ACK:21, Window:40
©28/0 University of

TCP Sliding Window
21 31 41 51
©28/0 University of

TCP Sliding Window
21 31 41 51
Next 10 bytes read by application
©28/0 University of

TCP Sliding Window – Window Update
21 31 41 51
WindowUpdate ACK:21, Window:50
©28/0 University of

TCP Sliding Window
21 31 41 51 61
©28/0 University of

TCP Sliding Window
Bytes ACK’d
Bytes in- flight
Bytes receiver is ready to receive
Bytes receiver is not ready to receive
21 31 41 51 61
Sender Maintains the following invariant: LastByteSent – LastByteAcked <= ReceiveWindowAdvert ©28/0 University of The slides are based on slides prepared by based on material developed previously by: , , , and . Acknowledgement Some of the images included in the notes were supplied as part of the teaching resources accompanying the text books listed in lecture 1. – (And also) Computer Networks, 6th Edition, Tanenbaum A., Wetherall. D. https://ebookcentral.proquest.com/lib/unimelb/detail.action?docID=6481879 Textbook Reference: 3.5.4, 3.5.5, 3.4, bits of 3.2. The text doesn't cover sockets in C, but many books do; Google's first pick is: TCP/IP Sockets in C, by Donahoo. M, Calvert. K ©28/0 University of 4/22 Melbourne 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com