TCP Programming in CSCE 515: Segment Format, Connection Creation, and Flow Control - Prof., Study notes of Computer Science

An overview of tcp programming as taught in the csce 515 course at the university of south carolina. Topics covered include tcp segment format, connection creation through the three-way handshake, and flow control. Students will learn about the structure of tcp segments, the role of sequence and acknowledgment numbers, and the importance of window sizes in managing data transfer.

Typology: Study notes

Pre 2010

Uploaded on 10/01/2009

koofers-user-9p5wt
koofers-user-9p5wt 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCE 515:
Computer Network Programming
------ TCP Programming
Wenyuan Xu
Department of Computer Science and Engineering
University of South Carolina
CSCE515 – Computer Network Programming9/15/2008
TCP
TCP provides the end-to-end reliable
connection that IP alone cannot support
The TCP protocol
Segment format
Connection Creation
Flow control
Congestion control
Connection termination
CSCE515 – Computer Network Programming9/15/2008
TCP Segment Format
0151631
20 bytes
destination port number
urgent pointerTCP checksum
option (if any)
source port number
window size
sequence number
acknowledgment number
header
length reserved U
R
G
A
C
K
P
S
H
R
S
T
S
Y
N
F
I
N
data (if any)
CSCE515 – Computer Network Programming9/15/2008
Client Server
SYN
ISN=X
SYN
ISN=X1
SYN
ISN=YACK=X+1
SYN
ISN=YACK=X+1
2
ACK=Y+1
ACK=Y+1 3
time
TCP Connection Establishment
Three-way handshake
“I want to talk, and
I’m starting with
byte number X+1”.
“OK, I’m here and I’ll
talk. My first byte will
be called number Y+1,
and I know your first
byte will be number X+1”
“Got it - you start
at byte number Y+1”.
CSCE515 – Computer Network Programming9/15/2008
TCP Data and ACK
Once the connection is established, data
can be sent.
Each data segment includes a sequence
number identifying the first byte in the
segment.
Each ACK segment includes a request
number indicating what data has been
received. (bytes instead of packets)
CSCE515 – Computer Network Programming9/15/2008
Important Information in TCP/IP packet headers
Send
NSEQ
Recv
ACK WIN
Number of
bytes in
packet (N)
ACK bit set
Sequence
number of
next expected
byte (ACK)
Sequence
number of
first data byte
in packet
(SEQ)
Window size
at the receiver
(WIN)
Contained in IP header Contained in TCP header
pf3
pf4
pf5

Partial preview of the text

Download TCP Programming in CSCE 515: Segment Format, Connection Creation, and Flow Control - Prof. and more Study notes Computer Science in PDF only on Docsity!

CSCE 515:

Computer Network Programming

------ TCP Programming

Wenyuan Xu

Department of Computer Science and Engineering University of South Carolina

9/15/2008 CSCE515 – Computer Network Programming

TCP

„ TCP provides the end-to-end reliable

connection that IP alone cannot support

„ The TCP protocol

Segment format

Connection Creation

Flow control

Congestion control

Connection termination

9/15/2008 CSCE515 – Computer Network Programming

TCP Segment Format

0 15 16 31

20 bytes

destination port number

TCP checksum urgent pointer

option (if any)

source port number

window size

sequence number

acknowledgment number header length reserved

UR G

AC K

PS H

RS T

SY N

FI N

data (if any)

9/15/2008 CSCE515 – Computer Network Programming

Client Server

SYN

ISN= X

SYN

ISN= X

SYN

ISN= Y ACK= X +

SYN

ISN= Y ACK= X +

ACK= ACK= YY +1+

time

TCP Connection Establishment

– Three-way handshake

“I want to talk, and I’m starting with byte number X+1 ”.

“OK, I’m here and I’ll talk. My first byte will be called number Y+1, and I know your first byte will be number X+1” “Got it - you start at byte number Y+1”.

TCP Data and ACK

„ Once the connection is established, data

can be sent.

„ Each data segment includes a sequence

number identifying the first byte in the

segment.

„ Each ACK segment includes a request

number indicating what data has been

received. (bytes instead of packets)

Important Information in TCP/IP packet headers

Send

N SEQ

Recv

ACK WIN

Number of bytes in packet (N)

ACK bit set

Sequence number of next expected byte (ACK)

Sequence number of first data byte in packet (SEQ)

Window size at the receiver (WIN)

Contained in IP header Contained in TCP header

9/15/2008 CSCE515 – Computer Network Programming

TCP Flow Control

Sender

Application does a 2K write

Application reads 2k

Sender is blocked

2K SEQ=0^ empty

receiver 0 4K

recv’s buffer

2K

ACK = 2048 WIN = 2048 Application does a 3K write 2K SEQ= Full ACK = 4096 WIN = 0

ACK = 4096 WIN = 2048

1k (^) SEQ=

Sender may send up to 2k

2K

1K 2K

9/15/2008 CSCE515 – Computer Network Programming

FIN

„ Either end of the connection can initiate

termination.

„ A FIN is sent, which means the application

is done sending data.

„ The FIN is ACK’d.

„ The other end must now send a FIN.

„ That FIN must be ACK’d.

9/15/2008 CSCE515 – Computer Network Programming

App1 App

FIN

SN= X

FIN

SN= X

ACK= ACK= XX +1+

ACK= ACK= YY +1+

FIN

SN= Y

FIN

SN= Y

“I have no more data for you” FIN_WAIT_

“ OK, I understand you are done sending .” CLOSE_WAIT

“OK - Now I’m also done sending data”. LAST_ACK

“Over and Out, Goodbye” TIME_WAIT

TCP Termination

FIN_WAIT_

CLOSED

9/15/2008 CSCE515 – Computer Network Programming

TCP Sockets Programming

„ Creating a passive mode (server) socket.

„ Establishing an application-level

connection.

„ send/receive data.

„ Terminating a connection.

Client-Server Communication (TCP)

socket()

bind()

listen()

accept()

read()

write()

read()

close()

socket()

connect()

write()

read()

close()

TCP Client

TCP Server

well-known port

blocks until connection from client

process request

connection establishment data(request)

data(reply)

end-of-file notification

int socket(int family, int type, int protocol);

int bind(int sockfd, struct sockaddr *my_addr, int addrlen);

int listen(int sockfd, int backlog); int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);int accept(int sockfd, void *addr, int *addrlen);

int close(int sockfd); int close(int sockfd);

int socket(int family, int type, int protocol);

Creating a TCP socket

int socket(int family,int type,int proto);

int sock;

sock = socket( AF_INET,

SOCK_STREAM,

if (sock<0) { /* ERROR */ }

9/15/2008 CSCE515 – Computer Network Programming

Binding to well known address

int mysock;

struct sockaddr_in myaddr;

mysock = socket(AF_INET,SOCK_STREAM,0);

myaddr.sin_family = AF_INET;

myaddr.sin_port = htons( 80 );

myaddr.sin_addr = htonl( INADDR_ANY );

bind(mysock, (struct sockaddr *) &myaddr,

sizeof(myaddr));

#include <sys/socket.h> int bind( int sockfd, const struct sockaddr * myaddr, socklen_t addrlen); bind( ) return 0 if OK, -1 on error 9/15/2008 CSCE515 – Computer Network Programming

getsockname()

struct socketaddr_in ss; socklen_t len;

*myaddr.sin_port = htons( 0 ); myaddr.sin_addr = htonl( INADDR_ANY ); bind(mysock, (struct sockaddr ) &myaddr, sizeof(myaddr));

getsockname(mysock, &ss, &len)

#include <sys/socket.h> int getsockname(int sockfd, struct sockaddr * localaddr, socklen_t * addrlen); bind( ) return 0 if OK, -1 on error

9/15/2008 CSCE515 – Computer Network Programming

listen()

int listen( int sockfd, int backlog);

sockfd is the TCP socket (already bound to an

address)

backlog is the number of incoming

connections the kernel should be able to

keep track of (queue for us).

listen() returns -1 on error (otherwise 0).

9/15/2008 CSCE515 – Computer Network Programming

listen()

Server

TCP

3-way handshakecomplete

accept

arrivingSYN

Completed connection queueESTABLISHED state

Incomplete connection queueSYN_RCVD state

Sum of both queuescannot exceed backlog

Accepting an incoming connection.

„ Once we call listen(), the O.S. will

queue incoming connections

Handles the 3-way handshake

Queues up multiple connections.

„ When our application is ready to handle a

new connection, we need to ask the O.S.

for the next connection.

accept()

int accept(int sockfd,

struct sockaddr* cliaddr,

socklen_t *addrlen);

sockfd is the passive mode TCP socket.

cliaddr is a pointer to allocated space.

addrlen is a value-result argument

must be set to the size of cliaddr

on return, will be set to be the number of

used bytes in cliaddr.

9/15/2008 CSCE515 – Computer Network Programming

accept() return value

accept() returns a new socket descriptor

(small positive integer) or -1 on error.

After accept returns a new socket descriptor,

I/O can be done using the read() and

write() system calls.

read() and write() operate a little

differently on sockets (vs. file operation)!

9/15/2008 CSCE515 – Computer Network Programming

Terminating a TCP connection

„ Either end of the connection can call the

close() system call.

„ If the other end has closed the connection,

and there is no buffered data, reading from

a TCP socket returns 0 to indicate EOF.

9/15/2008 CSCE515 – Computer Network Programming

fork()

„ In Unix the way to create a new process is the

fork() system call.

„ fork() is called once but it returns twice

„ Return value:

0: return in the child

Non-0: the PID of the newly created process

9/15/2008 CSCE515 – Computer Network Programming

Client/Server

„ before call to accept return

Client (129.1.1.200) server

connect()

connection request listenfd

129.1.1.200:1500 (^) 65.1.1.

*:

listenfd=socket(…) bind(listenfd…) listen(listenfd,LISTENQ); For( ; ;) { connfd = accept(listenfd, …); if ( (pid = fork())==0) { close(listendf); doit(connfd); close(connfd); exit(0); } close(connfd);

Client/Server

„ After call to accept return

Client (129.1.1.200) server

connect()

listenfd connection request

{129.1.1.200:1500, 65.1.1.200:80} (^) 65.1.1.

*: connfd

listenfd=socket(…) bind(listenfd…) listen(listenfd,LISTENQ); For( ; ;) { connfd = accept(listenfd, …); if ( (pid = fork())==0) { close(listendf); doit(connfd); close(connfd); exit(0); } close(connfd);

Client/Server

„ After call to accept return

Client (129.1.1.200) Server (parent)

connect()

listenfd connection request

{129.1.1.200:1500, 65.1.1.200:80} (^) 65.1.1.

*: connfd

listenfd=socket(…) bind(listenfd…) listen(listenfd,LISTENQ); For( ; ;) { connfd = accept(listenfd, …); if ( (pid = fork())==0) { close(listendf); doit(connfd); close(connfd); exit(0); } close(connfd);

*listenfd : connfd

Server (child)

fork