cs 241 discussion section (11/11/--11). outline good ideas for mp7 socket programming hypertext...

40
CS 241 Discussion Section (11/11/--11)

Upload: naomi-hall

Post on 04-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

CS 241 Discussion Section(11/11/--11)

Page 2: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Outline

Good Ideas for MP7 Socket ProgrammingHypertext Transfer Protocol (HTTP)

Page 3: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Hints for MP7

Following Slides stolen from CMU CS 213Good ideas in book: Chapter 23 Sec 10.9

Page 4: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Basic allocator mechanisms

Sequential fits (implicit or explicit single free list)best fit, first fit, or next fit placementvarious splitting and coalescing options

splitting thresholdsimmediate or deferred coalescing

Segregated free listssimple segregated storage -- separate heap for each size classsegregated fits -- separate linked list for each size class

buddy systems

Page 5: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Segregate StorageEach size “class” has its own collection of blocks

1-2

3

4

5-8

9-16

• Often have separate collection for every small size (2,3,4,…)

• For larger sizes typically have a collection for each power of 2

Page 6: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Simple segregated storage

Separate heap and free list for each size classNo splittingTo allocate a block of size n:

if free list for size n is not empty,allocate first block on list (note, list can be implicit or explicit)

if free list is empty, get a new page create new free list from all blocks in pageallocate first block on list

constant time

To free a block:Add to free listIf page is empty, return the page for use by another size (optional)

Tradeoffs:fast, but can fragment badly

Page 7: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Segregated fits

Array of free lists, each one for some size classTo allocate a block of size n:

search appropriate free list for block of size m > nif an appropriate block is found:

split block and place fragment on appropriate list (optional)

if no block is found, try next larger classrepeat until block is found

To free a block:coalesce and place on appropriate list (optional)

Tradeoffsfaster search than sequential fits (i.e., log time for power of two size classes)controls fragmentation of simple segregated storagecoalescing can increase search times

deferred coalescing can help

Page 8: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Buddy systemsSpecial case of segregated fits.

all blocks are power of two sizesBasic idea:

Heap is 2m wordsMaintain separate free lists of each size 2k, 0 <= k <= m.Requested block sizes are rounded up to nearest power of 2.Originally, one free block of size 2m.

Page 9: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Buddy systems (cont)To allocate a block of size 2k:

Find first available block of size 2j s.t. k <= j <= m.if j == k then done.otherwise recursively split block until j == k. Each remaining half is called a “buddy” and is placed on the appropriate free list

2m

buddy

buddy

buddy

Page 10: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Buddy systems (cont)To free a block of size 2k

continue coalescing with buddies while the buddies are free

buddy

buddy

Block to free

buddy

Not free, done

Added to appropriate free list

Page 11: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Buddy systems (cont)Key fact about buddy systems:

given the address and size of a block, it is easy to compute the address of its buddy

e.g., block of size 32 with address xxx...x00000 has buddy xxx...x10000

Tradeoffs:fast search and coalescesubject to internal fragmentation

Page 12: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Internal fragmentationInternal fragmentation is wasted space inside

allocated blocks:minimum block size larger than requested amount

e.g., due to minimum free block size, free list overhead

policy decision not to split blockse.g., buddy system Much easier to define and measure than

external fragmentation.

Page 13: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Other Sources of Wisdom

Many implementations and algorithms online…

All work should be your own!

Good Luck

12/02/09

Page 14: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Socket Programming

Page 15: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

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 16: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

12/02/09

Typical TCP Server-Client

Page 17: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

12/02/09

Typical TCP Server-Client

Page 18: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Typical UDP Server-Client

Page 19: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Programming Sockets

• To create a socket in C, you need to run two commands:• socket()• bind()

Page 20: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

socket

int socket(int domain, int type, int protocol);

Returns a nonnegative integer (socket file descriptor)

• Parameters• domain: AF_INET (IPv4)• type: SOCK_STREAM (TCP) or SOCK_DGRAM (UDP)• protocol: 0 (socket chooses the correct protocol

based on type)

• TCP: socket(AF_INET,SOCK_STREAM, 0);• UDP: socket(AF_INET, SOCK_DGRAM, 0);

Page 21: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

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 sockaddr

sa_family_t sin_family; /* AF_INET */in_port_t sinport; /* port number */struct in_addr sin_addr; /* IP address */

Page 22: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Programming Sockets

• UDP is packet-based

• TCP is connection-based• you need to establish a connection in TCP:• Server: listen(), accept()• Client: connect()

Page 23: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

A Generic TCP Server & Client Script

Serversocket()

bind()

listen()

while (…) {

accept()

send()/recv()

}

close()

Clientsocket()

connect()

while (…) {

send()/recv()

}

close()

What is the problem with the server?

Page 24: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

A Generic TCP Server & Client Script

Serversocket()

bind()

listen()

while (…) {

accept()

send()/recv()

}

close()

Clientsocket()

connect()

while (…) {

send()/recv()

}

close()

Handle one request at a timeHow to fix this?

Page 25: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

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

Page 26: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

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 27: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

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 28: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Programming Sockets

• In both TCP and UDP, you send and receive by using the same calls:• send() / sendto()• recv() / recvfrom()

Page 29: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

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, socklen_t tolen);

send sends along an established connection (TCP), while sendto sends to an address (UDP).

The extra two parameters specify the destination.

Page 30: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

recv and recvfromint recv(int socket, const void *msg, int len, int flags);

int recvfrom(int socket, const void *msg, int len, int flags, const struct sockaddr *from, socklen_t *fromlen);

recv receives from an established connection (TCP), while recvfrom receives from anywhere (UDP), and saves the address.

The extra two parameters specify the source.

Page 31: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

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 32: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

TCP vs. UDP at a glance

TCP UDP

Socket type SOCK_STREAM SOCK_DGRAM

Form of data transmitted Stream Packets

Calls for sending and receiving send, recv sendto, recvfrom

Uses sessions? Yes No

Overhead for ordering packets Substantial Minimal

Example Services FTP, HTTP DNS, SNMP

Page 33: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

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 34: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

TCP Client/Server Example

Run the provided server.c and client.c executables in two separate windows.

client sends the string “Hello World!” to IP address 127.0.0.1 port 10000

server listens on port 10000 and prints out any text received

Page 35: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

HyperText Transfer Protocol

Page 36: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

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 37: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

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 38: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

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 socketGET /path/file.html HTTP/1.0From: [email protected]: HTTPTool/1.0[blank line here]

Page 39: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Sample HTTP exchange

• What a server does• Server responds through the same socketHTTP/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 40: CS 241 Discussion Section (11/11/--11). Outline Good Ideas for MP7 Socket Programming Hypertext Transfer Protocol (HTTP)

Reference

• Beej's Guide to Network Programming• http://beej.us/guide/bgnet/