DOC PREVIEW
Berkeley ELENG 122 - Socket Programming

This preview shows page 1-2-16-17-18-33-34 out of 34 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Socket Programming EE 122 Intro to Communication Networks Vern Paxson TAs Lisa Fowler Daniel Killebrew Jorge Ortiz Materials with thanks to Sukun Kim Artur Rivilis Jennifer Rexford Ion Stoica and colleagues at Princeton and UC Berkeley 1 Overview 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 servers 2 Socket 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 applications User process User process socket socket Operating System Operating System 3 Using Ports to Identify Services Client host Client Service request for 128 2 194 242 80 i e the Web server Server host 128 2 194 242 Web server port 80 OS Echo server port 7 Client Service request for 128 2 194 242 7 i e the echo server Web server port 80 OS Echo server port 7 4 Knowing 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 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 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 5 UNIX 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 close 6 Types 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 7 Using Stream Sockets No need to packetize data Data arrives in the form of a byte stream Receiver needs to separate messages in stream TCP 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 application transport network data link physical User application sends messages Hi there and Hope you are well separately 8 Recovering message boundaries Stream socket data separation Use records data structures to partition data stream How do we implement variable length records size of record A fixed length record B C fixed length record 4 variable length record 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 framing 9 Datagram 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 anyway 10 Creating 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 11 Sending 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 12 Byte Ordering We talk about two numeric presentations Big Endian Architectures Sun SPARC PowerPC 970 IBM System 360 The most significant byte stored in memory at the lowest address Example 4A3B2C1D hexadecimal will be stored as Lowest addres s here 4A 3B 2C 1D This is network byte order Little Endian Architectures Intel x86 AMD64 The least significant byte stored in memory at the lowest address Example 4A3B2C1D hexadecimal will be stored as 1D 2C 3B 4A What problems can arise because of this What can we do to solve them 13 Byte Ordering Solution The networking API provides us the following functions uint16 t htons uint16 t host16bitvalue uint32 t htonl uint32 t host32bitvalue uint16 t ntohs uint16 t net16bitvalue uint32 t ntohl uint32 t net32bitvalue Use for all 16 bit and 32 bit binary numbers short int to be sent across network h stands for host order These routines do nothing on big endian hosts Note common mistake is to forget to use these 14 Why Can t Sockets Hide These Details Dealing with endian differences is tedious Couldn t the socket implementation deal with this by swapping the bytes as needed No swapping depends on the data type Two byte short int byte 1 byte 0 vs byte 0 byte 1 Four byte long int byte 3 byte 2 byte 1 byte 0 vs byte 0 byte 1 byte 2 byte 3 String of one byte characters char 0 char 1 char 2 never requires reordering Socket layer doesn t know the data types Sees the data as simply a buffer pointer and a length Doesn t have enough information to do the swapping 15 Clients vs Server setup Client Create a Socket Determine server address and


View Full Document

Berkeley ELENG 122 - Socket Programming

Documents in this Course
Lecture 6

Lecture 6

22 pages

Wireless

Wireless

16 pages

Links

Links

21 pages

Ethernet

Ethernet

10 pages

routing

routing

11 pages

Links

Links

7 pages

Switches

Switches

30 pages

Multicast

Multicast

36 pages

Switches

Switches

18 pages

Security

Security

16 pages

Switches

Switches

18 pages

Lecture 1

Lecture 1

56 pages

OPNET

OPNET

5 pages

Lecture 4

Lecture 4

16 pages

Ethernet

Ethernet

65 pages

Models

Models

30 pages

TCP

TCP

16 pages

Wireless

Wireless

48 pages

Load more
Download Socket Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Socket Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Socket Programming and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?