cs 241 section week #12 (04/23/09). outline lmp2 overview brief intro to networking what is a...
TRANSCRIPT
![Page 1: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/1.jpg)
CS 241 Section Week #12(04/23/09)
![Page 2: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/2.jpg)
Outline
• LMP2 Overview• Brief intro to Networking• What is a protocol?• OSI Model• TCP/IP Model• UDP and TCP• Socket Programming Library Functions• Hypertext Transfer Protocol
![Page 3: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/3.jpg)
LMP2 Overview
![Page 4: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/4.jpg)
LMP2 Overview
• LMP2 encodes or decodes a number of files• It has the following parameters:
– the filenames – the number of bytes (rw_units) for each
read/write from the file;• But, process files in “round robin” fashion
• encrypt rw_unit of file1• encrypt rw_unit of file2• encrypt rw_unit of file3• encrypt rw_unit of file1• ………
![Page 5: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/5.jpg)
LMP2 Overview
• For the output in 2nd part, – print in get_frame() will suffice– you may print in my_munmap.
• Most csil machines, use signed char, whose range is from -128 to 127. So use unsigned char if you compare with 255.
![Page 6: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/6.jpg)
LMP2 Overview• For N files,
-The Table of File-Mappings has exactly N entries -The Virtual Page Table has enough entries to fit each file's length divided by 4096B -The Physical Memory Frame Table has 16 entries
• Example: 3 files of length 3072bytes, 2048bytes, and 8192bytes: -The Table of File-Mappings has 3 entries-The Virtual Page Table has:
(3072/4096=>1) + (2048/4096=>1) + (8192/4096=>2) = 4 entries -The Physical Memory Frame Table has 16 entries and four of them are filled
![Page 7: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/7.jpg)
Brief intro to Networking
![Page 8: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/8.jpg)
Networking
• Allows computers and other networked devices to talk to each other– How can we tell what the packet we just received means?
• Interactions between applications on different machines are governed by protocols– Protocols dictate the format and sequence of the
information exchange
![Page 9: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/9.jpg)
Names and Addresses
• A network address identifies a specific computer on the network– Several kinds of addresses exist: MAC address (for LANs),
IP address (for the Internet), etc.
• Domain or DNS names are used for convenience, so we don’t have to remember numerical addresses
![Page 10: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/10.jpg)
Ports• Ports are numbers that represent an end-point for
communication– Ports are logical, not physical, entities– All packets arrive to the same physical interface, and are
then differentiated based on the port number (and other contents of the packet header)
• Usually, distinct ports are used for communication via different protocols– E.g., port 80 is for HTTP, 22 is for SSH, etc.– See /etc/services for a list
![Page 11: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/11.jpg)
What is a protocol?
![Page 12: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/12.jpg)
12
What is a protocol?
• It is a formal description of message formats and the rules that two computers must follow in order to exchange messages.
• This set of rules describes how data is transmitted over a network.
![Page 13: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/13.jpg)
13
Why are protocols needed?
• Protocols are needed for communication between any two devices. – In what format will the messages be transmitted?– At what speed should messages be transmitted?– What to do if errors take place?– What to do if parts of a message are lost?
![Page 14: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/14.jpg)
14
Network Model• What is a model? – A hypothetical description of a
complex entity or process.
• Network model - A method of describing and analyzing data communications networks by breaking the entire set of communications process into a number of layers
• Each layer has a specific function
![Page 15: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/15.jpg)
15
Open Systems Interconnect (OSI) Model
• Who made: – International Standards Organization (ISO)
• A Model of How Protocols and Networking Components Could be Made
• “Open” means the concepts are non-proprietary; can be used by anyone.
• OSI is not a protocol. It is a model for understanding and designing a network architecture that is flexible and robust.
![Page 16: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/16.jpg)
17
7-layer OSI model
• Why so many layers?– To reduce complexity, networks are organized as
a stack of layers, one below the other
– Each layer performs a specific task. It provides services to an adjacent layer
– This is similar to the concept of a function in programming languages – function do a specific task
![Page 17: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/17.jpg)
18
The Layers of the OSI Model
Application
Presentation
Session
Transport
Network
Data Link
Physical
![Page 18: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/18.jpg)
19
The Layers of the OSI ModelSome Mnemonics
Application
Presentation
Session
Transport
Network
Data Link
Physical
All
People
Seem
To
Need
Data
Processing
Please
Do
Not
Tell
Secret
Passwords
Anytime
![Page 19: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/19.jpg)
20
Physical layer• Specifications for the physical
components of the network• Bit representation: encode bits into electrical or
optical signals• Transmission rate: the number of bits sent each
second
Application
Presentation
Session
Transport
Network
Data Link
Physical
![Page 20: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/20.jpg)
21
Data Link LayerResponsible for delivery of data between two systems on the same network. Main functions are:
• Framing: divides the stream of bits received from network layer into manageable data units called frames.
• Physical Addressing: add a header to the frame to define the physical address of the source and the destination machines.
Application
Presentation
Session
Transport
Network
Data Link
Physical
![Page 21: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/21.jpg)
22
Network LayerMain functions of this layer are:
• Responsible for delivery of packets across multiple networks.
• Routing: Provide mechanisms to transmit data over independent networks that are linked together.
Application
Presentation
Session
Transport
Network
Data Link
Physical
![Page 22: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/22.jpg)
23
Transport LayerMain functions of this layer are:
• Responsible for source-to-destination delivery of the entire message.
• Segmentation and reassembly: divide message into smaller segments, number them and transmit. Reassemble these messages at the receiving end.
Application
Presentation
Session
Transport
Network
Data Link
Physical
![Page 23: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/23.jpg)
24
Session LayerMain functions of this layer are:• Dialog control: allows two systems
to enter into a dialog, keep a track of whose turn it is to transmit.
Application
Presentation
Session
Transport
Network
Data Link
Physical
H5synsyn syn
From Presentation Layer
To Transport Layer
Session Layer
From Transport Layer
To Presentation Layer
H5synsyn syn
Session Layer
![Page 24: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/24.jpg)
25
Presentation LayerResponsibilities of this layer are:
• Translation since different computers use different encoding systems (bit order translation
Application
Presentation
Session
Transport
Network
Data Link
Physical
![Page 25: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/25.jpg)
26
Application Layer•Contains protocols that allow the users
to access the network (FTP, HTTP, SMTP, etc)
•DOES NOT include application programs such as email, browsers, word processing applications, etc.
Application
Presentation
Session
Transport
Network
Data Link
PhysicalTo Presentation Layer From Presentation Layer
![Page 26: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/26.jpg)
27
Summary of Functions of Layers
Application
Presentation
Session
Transport
Network
Data Link
Physical
To allow access to network resources
To establish, manage & terminate sessions
To move packets from source to destination
To transmit bits over a medium & provide electrical specs.
To translate, encrypt and compress data
To provide reliable end-to-end message
delivery
To organise bits into frames
![Page 27: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/27.jpg)
TCP/IP Model
• Layers in the TCP/IP model– Physical Layer– Data Link Layer (MAC)– Network Layer (IPv4, IPv6)– Transport Layer (UDP, TCP)– Application Layer (HTTP, SSH)
![Page 28: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/28.jpg)
Networking
• Each layer is (traditionally) self-contained.– The data-link layer doesn’t care if it’s running IPv4
or IPv6, doesn’t care it it’s TCP or UDP.– When processing the data-link layer, no other
layer is (traditionally) considered.
![Page 29: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/29.jpg)
Networking
![Page 30: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/30.jpg)
Networking• Data Link Layer:
– Purpose: To transfer packets in a flat addressing space.
• Multiple packets go into a hub.• The hub bundles packets together to send them
“upstream” if necessary, based only on the MAC address.
– Not significantly covered in this class.
![Page 31: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/31.jpg)
Networking• Network Layer:
– Purpose: To transfer packets in a hierarchical addressing space.
• Streams of packets go into switches.• Switches determine if the address is within it’s
“subnet” and will route based on “subnets” (IPv4/IPv6).
– Also, not significantly covered in this class.
![Page 32: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/32.jpg)
Networking• Transport Layer:
– After the data link and network layer, packets have arrived at the destination computer.
– The transport layer:• Provides “port multiplexing”, allowing for multiple
programs all communicate without receiving every packet.
• Protocol-specific assurances.
![Page 33: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/33.jpg)
UDP
• User Datagram Protocol (UDP)– The simplest commonly used transport protocol.– Provides:
• Port multiplexing• Error-checking (via simple checksum)
![Page 34: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/34.jpg)
UDP
• User Datagram Protocol (UDP)– Does NOT provide:
• Ordering– Packet A may be sent before Packet B, but Packet B may arrive
after Packet A• Reliability
– The sender has no way to know that Destination received some packet sent by Sender (assured by retransmissions)
• Flow / Congestion Control– There’s no mechanism to “tweak” packet size
![Page 35: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/35.jpg)
UDP• User Datagram Protocol (UDP)
– Only a few parameters are needed for UDP:• Source Port• Destination Port
– From the network layer (IP), you have two more parameters:
• Source IP• Destination IP
![Page 36: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/36.jpg)
UDP
• UDP Packet:
Source Port Destination Port
Data Length Checksum
Data
![Page 37: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/37.jpg)
TCP
• Transmission Control Protocol– TCP provides an alternative to UDP to allow for
more assurances– Provides:
• Port multiplexing• Ordered data / no duplicates• Error-free transmission• Flow control• Congestion control
![Page 38: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/38.jpg)
TCP
• TCP Packet:
Source Port Destination Port
ACK Number
SEQ Number
Data
Offset / Flags Window Size
Checksum Urgent Pointer
Optional Fields
![Page 39: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/39.jpg)
Socket Programming
![Page 40: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/40.jpg)
Socket
• Standard APIs for sending and receiving data across computer networks
• Introduced by BSD operating systems in 1983
• POSIX incorporated 4.3BSD sockets and XTI in 2001
• #include <sys/socket.h>
![Page 41: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/41.jpg)
Typical TCP Server-Client
![Page 42: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/42.jpg)
Typical UDP Server-Client
![Page 43: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/43.jpg)
Programming Sockets
• Lucky for us, the OS does most the work between UDP and TCP!– TCP Socket:
• socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
– UDP Socket:• socket(PF_INET, SOCK_DGRAM, 0);
![Page 44: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/44.jpg)
Programming Sockets
• To create a socket in C, you need to run two commands:– socket()
![Page 45: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/45.jpg)
socket
int socket(int domain, int type, int protocol);
• Creates a communication endpoint
• Parameters– domain: AF_INET (IPv4)– type: SOCK_STREAM (TCP) or SOCK_DGRAM (UDP)– protocol: 0 (socket chooses the correct protocol based on
type)
• Returns a nonnegative integer corresponding to a socket file descriptor if successful, -1 with errno set if unsuccessful
![Page 46: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/46.jpg)
Programming Sockets
• To create a socket in C, you need to run two commands:– socket()– bind()
![Page 47: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/47.jpg)
bind
int bind(int socket, const struct sockaddr *address, socklen_t address_len);
• Associates the socket with a port on your local machine
• struct sockaddr_in used for struct sockaddrsa_family_t sin_family; /* AF_INET */in_port_t sinport; /* port number */struct in_addr sin_addr; /* IP address */
• Returns 0 if successful, -1 with errno set if unsuccessful
![Page 48: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/48.jpg)
Programming Sockets
• Since UDP is packet-based and TCP is connection-based, you need to establish a connection in TCP:– Server: listen(), accept()
![Page 49: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/49.jpg)
listen
int listen(int socket, int backlog);
• Puts the socket into the passive state to accept incoming requests
• Internally, it causes the network infrastructure to allocate queues to hold pending requests– backlog: number of connections allowed on the incoming
queue
• bind should be called beforehand
• Returns 0 if successful, -1 with errno set if unsuccessful
![Page 50: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/50.jpg)
acceptint accept(int socket, struct sockaddr *restrict
address, socklen_t *restrict address_len);
• Accepts the pending requests in the incoming queue
• *address is used to return the information about the client making the connection. – sin_addr.s_addr holds the Internet address
• listen should be called beforehand
• Returns nonnegative file descriptor corresponding to the accepted socket if successful, -1 with errno set if unsuccessful
![Page 51: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/51.jpg)
Programming Sockets
• Since UDP is packet-based and TCP is connection-based, you need to establish a connection in TCP:– Server: listen(), accept()– Client: connect()
![Page 52: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/52.jpg)
connect
int connect(int socket, const struct sockaddr *address, socklen_t address_len);
• Establishes a link to the well-known port of the remote server
• Initiates the TCP 3-way handshake– Cannot be restarted even if interrupted
• Returns 0 if successful, -1 with errno set if unsuccessful
![Page 53: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/53.jpg)
Programming Sockets
• In both TCP and UDP, you send and receive by using the same calls:– recv() / recvfrom()
![Page 54: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/54.jpg)
recv and recvfromint recv(int socket, void *buf, int len, int flags);
int recvfrom(int socket, void *buf, int len, int flags, const struct sockaddr *from, socklet_t fromlen);
• receives data into the buffer buf
• recvfrom is used for unconnected datagram sockets. If used in connection-mode, last two parameters are ignored.
• Returns the number of bytes actually read if successful, -1 with errno set if unsuccessful
![Page 55: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/55.jpg)
Programming Sockets
• In both TCP and UDP, you send and receive by using the same calls:– recv() / recvfrom()– send() / sendto()
![Page 56: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/56.jpg)
send and sendtoint send(int socket, const void *msg, int len, int flags);
int sendto(int socket, const void *msg, int len, int flags, const struct sockaddr *to, socklet_t tolen);
• sends data pointed by msg
• sendto is used for unconnected datagram sockets. If used in connection-mode, last two parameters are ignored.
• Returns the number of bytes actually sent out if successful, -1 with errno set if unsuccessful
![Page 57: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/57.jpg)
close and shutdownint close(int socket);
int shutdown(int socket, int how);
• close– Prevents any more reads and writes– same function covered in file systems
• shutdown– provides a little more control– how
• 0 – Further receives are disallowed• 1 – Further sends are disallowed• 2 – same as close
• Returns 0 if successful, -1 with errno set if unsuccessful
![Page 58: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/58.jpg)
Using Sockets in C
#include <sys/socket.h>#include <sys/types.h>#include <netinet/in.h>#include <unistd.h>
On csil-core:gcc –o test test.c
On some systems, e.g., Solaris:gcc –o test test.c –lsocket -lnsl
![Page 59: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/59.jpg)
TCP Client/Server Example
Run the provided test-server and test-client executables in two separate windows.test-client sends the string “Hello World!” to IP address
127.0.0.1 port 10000test-server listens on port 10000 and prints out any text
received
Next week, we will reproduce this behavior with codes client.c and server.c
![Page 60: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/60.jpg)
HyperText Transfer Protocol
![Page 61: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/61.jpg)
HTTP
• Hypertext Transfer Protocol– Delivers virtually all files and resources on the
World Wide Web– Uses Client-Server Model
• HTTP transaction– HTTP client opens a connection and sends a
request to HTTP server– HTTP server returns a response message
![Page 62: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/62.jpg)
HTTP (continued)
• Request– GET /path/to/file/index.html HTTP/1.0– Other methods (POST, HEAD) possible for request
• Response– HTTP/1.0 200 OK– Common Status Codes
• 200 OK• 404 Not Found• 500 Server Error
![Page 63: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/63.jpg)
Sample HTTP exchange
• Scenario– Client wants to retrieve the file at the following URL
(http://www.somehost.com/path/file.html)
• What a client does– Client opens a socket to the host www.somehost.com, port
80– Client sends the following message through the socket
GET /path/file.html HTTP/1.0From: [email protected]: HTTPTool/1.0[blank line here]
![Page 64: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/64.jpg)
Sample HTTP exchange
• What a server does– Server responds through the same socket
HTTP/1.0 200 OKDate: Mon, 17 Apr 2006 23:59:59 GMTContent-Type: text/htmlContent-Length: 1354
<html><body>(more file contents) . . .</body></html>
![Page 65: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/65.jpg)
Reference
• Beej's Guide to Network Programming– http://beej.us/guide/bgnet/
![Page 66: CS 241 Section Week #12 (04/23/09). Outline LMP2 Overview Brief intro to Networking What is a protocol? OSI Model TCP/IP Model UDP and TCP Socket Programming](https://reader036.vdocuments.us/reader036/viewer/2022070412/56649f1c5503460f94c32354/html5/thumbnails/66.jpg)
Summary
• Protocols
• Socket Programming– Library Functions
• Hypertext Transfer Protocol– Request– Response