Slide 1OverviewSocket: End Point of CommunicationUsing Ports to Identify ServicesKnowing What Port Number To UseUNIX Socket APITypes of SocketsUsing Stream SocketsRecovering message boundariesDatagram SocketsCreating a Socket: socket()Sending and Receiving DataByte OrderingByte Ordering SolutionWhy Can’t Sockets Hide These Details?Clients vs. Server setupConnecting Client socket to ServerServer Preparing its SocketAccepting a New Client ConnectionPutting it All TogetherSame, But Emphasizing the ClientSlide 22Client Establishes connectionServer Binding Port to SocketServer waits for incoming connectionsSending Data StreamReceiving Data StreamDatagram SocketsHow to handle multiple connections?I/O Multiplexing: Select (1)I/O Multiplexing: Select (2)I/O Multiplexing: Select (3)Server Reusing Its PortCommon Mistakes + Hints1Socket ProgrammingEE 122: Intro to Communication NetworksVern PaxsonTAs: Lisa Fowler, Daniel Killebrew, Jorge OrtizMaterials with thanks to Sukun Kim, Artur Rivilis, Jennifer Rexford,Ion Stoica, and colleagues at Princeton and UC Berkeley2Overview•Socket Programming: how applications use the network–Sockets are a C-language programming interface between Layer 7 (applications) and Layer 4 (transport)–Interface is quite general and fairly abstract–Use of interface differs somewhat between clients & servers3Socket: End Point of Communication•Sending message from one process to another–Message must traverse the underlying network•Process sends and receives through a “socket”–In essence, the doorway leading in/out of the house•Socket as an Application Programming Interface–Supports the creation of network applicationssocket socketUser processUser processOperatingSystemOperatingSystem4Using Ports to Identify ServicesWeb server(port 80)Client hostServer host 128.2.194.242Echo server(port 7)Service request for128.2.194.242:80(i.e., the Web server)Web server(port 80)Echo server(port 7)Service request for128.2.194.242:7(i.e., the echo server)OSOSClientClient5Knowing What Port Number To Use•Popular applications have “well-known ports”–E.g., port 80 for Web and port 25 for e-mail–Well-known ports listed at http://www.iana.orgOr see /etc/services on Unix systems•Well-known vs. ephemeral ports–Server has a well-known port (e.g., port 80)By convention, between 0 and 1023; privileged–Client gets an unused “ephemeral” (i.e., temporary) portBy convention, between 1024 and 65535•Uniquely identifying the traffic between the hosts–The two IP addresses plus the two port numbersSometimes called the “four-tuple”–And: underlying transport protocol (e.g., TCP or UDP)With the above, called the “five-tuple”6UNIX Socket API•In UNIX, everything is like a file–All input is like reading a file–All output is like writing a file–File is represented by an integer file descriptor•System calls for sockets–Client: create, connect, write/ send, read, close–Server: create, bind, listen, accept, read/recv, write/send, close7Types of Sockets•Different types of sockets implement different service models–Stream: SOCK_STREAM –Datagram: SOCK_DGRAM •Stream socket (TCP)–Connection-oriented (includes establishment + termination)–Reliable (lossless) and in-order delivery guaranteed.–At-most-once delivery, no duplicates–E.g., SSH, HTTP (Web), SMTP (email)•Datagram socket (UDP)–Connectionless (just data-transfer of packets)–“Best-effort” delivery, possibly lower variance in delay–E.g., IP Telephony (Skype), simple request/reply protocols (hostname address lookups via DNS; time synchronization via NTP)8Using Stream Sockets•No need to packetize data•Data arrives in the form of a “byte stream”•Receiver needs to separate messages in streamUser application sends messages“Hi there!” and “Hope you are well” separatelyphysicaldata-linknetworktransportapplicationTCP may send the messages joined together, “Hi there!Hope you are well” or may send them separately, or might even split them like “Hi there!Ho” and “pe you are well”9Recovering message boundaries•Stream socket data separation:–Use records (data structures) to partition data stream–How do we implement variable length records?–What if field containing record size gets corrupted?Not possible! Why?•Structuring the byte stream so you can recover the original data boundaries is termed framingA B C 4fixed lengthrecordfixed lengthrecordvariable lengthrecordsize ofrecord10Datagram Sockets•User packetizes data before sending•Maximum size of 64 KB•Using previous example, “Hi there!” and “Hope you are well” definitely sent in separate packets at network layer–Message boundaries preserved–But note: your message had better fit within 64 KB or else you’ll have to layer your own boundary mechanism on top of the datagram delivery anyway11Creating a Socket: socket()•Operation to create a socket–int socket(int domain, int type, int protocol)–Returns a descriptor (or handle) for the socket–Originally designed to support any protocol suite•Domain: protocol family–Use PF_INET for the Internet•Type: semantics of the communication–SOCK_STREAM: reliable byte stream–SOCK_DGRAM: message-oriented service•Protocol: specific protocol–UNSPEC: unspecified. No need for us to specify, since PF_INET plus SOCK_STREAM already implies TCP, or SOCK_DGRAM implies UDP.12Sending and Receiving Data•Sending data–ssize_t write(int sockfd, void *buf, size_t len)–Arguments: socket descriptor, pointer to buffer of data to send, and length of the buffer–Returns the number of characters written, and -1 on error–send(): same as write() with extra flags parameter•Receiving data–ssize_t read(int sockfd, void *buf, size_t len)–Arguments: socket descriptor, pointer to buffer to place the data, size of the buffer–Returns the number of characters read (where 0 implies “end of file”), and -1 on error–recv(): same as read() with extra flags parameter•Closing the socket–int close(int sockfd)13Byte Ordering•We talk about two numeric presentations:–Big EndianArchitectures: Sun SPARC, PowerPC 970, IBM System/360The most significant byte stored in memory at the lowest address.Example: 4A3B2C1D hexadecimal will be stored as: This is network-byte order.–Little EndianArchitectures: Intel x86, AMD64The least significant byte stored in memory at the lowest address.Example:
View Full Document