TCP/IP Protocol and System Programming: Lecture 36, Study notes of Computer Science

A part of the cs 241 spring 2007 course notes focusing on tcp programming and tcp/ip protocol issues. It covers socket creation using the socket function, bind function, and connection setup for sock_stream. The document also discusses the listen function, accept function, and connect call for sending and receiving data. Additionally, it touches upon the internet protocol and tcp/ip protocol architecture.

Typology: Study notes

Pre 2010

Uploaded on 03/16/2009

koofers-user-4ei
koofers-user-4ei 🇺🇸

10 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 241 Spring 2007
System Programming
1
TCP Programming and TCP/IP
Protocol Issues
Lecture 36
Klara Nahrstedt
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download TCP/IP Protocol and System Programming: Lecture 36 and more Study notes Computer Science in PDF only on Docsity!

CS 241 Spring 2007System Programming

TCP Programming and TCP/IPProtocol Issues^ Lecture 36^ Klara Nahrstedt

CS241 Administrative † Read Stallings Chapter 13, R&R 18.1-18.3, 18.7, 18.8 † LMP3 (Part I) Due April 24 – extension until 9am Tuesday † LMP3 (Part II) Due April 30 † Last Regular Quiz will be on Friday, April 27 onNetworking † Homework 2 posted today – Due Wednesday, May 2,4pm † Monday, April 30^ „^

We will have LMP3 Quiz „ We will have guest lecture – speaker from Mathematica talkingabout challenges of Mathematica Development on Different OSPlatforms (UNIX, MAC, Linux, Windows). †^

Wednesday, May 2 – In Class Review Session for FinalExam

Socket Creation in C: socket int s = socket(domain, type, protocol);

s: socket descriptor, an integer (like a file-handle) domain: integer, communication domain^ e.g., AF_INET typically used (Internet domain) type: communication type^ SOCK_STREAM: reliable, 2-way, connection-based service^ SOCK_DGRAM: unreliable, connectionless,^ other values: need root permission, rarely used, or obsolete protocol: specifies protocol (see file /etc/protocols for a list ofoptions) - usually set to 0 (IP)

NOTE: socket call does not specify where data will be

coming from, nor where it will be going to – it justcreates the interface!

Socket Structure The format of the address struct sockaddr is

determined by the address family (domain). For AF_INET it is a struct sockaddr_in Socket structure is defined in netinet/in.h Socket structure has at least the followingmembers in network byte order:

sa_family_t sin_family; in_port_t sin_port; /* Host Port Number / struct in_addr sin_addr; / IP Address*/

Connection Setup

(SOCK_STREAM)

  • TCP

Recall: no connection setup for SOCK_DGRAM A connection occurs between two kinds of participants

passive: waits for an active participant to request connection active: initiates connection request to passive side

Once connection is established, passive and active

participants are “similar”^ both can send & receive data^ either can terminate the connection

Analogy?

telephone

Connection setup cont’d •^ Passive participant– step 1: listen (for

incoming requests)– step 3: accept (a request)– step 4: data transfer

-^ The acceptedconnection is on a newsocket•^ The old socketcontinues to listen forother active participants

  • Active participant
  • step 2: request &establish connection– step 4: data transferPassive Participant

l-sock a-sock-

a-sock-

socket Active 1

socket Active 2

Accept Function A^ blocking

call Extracts the first connection on the queue of pending connections creates a new socket with the same socket type protocol and addressfamily as the specified socket allocates a new file descriptor for that socket Returns communication file descriptor if successful or -1 otherwise. Server fills the second parameter with information about the client You fill in the size of the buffer used for the second parameter and on

return the third parameter contains the actual size needed tocontain this information. #include <sys/socket.h> int accept (int socket, struct sockaddr *restrict address,socklen_t *restrict address_len);

connect call /* Client side */ int status = connect(sock, &name, namelen);

status: 0 if successful connect, -1 otherwise sock: integer, socket to be used in connection name: struct sockaddr: address of passive participant namelen: integer, sizeof(name)

connect is

blocking

close When finished using a socket, the socket should

be closed: status = close(s);

status: 0 if successful, -1 if error s: the file descriptor (socket being closed)

Closing a socket

closes a connection (for SOCK_STREAM) frees up the port used by the socket

Typical TCP Server-Client

TCP/IP Protocol Architecture Reliable connection for transfer of data between

applications Connection – temporary logical association

between two entities in different systems TCP Header

Source Port

Destination Port

Sequence Number

Acknowledgement Number

HeaderLength

Reserved

Flags

Window

Checksum

Urgent Pointer

Options + Padding

32 bit length

IP and IPv6 Interconnectivity Protocol – Internet Protocol (IP) IP Header

Version

IHL

DS

ECN^

Total Length

Identification

Flags^

Fragment Offset

Time to Live

Protocol

Header Checksum

Source AddressDestination AddressOptions + Padding

Send Protocol Sender Application (e.g., App Y on Host A) sends

message to TCP over the port 3 TCP hands the message down to IP with

instructions to send it to host B, port 2 IP hands message the message down to MAC

layer (Network Access Protocol – e.g.,Ethernet) with instructions to send it to router J TCP sends smaller pieces – TCP segments IP sends IP Datagrams

Receive Protocol Router examines IP header and based on

destination address Router directs the IP datagram out across network

2 to B – router augments the datagram with anetwork access header Network access protocol on B removes the NAP

header and passes the message to higher layer(IP) IP removes the IP header and passes the

message to TCP TCP does processing and then removes the TCP

header and passes the message to application.