More Network ProgrammingSlide 2HTTP Request FramingHTTP Server PushSlide 5Slide 7Select and PollSelect and Poll: PrototypesSelectFile Descriptor SetsSlide 12TimeoutSlide 14Select: ExamplePollDescriptorsEvent FlagsSlide 19Poll: ExampleAdvanced SocketsSlide 22Posix Threads (pthreads)pthreads AttributesSlide 25pthread Error Handlingpthread Creationpthread CleanupSlide 29pthread Cleanup PitfallsSlide 31Unix FilesUnix File OperationsFile: OpenFile: ReadFile: WriteFile: CloseMultithreading-Safe FunctionsSlide 39ExampleExample: Server Thread Flow ChartExample: Client Thread Flow ChartMakefileset_up_server_socketSlide 45wait_for_connectionsSlide 47Slide 48client_threadclient_has_dataMore Network Programming01/14/19 UIUC - CS/ECE 438, Fall 2006 2More Network ProgrammingHTTP push serverRequest framing and server push conceptsDemoUseful API’sselect/poll and advanced sockets tidbitsthreadsHTTP push server codeComponentsFlow chartsCode walk-through (code is online)01/14/19 UIUC - CS/ECE 438, Fall 2006 3HTTP Request FramingCharacteristicsASCII-based (human readable)Framed by text linesFirst line is commandRemaining lines are additional dataBlank line ends request frameGET /surf/too/much.html HTTP/1.0Date: 28 February 2006 11:25:53 CSTHost: www.surfanon.org<blank line>01/14/19 UIUC - CS/ECE 438, Fall 2006 4HTTP Server PushIdeaConnection remains openServer pushes down new data as neededTerminationAny time by serverStop loading (or reload) by clientComponentsHeader indicating multiple partsNew part replaces old partNew part sent any timeWrappers for each part01/14/19 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---01/14/19 UIUC - CS/ECE 438, Fall 2006 7More Network ProgrammingUseful Application Programming InterfacesTopicsMore advanced socketsUnix file functionalityMultithreaded programming (Posix Threads)Specific APIsselect/poll and advanced socketsThreadsAttributesCreationThread-specific dataMinor synchronization01/14/19 UIUC - CS/ECE 438, Fall 2006 8Select and PollBuilding timeouts with select/pollSimilar functionsParametersSet of file descriptorsSet of events for each descriptorTimeout lengthReturn valueSet of file descriptorsEvents for each descriptorNotesSelect is somewhat simplerPoll supports more events01/14/19 UIUC - CS/ECE 438, Fall 2006 9Select and Poll: PrototypesSelect: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, int timeout);01/14/19 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 errnoParameters:num_fds: number of file descriptors to check, numbered from 0read_set, write_set, except_set:Sets (bit vectors) of file descriptors to check for the specific conditiontimeout:Time to wait for a descriptor to become ready01/14/19 UIUC - CS/ECE 438, Fall 2006 11File Descriptor SetsBit vectors Often 1024 bits (FD_SETSIZE)Only first num_fds checkedMacros 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? */01/14/19 UIUC - CS/ECE 438, Fall 2006 12File Descriptor SetsThree conditions to check forReadable: Data available for readingWritable: Buffer space available for writingException: Out-of-band data available (TCP)01/14/19 UIUC - CS/ECE 438, Fall 2006 13TimeoutStructurestruct timeval {long tv_sec; /* seconds */long tv_usec; /* microseconds */};01/14/19 UIUC - CS/ECE 438, Fall 2006 14SelectHigh-resolution sleep functionAll descriptor sets NULLPositive timeoutWait until descriptor(s) become readyAt least one descriptor in settimeout NULLWait until descriptor(s) become ready or timeout occursAt least one descriptor in setPositive timeoutCheck descriptors immediately (poll)At least one descriptor in set0 timeout01/14/19 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 */01/14/19 UIUC - CS/ECE 438, Fall 2006 16Poll#include <poll.h>int poll (struct pollfd* pfds, nfds_t nfds, int timeout);Poll file descriptors for events.Return:Number of descriptors with events-1 on error, sets errnoParameters:pfds:An array of descriptor structures. File descriptors, desired events and returned eventsnfds:Length of the pfds arraytimeout:Timeout value in milliseconds01/14/19 UIUC - CS/ECE 438, Fall 2006 17DescriptorsStructurestruct pollfd {int fd; /* file descriptor */short events; /* queried event bit mask */short revents; /* returned event mask */Note:Any structure with fd < 0 is skipped01/14/19 UIUC - CS/ECE 438, Fall 2006 18Event FlagsPOLLIN:data available for readingPOLLOUT:Buffer space available for writingPOLLERR:Descriptor has error to reportPOLLHUP:Descriptor hung up (connection closed)POLLVAL:Descriptor invalid01/14/19 UIUC - CS/ECE 438, Fall 2006 19PollHigh-resolution sleep function0 nfdsPositive timeoutWait until descriptor(s) become readynfds > 0timeout INFTIM or -1Wait until descriptor(s) become ready or timeout occursnfds > 0Positive timeoutCheck descriptors immediately (poll)nfds > 00 timeout01/14/19 UIUC - CS/ECE 438, Fall 2006 20Poll: Examplestruct pollfd my_pfds[1];my_pfds[0].fd = 0;my_pfds[0].events = POLLIN;if (poll(&my_pfds, 1, INFTIM) == 1) {ASSERT (my_pfds[0].revents & POLLIN);/* data ready on stdin */01/14/19 UIUC - CS/ECE 438, Fall 2006 21Advanced Socketssignal (SIGPIPE, SIG_IGN);Call at start of main in serverAllows you to ignore broken pipe signals which are degenerated when you write to a socket
View Full Document