DOC PREVIEW
U of I CS 438 - More Network Programming

This preview shows page 1-2-3-23-24-25-26-47-48-49 out of 49 pages.

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

Unformatted text preview:

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 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)01/14/19 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>01/14/19 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 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 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 synchronization01/14/19 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 events01/14/19 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, 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 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 ready01/14/19 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? */01/14/19 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)01/14/19 UIUC - CS/ECE 438, Fall 2006 13TimeoutStructurestruct timeval {long tv_sec; /* seconds */long tv_usec; /* microseconds */};01/14/19 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 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 errnoParameters:pfds:An array of descriptor structures. File descriptors, desired events and returned eventsnfds:Length of the pfds arraytimeout:Timeout value in milliseconds01/14/19 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 skipped01/14/19 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 invalid01/14/19 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 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 serverAllows you to ignore broken pipe signals which are degenerated when you write to a socket


View Full Document

U of I CS 438 - More Network Programming

Documents in this Course
Routing

Routing

5 pages

TCP

TCP

26 pages

TROLL

TROLL

3 pages

Load more
Download More Network 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 More Network 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 More Network Programming 2 2 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?