/
CSCI 330 UNIX and Network Programming Unit XVII: Socket Programming Detail CSCI 330 UNIX and Network Programming Unit XVII: Socket Programming Detail

CSCI 330 UNIX and Network Programming Unit XVII: Socket Programming Detail - PowerPoint Presentation

sherrill-nordquist
sherrill-nordquist . @sherrill-nordquist
Follow
361 views
Uploaded On 2019-11-06

CSCI 330 UNIX and Network Programming Unit XVII: Socket Programming Detail - PPT Presentation

CSCI 330 UNIX and Network Programming Unit XVII Socket Programming Detail Unit Overview TCP programming socket behavior blocking vs nonblocking signal handler 2 CSCI 330 UNIX and Network Programming ID: 763680

network programming 330 unix programming network unix 330 blocking csci socket signal write call mode alarm set int system

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "CSCI 330 UNIX and Network Programming Un..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

CSCI 330UNIX and Network Programming Unit XVII: Socket Programming Detail

Unit Overview TCP programming socket behavior blocking vs. non-blockingsignal handler 2 CSCI 330 - UNIX and Network Programming

Socket system calls 3 Primitive Meaning socket Create a new communication endpoint bind Attach a local address to a socket listenAnnounce willingness to accept connectionsacceptBlock caller until a connection request arrivesconnectActively attempt to establish a connectionwriteSend(write) some data over the connectionreadReceive(read) some data over the connectioncloseRelease the connection server client CSCI 330 - UNIX and Network Programming

TCP socket programming sockets can operate in one of two modes: blocking or non-blocking default: blocking modecalls may block, i.e. wait for something before they returnblocking is a special state in which a process is waiting for I/O process is removed from the scheduler queuewhen the I/O completes, the process is woken upnon-blocking modecalls returns immediately, producing full, partial or no result4CSCI 330 - UNIX and Network Programming

Blocking mode: calls that can block accept blocks until a connection is present connectblocks until connection is establishedread blocks if no data is available write blocks when data does not fit into send buffer of the socket5CSCI 330 - UNIX and Network Programming

Illustration: blocking mode // loop to write a single character char c = 'x'; while (true) { if (write(sock, &c, 1) < 1) { perror("write"); exit(EXIT_FAILURE); }} 6CSCI 330 - UNIX and Network Programming

Illustration: dummy server 7 CSCI 330 - UNIX and Network Programming

Illustration: blocking mode 8 CSCI 330 - UNIX and Network Programming

What to do when call blocks ? Answer: wait or: set alarm i.e. schedule signal to sent in futurehow: set alarm timeout program and install signal handler 9 CSCI 330 - UNIX and Network Programming

System call: alarm 10 CSCI 330 - UNIX and Network Programming

System call: alarm unsigned int alarm(unsigned int seconds) arranges for a SIGALRM signal to be delivered to the calling process in seconds secondsany previously set alarm is canceledif seconds is zero, no new alarm is scheduledreturns the number of seconds remaining until any previously scheduled alarm was due to be deliveredreturns zero if there was no previously scheduled alarm11CSCI 330 - UNIX and Network Programming

Illustration: blocking mode 12 CSCI 330 - UNIX and Network Programming

System call: signal 13 CSCI 330 - UNIX and Network Programming

System call: signal typedef void (* sighandler_t )( int ); sighandler_t signal(int signum, sighandler_t handler)sets the disposition of the signal signum to handlerhandler can be:SIG_IGN to ignore signalSIG_DFL to restore defaultaddress of a programmer-defined functionreturns the previous value of the signal handler 14CSCI 330 - UNIX and Network Programming

Signal handler: detail void alarm_action ( int s) { cerr << "write blocked after " << count << " characters\n"; exit(EXIT_SUCCESS);}// set signal handler on ALARMsignal(SIGALRM, alarm_action);15CSCI 330 - UNIX and Network Programming

Illustration: blocking mode w/signal 16 CSCI 330 - UNIX and Network Programming

TCP socket: non-blocking mode calls returns immediately, producing full, partial or no result socket can be in blocking or non-blocking mode determined by flag associated with socket descriptor flag can be set via fcntl system call 17 CSCI 330 - UNIX and Network Programming

Non-Blocking mode: call behavior accept when there are connections to accept, accept behaves as usualif there are no pending connections, returns −1 with errno set to EWOULDBLOCKconnectwhen connection to a listening server can be established, connect behaves as usualif connection cannot be established, connect returns −1 with errno set to EINPROGRESS18CSCI 330 - UNIX and Network Programming

Non-Blocking mode: call behavior read when there is data to read, read behaves as usualif there is no data, read returns −1 with errno EWOULDBLOCK write when buffer space is available, write behaves as usualif buffer becomes full, write returns number of bytes writtenif buffer is full, write returns -1 with errno EWOULDBLOCK19CSCI 330 - UNIX and Network Programming

System call: fcntl 20 CSCI 330 - UNIX and Network Programming

System call: fcntl int fcntl ( int fd , int cmd, ... /* arg */ )performs operation cmd on the open descriptor fdcmd to set non-blocking mode for socket: F_SETFL, O_NONBLOCKreturns -1 on error21CSCI 330 - UNIX and Network Programming

Illustration: non-blocking write 22 CSCI 330 - UNIX and Network Programming

Summary TCP programming socket behavior blocking vs. non-blockingsignal handler 23 CSCI 330 - UNIX and Network Programming