More Network Programming10/11/06 UIUC - CS/ECE 438, Fall 2006 2More Network Programming HTTP push server Request framing and server push concepts Demo Useful API’s select/poll and advanced sockets tidbits threads HTTP push server code Components Flow charts Code walk-through (code is online)10/11/06 UIUC - CS/ECE 438, Fall 2006 3HTTP Request Framing Characteristics ASCII-based (human readable) Framed by text lines First line is command Remaining lines are additional data Blank line ends request frame10/11/06 UIUC - CS/ECE 438, Fall 2006 3HTTP Request Framing Characteristics ASCII-based (human readable) Framed by text lines First line is command Remaining lines are additional data Blank line ends request frameGET /surf/too/much.html HTTP/1.0Date: 28 February 2006 11:25:53 CSTHost: www.surfanon.org<blank line>10/11/06 UIUC - CS/ECE 438, Fall 2006 4HTTP Server Push Idea Connection remains open Server pushes down new data as needed Termination Any time by server Stop loading (or reload) by client Components Header indicating multiple parts New part replaces old part New part sent any time Wrappers for each part10/11/06 UIUC - CS/ECE 438, Fall 2006 5HTTP Server Push10/11/06 UIUC - CS/ECE 438, Fall 2006 5HTTP Server PushHTTP/1.0 200 OKContent-type: multipart/x-mixed-replace;\boundary=---never_in_document------never_in_document---10/11/06 UIUC - CS/ECE 438, Fall 2006 5HTTP Server Pushthe data componentHTTP/1.0 200 OKContent-type: multipart/x-mixed-replace;\boundary=---never_in_document------never_in_document---10/11/06 UIUC - CS/ECE 438, Fall 2006 5HTTP Server Pushthe data componentHTTP/1.0 200 OKContent-type: multipart/x-mixed-replace;\boundary=---never_in_document------never_in_document---Content-type: text/html(actual data)---never_in_document---10/11/06 UIUC - CS/ECE 438, Fall 2006 6HTTP Push ServerDemonstration Server running onport 5012 File of interest: test.html Driven by unrelated process (not the server) Changes every 2 seconds Alternates between 13 files (a through m) Demonstration Server detects file changes Pushes update after every change10/11/06 UIUC - CS/ECE 438, Fall 2006 7More Network Programming Useful Application Programming Interfaces Topics More advanced sockets Unix file functionality Multithreaded programming (Posix Threads) Specific APIs select/poll and advanced sockets Threads Attributes Creation Thread-specific data Minor synchronization10/11/06 UIUC - CS/ECE 438, Fall 2006 8Select and Poll Building timeouts with select/poll Similar functions Parameters Set of file descriptors Set of events for each descriptor Timeout length Return value Set of file descriptors Events for each descriptor Notes Select is somewhat simpler Poll supports more events10/11/06 UIUC - CS/ECE 438, Fall 2006 9Select and Poll: Prototypes Select: Wait for readable/writable file descriptors#include <sys/time.h>int select (int num_fds, fd_set* read_set, fd_set*write_set, fd_set* except_set, struct timeval*timeout); Poll: Poll file descriptors for events#include <poll.h>int poll (struct pollfd* pfds, nfds_t nfds, inttimeout);10/11/06 UIUC - CS/ECE 438, Fall 2006 10Selectint select (int num_fds, fd_set* read_set, fd_set*write_set, fd_set* except_set, struct timeval*timeout); Wait for readable/writable file descriptors. Return: Number of descriptors ready -1 on error, sets errno Parameters: num_fds: number of file descriptors to check, numbered from 0 read_set, write_set, except_set: Sets (bit vectors) of file descriptors to check for the specific condition timeout: Time to wait for a descriptor to become ready10/11/06 UIUC - CS/ECE 438, Fall 2006 11File Descriptor Sets Bit vectors Often 1024 bits (FD_SETSIZE) Only first num_fds checked Macros to create and check setsfds_set myset;void FD_ZERO (&myset); /* clear all bits */void FD_SET (n, &myset); /* set bits n to 1 */void FD_CLEAR (n, &myset); /* clear bit n */int FD_ISSET (n, &myset); /* is bit n set? */10/11/06 UIUC - CS/ECE 438, Fall 2006 12File Descriptor Sets Three conditions to check for Readable: Data available for reading Writable: Buffer space available for writing Exception: Out-of-band data available (TCP)10/11/06 UIUC - CS/ECE 438, Fall 2006 13Timeout Structurestruct timeval {long tv_sec; /* seconds */long tv_usec; /* microseconds */};10/11/06 UIUC - CS/ECE 438, Fall 2006 14Select High-resolution sleep function All descriptor sets NULL Positive timeout Wait until descriptor(s) become ready At least one descriptor in set timeout NULL Wait until descriptor(s) become ready or timeout occurs At least one descriptor in set Positive timeout Check descriptors immediately (poll) At least one descriptor in set 0 timeout10/11/06 UIUC - CS/ECE 438, Fall 2006 15Select: Examplefd_set my_read;FD_ZERO(&my_read);FD_SET(0, &my_read);if (select(1, &my_read, NULL, NULL) == 1) {ASSERT(FD_ISSET(0, &my_read);/* data ready on stdin */10/11/06 UIUC - CS/ECE 438, Fall 2006 16Poll#include <poll.h>int poll (struct pollfd* pfds, nfds_t nfds, inttimeout); Poll file descriptors for events. Return: Number of descriptors with events -1 on error, sets errno Parameters: pfds: An array of descriptor structures. File descriptors, desired events and returnedevents nfds: Length of the pfds array timeout: Timeout value in milliseconds10/11/06 UIUC - CS/ECE 438, Fall 2006 17Descriptors Structurestruct pollfd {int fd; /* file descriptor */short events; /* queried event bit mask */short revents; /* returned event mask */ Note: Any structure with fd < 0 is skipped10/11/06 UIUC - CS/ECE 438, Fall 2006 18Event Flags POLLIN: data available for reading POLLOUT: Buffer space available for writing POLLERR: Descriptor has error to report POLLHUP: Descriptor hung up (connection closed) POLLVAL: Descriptor invalid10/11/06 UIUC - CS/ECE 438, Fall 2006 19Poll High-resolution sleep function 0 nfds Positive timeout Wait until descriptor(s) become ready nfds > 0 timeout INFTIM or -1 Wait until descriptor(s) become ready or timeout occurs nfds > 0 Positive timeout Check descriptors immediately (poll) nfds > 0 0 timeout10/11/06 UIUC - CS/ECE 438,
View Full Document