TCP Segment Format by cometjunkie46

VIEWS: 1,610 PAGES: 6

									TCP
CSCE 515:
Computer Network Programming ------ TCP Programming
Wenyuan Xu

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

Department of Computer Science and Engineering University of South Carolina

TCP Segment Format
0 15 16 31

TCP Connection Establishment
– Three-way handshake
Client
“I want to talk, and I’m starting with byte number X+1”.

source port number

destination port number

Server
SYN SYN ISN=X ISN=X

sequence number acknowledgment number
header length reserved
U A P R S F R C S S Y I G K H T N N

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”

20 bytes

window size urgent pointer

TCP checksum

SYN SYN 2 ISN=Y ACK=X+1 ISN=Y ACK=X+1 ACK=Y+1 ACK=Y+1

time

option (if any) data (if any)

“Got it - you start at byte number Y+1”.

3

9/15/2008

CSCE515 – Computer Network Programming

9/15/2008

CSCE515 – Computer Network Programming

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

N SEQ Send

Number of bytes in packet (N)

Sequence number of first data byte in packet (SEQ)

ACK WIN Recv

ACK bit set

Sequence number of next expected byte (ACK)

Window size at the receiver (WIN)

Contained in IP header
9/15/2008 CSCE515 – Computer Network Programming 9/15/2008

Contained in TCP header

CSCE515 – Computer Network Programming

TCP Flow Control
Sender
Application does a 2K write
2K SEQ=0

FIN
receiver recv’s buffer 4K 0 empty 2K

ACK = 2048 WIN

= 2048

Application does a 3K write
2K SEQ=2048

Sender is blocked

Full
ACK = 4096 WIN ACK = 4096 WIN =0

Application reads 2k

= 2048

2K

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.

Sender may send up to 2k

1k

SEQ=4096

1K

2K

9/15/2008

CSCE515 – Computer Network Programming

9/15/2008

CSCE515 – Computer Network Programming

TCP Termination
App1
“I have no more data for you” FIN_WAIT_1

TCP Sockets Programming
App2
1
“OK, I understand you are done sending.”
CLOSE_WAIT

FIN FIN SN=X SN=X

ACK=X+1 ACK=X+1
FIN_WAIT_2

2

...

Creating a passive mode (server) socket. Establishing an application-level connection. send/receive data. Terminating a connection.

FIN FIN SN=Y SN=Y
“Over and Out, Goodbye” TIME_WAIT

3

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

ACK=Y+1 ACK=Y+1

4
CLOSED

9/15/2008

CSCE515 – Computer Network Programming

9/15/2008

CSCE515 – Computer Network Programming

Client-Server Communication (TCP)
int socket(int family, int type, int protocol); int bind(int sockfd, struct sockaddr *my_addr, int addrlen);

Creating a TCP socket
int socket(int family,int type,int proto);

socket() bind() listen()

TCP Server
well-known port

TCP Client
socket()
int listen(int sockfd, int backlog); int socket(int family, int type, int protocol);

accept() int accept(int sockfd, void *addr, int *addrlen); int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); connection establishment
data(request)

connect() write()

blocks until connection from client read() process request

read() close()

data(reply)

write()

end-of-file no tification
int close(int sockfd); int close(int sockfd);

int sock; sock = socket( AF_INET, SOCK_STREAM, 0); if (sock<0) { /* ERROR */ }
9/15/2008 CSCE515 – Computer Network Programming

read() close()

9/15/2008

CSCE515 – Computer Network Programming

Client Code
TCP clients can call connect() which:
takes care of establishing an endpoint address for the client socket.
don’t need to call bind first, the O.S. will take care of assigning the local endpoint address (TCP port number, IP address).

Connect
int mysock; struct sockaddr_in servaddr; sockfd = socket(AF_INET,SOCK_STREAM,0); servaddr.sin_family = AF_INET; servaddr.sin_port = htons( SERVER_PORT ); servaddr.sin_addr = htonl(SEVER_IP ); if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { perror("connect error"); exit(1); }
#include <sys/socket.h> int connect( int sockfd, const struct sockaddr * servaddr, socklen_t addrlen); sockfd is an already created TCP socket. servaddr contains the address of the server (IP Address and TCP port number) connect( ) returns 0 if OK, -1 on error

Attempts to establish a connection to the specified server.
3-way handshake

9/15/2008

CSCE515 – Computer Network Programming

9/15/2008

CSCE515 – Computer Network Programming

Reading from a TCP socket
int read( int fd, char *buf, int max);

Writing to a TCP socket
int write( int fd, char *buf, int num);

By default read() will block until data is available. reading from a TCP socket may return less than max bytes (whatever is available). You must be prepared to read data 1 byte at a time!
9/15/2008 CSCE515 – Computer Network Programming

write might not be able to write all num bytes (on a nonblocking socket). The book includes readn(), writen() and readline() function definitions.

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.

Server code: establishing a passive mode TCP socket
Passive mode:
Address already determined. Tell the kernel to accept incoming connection requests directed at the socket address.
3-way handshake

#include <unistd.h> int close( int sockfd); close( ) returns 0 if OK, -1 on error

Tell the kernel to queue incoming connections for us.
9/15/2008 CSCE515 – Computer Network Programming

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

listen()
Sum of both queues cannot exceed backlog

Server

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 9/15/2008

accept

TCP

3-way handshake complete

Completed connection queue ESTABLISHED state

Incomplete connection queue SYN_RCVD state

arriving SYN

CSCE515 – Computer Network Programming

Accepting an incoming connection.
Once we call listen(), the O.S. will queue incoming connections
Handles the 3-way handshake Queues up multiple connections.

accept()
int accept(int sockfd, struct sockaddr* cliaddr, socklen_t *addrlen);

When our application is ready to handle a new connection, we need to ask the O.S. for the next connection.

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

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

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.

differently on sockets (vs. file operation)!
9/15/2008 CSCE515 – Computer Network Programming 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

Client/Server
before call to accept return
129.1.1.200:1500

Client (129.1.1.200)
connection request

65.1.1.200

server

connect()

listenfd *:80

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);
9/15/2008 CSCE515 – Computer Network Programming

9/15/2008

CSCE515 – Computer Network Programming

Client/Server
After call to accept return
{129.1.1.200:1500, 65.1.1.200:80}

Client/Server
After call to accept return
server
65.1.1.200 {129.1.1.200:1500, 65.1.1.200:80}

Client (129.1.1.200)
connection request

Client (129.1.1.200)
connection request

Server (parent)
listenfd *:80 connfd

65.1.1.200

connect()

listenfd *:80 connfd

connect()

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);
9/15/2008 CSCE515 – Computer Network Programming

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);
9/15/2008 CSCE515 – Computer Network Programming

fork

Server (child)
listenfd *:80 connfd

Client/Server
After call to accept return
{129.1.1.200:1500, 65.1.1.200:80}

Assignment & Next time
Reading:
Server (parent)
listenfd *:80 65.1.1.200

Client (129.1.1.200)

UNP 3.9, 4.3-4.9 **

connect()

Next Lecture:
RLOGIN TELNET FTP Protocol

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);
9/15/2008

Server (child)
connection

listenfd *:80 connfd

CSCE515 – Computer Network Programming

9/15/2008

CSCE515 – Computer Network Programming


								
To top