



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Wenyuan Xu
Department of Computer Science and Engineering University of South Carolina
9/15/2008 CSCE515 – Computer Network Programming
Segment format
Connection Creation
Flow control
Congestion control
Connection termination
9/15/2008 CSCE515 – Computer Network Programming
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
“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”.
Send
Recv
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
Sender
Application does a 2K write
Application reads 2k
Sender is blocked
2K SEQ=0^ empty
receiver 0 4K
recv’s buffer
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
9/15/2008 CSCE515 – Computer Network Programming
9/15/2008 CSCE515 – Computer Network Programming
“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
FIN_WAIT_
CLOSED
9/15/2008 CSCE515 – Computer Network Programming
socket()
bind()
listen()
accept()
read()
write()
read()
close()
socket()
connect()
write()
read()
close()
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);
9/15/2008 CSCE515 – Computer Network Programming
#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
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
9/15/2008 CSCE515 – Computer Network Programming
Server
3-way handshakecomplete
accept
arrivingSYN
Completed connection queueESTABLISHED state
Incomplete connection queueSYN_RCVD state
Sum of both queuescannot exceed backlog
Handles the 3-way handshake
Queues up multiple connections.
must be set to the size of cliaddr
on return, will be set to be the number of
9/15/2008 CSCE515 – Computer Network Programming
9/15/2008 CSCE515 – Computer Network Programming
9/15/2008 CSCE515 – Computer Network Programming
0: return in the child
Non-0: the PID of the newly created process
9/15/2008 CSCE515 – Computer Network Programming
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 (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 (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