DOC PREVIEW
Berkeley ELENG 122 - Socket Programming

This preview shows page 1-2-3 out of 8 pages.

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

Unformatted text preview:

Announcements Project 1 out today due Thu Sep 28 Vern Dilip away this week Socket Programming I will be covering Dilip s Friday section office hours in my office EE 122 Intro to Communication Networks No Lecture Wed Sept 13 Fall 2006 MW 4 5 30 in Donner 155 Sukun Kim Vern Paxson TAs Dilip Antony Joseph and Sukun Kim http inst eecs berkeley edu ee122 Materials with thanks to Artur Rivilis Jennifer Rexford Ion Stoica and colleagues at Princeton and UC Berkeley 1 2 Overview Socket End Point of Communication Sending message from one process to another Message must traverse the underlying network Socket Programming how applications use the network Process sends and receives through a socket In essence the doorway leading in out of the house 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 Socket as an Application Programming Interface Supports the creation of network applications 3 Identifying the Receiving Process User process User process socket socket Operating System Operating System 4 Using Ports to Identify Services Sending process must identify the receiver Server host 128 2 194 242 Address of the receiving end host Plus identifier port that specifies the receiving process Client host Service request for 128 2 194 242 80 i e the Web server Client Receiving host Web server port 80 OS Echo server port 7 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 Service request for 128 2 194 242 7 i e the echo server Port number is a 16 bit quantity Client Web server port 80 OS Echo server port 7 5 6 1 Knowing What Port Number To Use Delivering the Data Division of Labor Popular applications have well known ports Network E g port 80 for Web and port 25 for e mail Well known ports listed at http www iana org Deliver data packet to the destination host Based on the destination IP address Or see etc services on Unix system Well known vs ephemeral ports Operating system Server has a well known port e g port 80 Deliver data to the destination socket Based on the protocol and destination port By convention between 0 and 1023 privileged Client gets an unused ephemeral i e temporary port By convention between 1024 and 65535 Application Uniquely identifying the traffic between the hosts Read data from the socket Interpret the data e g render a Web page 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 7 UNIX Socket API Types of Sockets Different types of sockets implement different service models Socket interface Stream SOCK STREAM Datagram SOCK DGRAM Originally provided in Berkeley UNIX Later adopted by all popular operating systems Simplifies porting applications to different OSes Stream socket TCP 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 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 System calls for sockets Client create connect write send read close Server create bind listen accept read recv write send close 9 Using Stream Sockets 10 Recovering message boundaries No need to packetize data Stream socket data separation Data arrives in the form of a byte stream Use records data structures to partition data stream How do we implement variable length records size of 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 8 record A fixed length record application transport C fixed length record 4 variable length record What if field containing record size gets corrupted network Not possible Why data link physical B User application sends messages Hi there and Hope you are well separately 11 12 2 Datagram Sockets Typical Client Program User packetizes data before sending Prepare to communicate 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 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 13 Creating a Socket socket Close the socket 14 Connecting the Socket to the Server Operation to create a socket Translate the server s name to an address int socket int domain int type int protocol Returns a descriptor or handle for the socket Originally designed to support any protocol suite 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 Domain protocol family Identifying the service s port number Use PF INET for the Internet struct servent getservbyname char name char proto E g getservbyname http tcp Type semantics of the communication SOCK STREAM reliable byte stream SOCK DGRAM message oriented service Establishing the connection Protocol specific protocol UNSPEC unspecified No need for us to specify since PF INET plus SOCK STREAM already implies TCP or 15 SOCK DGRAM implies UDP Sending and Receiving Data int connect int sockfd struct sockaddr server address socketlen t addrlen Arguments socket descriptor server address and address size 16 Returns 0 on success and 1 if an error occurs Sending and Receiving Data con t 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 Receiving data ssize t read int sockfd void buf size t len Arguments socket descriptor pointer


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?