Socket() System Call Family: PF INET 0 Family: PF INET Bind the Socket to an Address Using the Bind() System Service:Service: SOCK STREAMSOCK STREAM Call

Socket() System Call Family: PF INET 0 Family: PF INET Bind the Socket to an Address Using the Bind() System Service:Service: SOCK STREAMSOCK STREAM Call

Process Process Process Layer CSCE 515: Computer Network TCP UDP Transport Layer Programming ------ Sockets ICMP, ARP & IP Network Layer Wenyuan Xu RARP Department of Computer Science and Engineering University of South Carolina 802.3 Data-Link Layer 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 Internet OSI model Application programming system. protocol suite details Application User 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. Data link Data link kernel 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). Address (endpoint) representation independence. There are a variety of APIs for use with Provide special services for Client and Server? TCP/IP: 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 Client-Server Model Functions needed: Client 1 Specify local and remote communication Server Client 2 endpoints Initiate a connection Client 3 Wait for incoming connection One side of communication is client, and the other side Send and receive data is server Terminate a connection gracefully Server waits for a client request to arrive Server processes the client request and sends the Error handling 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. address representation independence Destination IP address 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 Stream sockets Use TCP Datagram sockets Provide reliable, connected networking 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 Datagram Sockets Unix Descriptor Table Also known as connectionless socket Descriptor Table Use UDP Data structure for file 0 Provide unreliable, best-effort networking 0 service 1 Data structure for file 1 Packets may be lost; may arrive out of 2 order 3 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 Server Descriptor Table Create a socket with the socket() system call Family: PF_INET 0 Family: PF_INET Bind the socket to an address using the bind() system Service:Service: SOCK_STREAMSOCK_STREAM call. For a server socket on the Internet, an address 1 LocalLocal IP:IP: 111.22.3.4111.22.3.4 consists of a port number on the host machine. RemoteRemote IP:IP: 123.45.6.78123.45.6.78 2 Listen for connections with the listen() system call LocalLocal Port:Port: 22492249 3 RemoteRemote Port:Port: 37263726 Accept a connection with the accept() system call. This call typically blocks until a client connects with 4 the server. int s, family, type, protocol; Send and receive data 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 specifies the protocol family Connect the socket to the address of the family 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 the read() and write() system calls. SOCK_RAW 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 socket() Specifying an Endpoint Address The socket() system call returns a Remember that the sockets API is generic socket descriptor (small integer) or -1 on error. There must be a generic way to specify 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 int8_t signed 8bit int socket descriptor. uint8_t unsigned 8 bit int int16_t signed 16 bit int bind( mysock, (struct sockaddr*) &myaddr, uint16_t unsigned 16 bit int sizeof(myaddr) ); int32_t signed 32 bit int uint32_t unsigned 32 bit int 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming More POSIX data types Generic socket addresses el rn ke y sa_family_t address family struct sockaddr { b ed s socklen_t length of struct uint8_t sa_len; U in_addr_t IPv4 address sa_family_t sa_family; char sa_data[14]; in_port_t IP port number }; 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 sockaddr AF_CSCE515 An address that will allow me to use sockets Initializing a sockaddr structure to point to communicate with you. to Henry : address type AF_CSCE515 address values: struct sockaddr henry; Dean 1 Sayan 6 Devon 2 Yuliya 7 henry.sa_family = AF_CSCE515; Samuel 3 Razvan 8 henry.sa_data[0] = 5; 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: ly! on struct in_addr sin_addr; 16 bit port number v4 IP 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 struct in_addr Byte Ordering 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. Low Byte Address A High Byte High Byte Address A+1 Low Byte 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming Byte Ordering Byte Order and Networking Little-Endian Big-Endian Suppose a Big Endian machine sends a Low Byte High Byte High Byte Low Byte 16 bit integer with the value 2: Addr A Addr A+1 Addr A Addr A+1 0000000000000010 IBM 80x86 IBM 370 A Little Endian machine will think it got DEC VAX Motorola 68000 the number 512: DEC PDP-11 Sun 0000001000000000 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming Network Byte Order Network Byte Order All values stored in a sockaddr_in must Conversion of application-level data is be in network byte order. left up to the presentation layer. sin_port a TCP/IP port number. But hold on !!! How do lower level layers sin_addr an IP address. communicate if they all represent values differently ? (data length fields in headers) A fixed byte order is used (called network byte order) for all control data. 9/8/2008 CSCE515 – Computer Network Programming 9/8/2008 CSCE515 – Computer Network Programming Network Byte Order Functions TCP/IP Addresses We don’t need to deal with sockaddr ‘h’ : host byte order ‘n’ : network byte order structures since we will only deal with a real protocol family. ‘s’ : short (16bit) ‘l’ : long (32bit) We can use structures. uint16_t htons(uint16_t); sockaddr_in uint16_t ntohs(uint_16_t); BUT: The C functions that make up the sockets uint32_t htonl(uint32_t); API expect structures of type sockaddr. uint32_t ntohl(uint32_t); int bind(int sockfd, struct sockaddr *my_addr, int addrlen); int connect(int sockfd,

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us