11Socket ProgrammingEE 122: Intro to Communication NetworksFall 2006 (MW 4-5:30 in Donner 155)Sukun Kim / Vern PaxsonTAs: Dilip Antony Joseph and Sukun Kimhttp://inst.eecs.berkeley.edu/~ee122/Materials with thanks to Artur Rivilis, Jennifer Rexford, Ion Stoicaand colleagues at Princeton and UC Berkeley2Announcements• Project #1 out today, due Thu Sep 28.• Vern & Dilip away this week.– I will be covering Dilip’s Friday section + office hours (inmy office)• No Lecture Wed Sept. 133Overview• Socket Programming: how applications use thenetwork– Sockets are a C-language programming interfacebetween Layer 7 (applications) and Layer 4 (transport)– Interface is quite general and fairly abstract– Use of interface differs somewhat between clients &servers4Socket: 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 processOperatingSystemOperatingSystem5Identifying the Receiving Process• Sending process must identify the receiver– Address of the receiving end host– Plus identifier (port) that specifies the receiving process• Receiving host– Destination address uniquely identifies the host IP address is a 32-bit quantity• Receiving process– Host may be running many different processes– Destination port uniquely identifies the socket Port number is a 16-bit quantity6Using 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)OSOSClientClient27Knowing 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.org Or see /etc/services on Unix system• 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) port By convention, between 1024 and 65535• Uniquely identifying the traffic between the hosts– The two IP addresses plus the two port numbers Sometimes called the “four-tuple”– And: underlying transport protocol (e.g., TCP or UDP) With the above, called the “five-tuple”8Delivering the Data: Division of Labor• Network–Deliver data packet to the destination host–Based on the destination IP address• Operating system–Deliver data to the destination socket–Based on the protocol and destination port #• Application–Read data from the socket–Interpret the data (e.g., render a Web page)9UNIX Socket API• Socket interface– Originally provided in Berkeley UNIX– Later adopted by all popular operating systems– Simplifies porting applications to different OSes• 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, close10Types 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 viaNTP)11Using 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 arewell” separatelyphysicaldata-linknetworktransportapplicationTCP may send themessages joinedtogether, “Hithere!Hope youare well” or maysend themseparately, ormight even splitthem like “Hithere!Ho” and “peyou are well”12Recovering 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?A B C 4fixed lengthrecordfixed lengthrecordvariable lengthrecordsize ofrecord313Datagram Sockets• User packetizes data before sending• Maximum size of 64 KB• Using previous example, “Hi there!” and “Hopeyou are well” definitely sent in separatepackets at network layer– Message boundaries preserved– But note: your message had better fit within 64 KB orelse you’ll have to layer your own boundary mechanismon top of the datagram delivery anyway14Typical Client Program• Prepare to communicate–Create a socket–Determine server address and port number–Initiate the connection to the server• Exchange data with the server–Write data to the socket–Read data from the socket Note, single socket supports both reading and writing–Do stuff with the data (e.g., render a Web page)• Close the socket15Creating 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, sincePF_INET plus SOCK_STREAM already implies TCP, orSOCK_DGRAM implies UDP.16Connecting the Socket to the Server• Translate the server’s name to an address– struct hostent *gethostbyname(char *name)– name: the name of the host (e.g., “www.cnn.com”)– Returns a structure that includes the host address Or NULL if host doesn’t exist• Identifying the service’s port number– struct servent *getservbyname(char *name, char *proto)–
View Full Document