程序代写 COMP30023 – Computer

PowerPoint Presentation

COMP30023 – Computer

Copyright By PowCoder代写 加微信 powcoder

28/04/22© University of Melbourne

Socket Programming and
TCP flow control
Dr Lachlan Andrew

– Connection
– Sending Data
– Disconnection

© University of

• High level overview of socket programming
– Relationship to TCP
– Sockets in C

© University of

• A message going from one host to another must cross the
underlying network.

• A process sends and receives through a socket
– the “doorway” leading in/out of the application

© University of

Using sockets

© University of

• The “address” of a socket is the 5-tuple:
– Protocol
– source-IP
– source-port number
– destination-IP
– destination-port number.

• Socket interface
– originally provided in Berkeley UNIX
– later adopted by all popular operating systems
– simplifies porting applications to different OSes

• In UNIX, everything is like a file
– all input is like reading a file
– all output is like writing a file
– file is “addressed” by an integer file descriptor

• API implemented as system calls:
– examples include connect(), read(), write(), close()

Berkeley Sockets

© University of

Using sockets

© University of

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

Socket Primitives

© University of

Simplified (6-)State diagram for
Connection Management

© University of

State Simplified

Description

CLOSED Idle No connection is active or pending

LISTEN Pass. est. The server is waiting for an incoming call

SYN RCVD Pass. est. A connection request has arrived; wait for ACK

SYN SENT Act. est. The application has started to open a connection

ESTABLISHED Established The normal data transfer state

FIN WAIT 1 Act. disc. The application has said it is finished

FIN WAIT 2 Act. disc. The other side has agreed to release

TIME WAIT Act. disc. Wait for all packets to die off

CLOSING Act. disc. Both sides have tried to close simultaneously

CLOSE WAIT Pass. disc. The other side has initiated a release

LAST ACK Pass. disc. Wait for all packets to die off

Complete list of Socket States

© University of

Socket Finite State Machine

Bold before slash:
System call
e.g., connect

Non-bold before

Packet received

After slash:
Packet sent

© University of

#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;

• Create a socket
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));

Sockets in C

© University of

• 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 here if it is >= 0.

Create IPv6 socket

© University of

• Bind and listen
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,
&client_addr_size);
snprintf(sendBuff, sizeof(sendBuff), “Hello World!\n”);
n = write(connfd, sendBuff, strlen(sendBuff));
close(connfd);

• Wait until one of several files is ready to read / write
• select(), pselect(), poll()

Sockets in C – server

© University of

Put TCP state machine in LISTEN

// 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

– If the socket is blocking (see fcntl and O_NONBLOCK), it waits until there is data
• This loop reads the whole connection

– If non-blocking, this just reads data that has arrived
• More may come after a delay

Sockets in C – client

© University of

• 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

Multi-threaded Web Server

© University of

• High level outline of code for previous slide:
– Dispatcher thread
– Worker thread

Multi-threaded web server

© University of

© University of

• Sliding window is controlled by receiver
• Determines amount of data the receiver is able to accept

– Sender and receiver maintain buffers to send and receive data
independently of the application

– No guarantee that data is immediately sent or read from the
respective buffers

TCP Sliding Window

© University of

TCP Sliding Window

© University of

• When the window is 0 the sender should not send any data
– Can send URGENT 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

• 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

© University of

• Send window
– What data the sender is able to send – unacknowledged segments

and unsent data that will fit into the receive window
• Receive window

– Amount of data the receiver is willing to receive – window size in

• Other windows are maintained for congestion control

TCP Sliding Window

© University of

1 11 21 31 41 51 61 71 81 91 101 111 121 131 141

TCP Sliding Window

Send Window

Receive Window

Segment Size 10 bytes
SYN-SYN/ACK-ACK Completed
SYN:1, ACK:1, Window:50

© University of

TCP Sliding Window

1 11 21 31 41 51 61 71 81 91 101 111 121 131 141

© University of

TCP Sliding Window

1 11 21 31 41 51 61 71 81 91 101 111 121 131 141

© University of

TCP Sliding Window

1 11 21 31 41 51 61 71 81 91 101 111 121 131 141

© University of

TCP Sliding Window

1 11 21 31 41 51 61 71 81 91 101 111 121 131 141

10 bytes read by application

© University of

TCP Sliding Window

1 11 21 31 41 51 61 71 81 91 101 111 121 131 141

© University of

TCP Sliding Window

1 11 21 31 41 51 61 71 81 91 101 111 121 131 141

© University of

TCP Sliding Window

1 11 21 31 41 51 61 71 81 91 101 111 121 131 141

© University of

TCP Sliding Window

1 11 21 31 41 51 61 71 81 91 101 111 121 131 141

Next 10 bytes read by application

© University of

TCP Sliding Window – Window

1 11 21 31 41 51 61 71 81 91 101 111 121 131 141

WindowUpdate

© University of

TCP Sliding Window

1 11 21 31 41 51 61 71 81 91 101 111 121 131 141

© University of

TCP Sliding Window

1 11 21 31 41 51 61 71 81 91 101 111 121 131 141

Sender Maintains the following invariant:
LastByteSent – LastByteAcked <= ReceiveWindowAdvertised ACK’d Bytes in- Bytes receiver is ready to Bytes receiver is not ready to © University of • The slides are based on slides prepared by based on material developed previously by: , , , and . • 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 Acknowledgement © University of https://ebookcentral.proquest.com/lib/unimelb/detail.action?docID=6481879 Socket Programming and TCP flow control Using sockets Berkeley Sockets Using sockets (2) Socket Primitives Simplified (6-)State diagram for Connection Management Complete list of Socket States Socket Finite State Machine Sockets in C Create IPv6 socket Sockets in C - server Sockets in C - client Multi-threaded Web Server Multi-threaded web server TCP Sliding Window TCP Sliding Window (2) TCP Sliding Window (4) TCP Sliding Window (5) TCP Sliding Window (6) TCP Sliding Window (7) TCP Sliding Window (8) TCP Sliding Window (9) TCP Sliding Window (10) TCP Sliding Window (11) TCP Sliding Window (12) TCP Sliding Window (13) TCP Sliding Window (14) TCP Sliding Window – Window Update TCP Sliding Window (15) TCP Sliding Window (16) Acknowledgement 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com