csce 515
play

CSCE 515: Computer Network Transport Layer TCP UDP Programming - PDF document

Process Layer Process Process CSCE 515: Computer Network Transport Layer TCP UDP Programming ------ Sockets ICMP, ARP Network Layer IP & Wenyuan Xu RARP Department of Computer Science and Engineering University of South Carolina


  1. Process Layer Process Process CSCE 515: Computer Network Transport Layer TCP UDP Programming ------ Sockets ICMP, ARP Network Layer IP & Wenyuan Xu RARP Department of Computer Science and Engineering University of South Carolina Data-Link Layer 802.3 9/8/2008 CSCE515 – Computer Network Programming Network API Network API � API - Application Programming Interface � API is a set of functionality/services delivered by a I nternet Application OSI model programming system. protocol suite details User Application processor Presentation Application � Network API Session � The services ( often provided by the operating system) Transport TCP UDP that provide the interface between application and Network IPv4, IPv6 protocol software. kernel Data link Data link Physical Physical Communications details 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming Network API wish list TCP/IP � Generic Programming Interface. � TCP/IP does not include an API definition. � Support multiple communication protocol suites (families). � There are a variety of APIs for use with � Address (endpoint) representation independence. TCP/IP: � Provide special services for Client and Server? � Sockets by Berkeley � Support for message oriented and connection � XTI (X/Open Transport Interface) by AT&T oriented communication. � Winsock - Windows Sockets API by Microsoft � Work with existing I/O services (when this � MacTCP / Open Transport by Apple makes sense). � Operating System independence 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming

  2. Client-Server Model Functions needed: � Specify local and remote communication Client 1 endpoints Server Client 2 � Initiate a connection Client 3 � Wait for incoming connection � Send and receive data � One side of communication is client, and the other side is server � Terminate a connection gracefully � Server waits for a client request to arrive � Error handling � Server processes the client request and sends the response back to the client � Iterative or concurrent 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming Berkeley Sockets Elements of a Socket � A socket is an abstract representation of a � Each socket can be uniquely identified by communication endpoint. � Source IP address � Generic: � Source port number � support for multiple protocol families. � Destination IP address � address representation independence � Destination port number � Sockets (obviously) have special needs: � An end-to-end protocol (TCP or UDP) � establishing a connection � specifying communication endpoint addresses � Sockets work with Unix I/O services just like files, pipes & FIFOs 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming Types of Sockets Stream Sockets � Two different types of sockets � Also known as connection-oriented socket � Use TCP � Stream sockets � Provide reliable, connected networking � Datagram sockets service � Error free; no out-of-order packets � Applications: telnet, ssh, http 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming

  3. Datagram Sockets Unix Descriptor Table � Also known as connectionless socket Descriptor Table Descriptor Table � Use UDP Data structure for file 0 Data structure for file 0 0 � Provide unreliable, best-effort networking service 1 Data structure for file 1 Data structure for file 1 � Packets may be lost; may arrive out of 2 order 3 Data structure for file 2 Data structure for file 2 � Applications: streaming audio/ video 4 int fd; int cc, nbytes; char *buf; fd = open (my_filename, O_RDONLY ); cc = write (fd, buf, nbytes); cc = read (fd, buf, nbytes); 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming Socket Descriptor Data Structure Client-Server Model � S erver Descriptor Table Descriptor Table � Create a socket with the socket() system call Family: PF_INET Family: PF_INET Family: PF_INET � Bind the socket to an address using the bind() system 0 Service: SOCK_STREAM Service: SOCK_STREAM Service: SOCK_STREAM call. For a server socket on the Internet, an address 1 Local IP: 111.22.3.4 Local IP: 111.22.3.4 Local IP: 111.22.3.4 consists of a port number on the host machine. Remote IP: 123.45.6.78 Remote IP: 123.45.6.78 Remote IP: 123.45.6.78 2 � Listen for connections with the listen() system call Local Port: 2249 Local Port: 2249 Local Port: 2249 � Accept a connection with the accept() system call. 3 Remote Port: 3726 Remote Port: 3726 Remote Port: 3726 This call typically blocks until a client connects with 4 the server. � Send and receive data int s, family, type, protocol; s = socket(family, type, protocol) ; etc... cc = read(s, buf, nbytes); 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming Client-Server Model Creating a Socket � Client int socket(int family,int type,int proto); � Create a socket with the socket() system call � family specifies the protocol family � Connect the socket to the address of the � AF_INET: IPv4 protocols server using the connect() system call � AF_INET6: IPv6 protocols � AF_ROUTE: Routing sockets � Send and receive data. There are a number � type specifies the type of service � SOCK_STREAM of ways to do this, but the simplest is to use � SOCK_DGRAM � SOCK_RAW the read() and write() system calls. � protocol specifies the specific protocol (usually 0, which means the default ). � IPPROTO_TCP: TCP transport protocol � IPPROTO_UDP: UDP transport protocol 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming

  4. Specifying an Endpoint Address socket() � The socket() system call returns a � Remember that the sockets API is generic socket descriptor (small integer) or -1 on � There must be a generic way to specify error. endpoint addresses. � socket() allocates resources needed for � TCP/IP requires an IP address and a port a communication endpoint - but it does not number for each endpoint address. deal with endpoint addressing. 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming Necessary Background Information: bind() POSIX data types � calling bind() assigns the address specified by the sockaddr structure to the signed 8bit int int8_t socket descriptor. unsigned 8 bit int uint8_t signed 16 bit int int16_t bind( mysock, unsigned 16 bit int uint16_t (struct sockaddr*) &myaddr, signed 32 bit int int32_t sizeof(myaddr) ); unsigned 32 bit int uint32_t 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming More POSIX data types Generic socket addresses Used by kernel address family sa_family_t struct sockaddr { uint8_t sa_len; length of struct socklen_t sa_family_t sa_family; IPv4 address in_addr_t char sa_data[14]; IP port number in_port_t }; � sa_family specifies the address type. � sa_data specifies the address value. 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming

  5. sockaddr AF_CSCE515 � Initializing a sockaddr structure to point � An address that will allow me to use sockets to Henry : to communicate with you. � address type AF_CSCE515 struct sockaddr henry; � address values: Dean 1 Sayan 6 henry.sa_family = AF_CSCE515; Devon 2 Yuliya 7 henry.sa_data[0] = 5; Samuel 3 Razvan 8 Shamik 4 Mythri 9 Henry 5 Femitolu 10 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming AF_INET struct sockaddr_in (IPv4) � For AF_CSCE515 we only needed 1 byte struct sockaddr_in { to specify the address. uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; � For AF_INET we need: IPv4 only! struct in_addr sin_addr; � 16 bit port number char sin_zero[8]; � 32 bit IP address }; A special kind of sockaddr structure 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming Byte Ordering struct in_addr struct in_addr { � Different computer architectures use in_addr_t s_addr; different byte ordering to represent }; multibyte values. � 16 bit integer: in_addr just provides a name for the ‘C’ type associated with IP addresses. Address A Address A Low Byte High Byte High Byte Address A+1 Address A+1 Low Byte 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming

Recommend


More recommend