UNIX Network Programming1 Chapter 15. Nonblocking I/O.

Slides:



Advertisements
Similar presentations
Echo server The client reads a line of text from its standard input and writes the line to the server The server reads the line from its network input.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Nonblocking I/O Blocking vs. non-blocking I/O
ADVANCED NETWORK PROGRAMMING BİLGİN METİN ADVANCED SOCKET OPTIONS  IPv4&IPv6 Interoperability  IOCTL Options  Advanced I/O Functions  Nonblocking I/O.
I/O Multiplexing: select and poll
Chapter 6 I/O Multiplexing: select and poll function.
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Socket Programming Application Programming Interface.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Computer Networks Sockets. Sockets and the OS F An end-point for Internet connection –What the application “plugs into” –OS provides Application Programming.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
1 TCP Sockets Computer Network Programming. 2 TCP Echo Server We will write a simple echo server and client –client read a line of text from standard.
Network Programming UNIX Internet Socket API. Everything in Unix is a File –When Unix programs do any sort of I/O, they do it by reading or writing to.
Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
TDC561 Network Programming Camelia Zlatea, PhD Week 2 – part II: Socket Application Programming Interface.
I/O Multiplexing Capability of tell the kernel that wants to be notified when one or more I/O conditions are ready. For example, I/O data is available.
1) The server should be concurrent. This implies that it should loop infinitely, listening for clients requests. It should NOT terminate after accepting.
1 Nonblocking I/O Nonblocking reads and writes Buffers enabling overlapped nonblocking I/O Nonblocking connect.
1 I/O Multiplexing Computer Network Programming. 2 Input from multiple sources Process keyboard sockets file A process may have multiple sources of input.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Protocol Programs that communicate across a network must agree on a protocol on how they will communicate High-level decisions must be made on which program.
Lecture 8 UDP Sockets & I/O Multiplexing
Chapter 5. TCP Client-Server Example. Contents –Introduction –TCP Echo Server –TCP Echo Client –Normal Startup and Termination –Posix Signal Handling.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
1 TCP Client-Server Example TCP echo server: main and str_echo TCP echo client: main and str_cli Normal startup and termination POSIX signal handling Handling.
1 Non-blocking I/O Computing Network Programming.
UNIX Network Programming1 UNIX Network Programming 2nd Edition.
Chapter 8 Elementary UDP Socket. Contents u recvfrom and sendto Function u UDP Echo Server( main, de_echo Function) u UDP Echo Client( main, de_cli Function)
Sirak Kaewjamnong Computer Network Systems
Nonblocking I/O Blocking vs. non-blocking I/O Nonblocking input, output, accept, and connect Readings –UNP Ch16 1.
Elementary TCP Sockets –The presentation will provide sufficient information to build a COMPLETE TCP client and server. –In addition the topic of concurrency.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
TELE 402 Lecture 4: I/O multi … 1 Overview Last Lecture –TCP socket and Client-Server example –Source: Chapters 4&5 of Stevens’ book This Lecture –I/O.
TELE 402 Lecture 10: Unix domain … 1 Overview Last Lecture –Daemon processes and advanced I/O functions This Lecture –Unix domain protocols and non-blocking.
Chapter18 broadcasting. contents Introduction broadcast address unicast versus broadcast dg_cli function using broadcasting Race conditions.
Chapter 14 Unix domain protocol. contents Introduction unix domain socket address structure socketpair socket function unix domain stream client-server.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
1 I/O Multiplexing We often need to be able to monitor multiple descriptors:We often need to be able to monitor multiple descriptors: –a generic TCP client.
CSCE 515: Computer Network Programming Select Wenyuan Xu Department of Computer Science and Engineering.
Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol ,
Introduction A Simple Daytime Client A Simple Daytime Server
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
CMPT 471 Networking II Network Programming © Janice Regan,
UNIX Network Programming1 Chapter 13. Advanced I / O Functions.
CSCI 330 UNIX and Network Programming Unit XVII: Socket Programming Detail.
Ioctl Operations. ioctl Function Interface Configuration  Netstat, ifconfig command 에서 사용.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
@Yuan Xue CS 283Computer Networks Spring 2013 Instructor: Yuan Xue.
I/O Multiplexing.
Chapter 5. TCP Client-Server Example
Elementary UDP Sockets
CH5 TCP Client - Server Example:
Review: TCP Client-Server Interaction
Chapter 8 Elementary UDP Socket
UNIX Domain sockets The Linux Programming Interface (ch 57)
Lecture 4 Socket Programming Issues
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
Lecture 11 Overview.
TCP Sockets Programming
TCP/IP Socket Programming in C
Elementary UDP Sockets connectionless, unreliable, datagram
Client-side Networking CSE 333 Summer 2018
TCP Client-Server Example
I/O Multiplexing We often need to be able to monitor multiple descriptors: a generic TCP client (like telnet) need to be able to handle unexpected situations,
Presentation transcript:

UNIX Network Programming1 Chapter 15. Nonblocking I/O

UNIX Network Programming Introduction When a socket call is issued that cannot be completed immediately, the process is put to sleep, waiting for the condition to true divide the socket calls that may block into four categories –Input Operations: the read, readv, recv, recvfrom, and recvmsg functions. (<=sleep until some data arrives), readn, MSG_WAITFLAG, :nonblocking socket => error of EWOULDBLOCK –Output Operations: the write, writev, send, sendto, and sendmsg functions. –Accepting incoming connections: the accept function. –Initiating outgoing connections: the connect function for TCP.

UNIX Network Programming3 Nonblocking Reads and Writes: str_cli Function ( Revisited ) Select operation: blocking I/O –fgets, writen(socket send buffer: full), fputs(standard output is slower than the network) develop a version of the function using nonblocking I/O To contains data going from standard input to the server, and fr contains data arriving from the server going to standard output.

UNIX Network Programming Nonblocking Reads and Writes: str_cli Function ( Revisited )

UNIX Network Programming5 str_cli function ( 1/5 ) void str_cli(FILE *fp, int sockfd) { intmaxfdp1, val, stdineof; ssize_tn, nwritten; fd_setrset, wset; charto[MAXLINE], fr[MAXLINE]; char*toiptr, *tooptr, *friptr, *froptr; val = Fcntl(sockfd, F_GETFL, 0); Fcntl(sockfd, F_SETFL, val | O_NONBLOCK); val = Fcntl(STDIN_FILENO, F_GETFL, 0); Fcntl(STDIN_FILENO, F_SETFL, val | O_NONBLOCK); val = Fcntl(STDOUT_FILENO, F_GETFL, 0); Fcntl(STDOUT_FILENO, F_SETFL, val | O_NONBLOCK); toiptr = tooptr = to;/* initialize buffer pointers */ friptr = froptr = fr; stdineof = 0;

UNIX Network Programming6 str_cli function ( 2/5 ) maxfdp1 = max(max(STDIN_FILENO, STDOUT_FILENO), sockfd) + 1; for ( ; ; ) { FD_ZERO(&rset); FD_ZERO(&wset); if (stdineof == 0 && toiptr < &to[MAXLINE]) FD_SET(STDIN_FILENO, &rset);/* read from stdin */ if (friptr < &fr[MAXLINE]) FD_SET(sockfd, &rset);/* read from socket */ if (tooptr != toiptr) FD_SET(sockfd, &wset);/* data to write to socket */ if (froptr != friptr) FD_SET(STDOUT_FILENO, &wset);/* data to write to stdout */ Select(maxfdp1, &rset, &wset, NULL, NULL); if (FD_ISSET(STDIN_FILENO, &rset)) { if ( (n = read(STDIN_FILENO, toiptr, &to[MAXLINE] - toiptr)) < 0) { if (errno != EWOULDBLOCK) err_sys("read error on stdin"); } else if (n == 0) { fprintf(stderr, "%s: EOF on stdin\n", gf_time()); stdineof = 1;/* all done with stdin */ if (tooptr == to) Shutdown(sockfd, SHUT_WR);/* send FIN */

UNIX Network Programming7 str_cli function ( 3/5 ) } else { fprintf(stderr, "%s: read %d bytes from stdin\n", gf_time(), n); toiptr += n;/* # just read */ FD_SET(sockfd, &wset);/* try and write to socket below */ } if (FD_ISSET(sockfd, &rset)) { if ( (n = read(sockfd, friptr, &fr[MAXLINE] - friptr)) < 0) { if (errno != EWOULDBLOCK) err_sys("read error on socket"); } else if (n == 0) { fprintf(stderr, "%s: EOF on socket\n", gf_time()); if (stdineof) return;/* normal termination */ else err_quit("str_cli: server terminated prematurely"); } else { fprintf(stderr, "%s: read %d bytes from socket\n", gf_time(), n); friptr += n;/* # just read */ FD_SET(STDOUT_FILENO, &wset);/* try and write below */ }

UNIX Network Programming8 str_cli function ( 4/5 ) if (FD_ISSET(STDOUT_FILENO, &wset) && ( (n = friptr - froptr) > 0)) { if ( (nwritten = write(STDOUT_FILENO, froptr, n)) < 0) { if (errno != EWOULDBLOCK) err_sys("write error to stdout"); } else { fprintf(stderr, "%s: wrote %d bytes to stdout\n", gf_time(), nwritten); froptr += nwritten;/* # just written */ if (froptr == friptr) froptr = friptr = fr;/* back to beginning of buffer */ }

UNIX Network Programming9 str_cli function ( 5/5 ) if (FD_ISSET(sockfd, &wset) && ( (n = toiptr - tooptr) > 0)) { if ( (nwritten = write(sockfd, tooptr, n)) < 0) { if (errno != EWOULDBLOCK) err_sys("write error to socket"); } else { fprintf(stderr, "%s: wrote %d bytes to socket\n", gf_time(), nwritten); tooptr += nwritten;/* # just written */ if (tooptr == toiptr) { toiptr = tooptr = to;/* back to beginning of buffer */ if (stdineof) Shutdown(sockfd, SHUT_WR); /* send FIN */ }

UNIX Network Programming10 Figure 15.6 gf_time function #include"unp.h" #include char * gf_time(void) { struct timevaltv; static charstr[30]; char*ptr; if (gettimeofday(&tv, NULL) < 0) err_sys("gettimeofday error"); ptr = ctime(&tv.tv_sec); strcpy(str, &ptr[11]); /* Fri Sep 13 00:00: \n\0 */ /* */ snprintf(str+8, sizeof(str)-8, ".%06ld", tv.tv_usec); return(str); }

UNIX Network Programming11

UNIX Network Programming12 A simpler Version of str_cli the function immediately calls fork to split into a parent and child. the TCP connection is full-duplex and that the parent and child are sharing the same socket descriptor : the parent writes to the socket and the child reads from the socket.

UNIX Network Programming13 Version of str_cli function that uses fork void str_cli(FILE *fp, int sockfd) { pid_tpid; charsendline[MAXLINE], recvline[MAXLINE]; if ( (pid = Fork()) == 0) {/* child: server -> stdout */ while (Readline(sockfd, recvline, MAXLINE) > 0) Fputs(recvline, stdout); kill(getppid(), SIGTERM);/* in case parent still running */ exit(0); } /* parent: stdin -> server */ while (Fgets(sendline, MAXLINE, fp) != NULL) Writen(sockfd, sendline, strlen(sendline)); Shutdown(sockfd, SHUT_WR);/* EOF on stdin, send FIN */ pause(); return; }

UNIX Network Programming14 Timing of str_cli –354.0 sec, stop-and-wait – 12.3 sec, select and blocking I/O – 6.9 sec, nonblocking I/O – 8.7 sec, fork – 8.5 sec, threaded version

UNIX Network Programming Nonblocking connect TCP socket nonblocking connect –return: an error of EINPROGRESS –TCP three-way handshake continues –check the connection establishment using select There are three uses for a nonblocking connect. –We can overlap other processing with the three-way handshake. –We can establish multiple connections at the same time using this technique. –Since we wait for the connection establishment to complete using select, we can specify time limit for select, allowing us to shorten the timeout for the connect.

UNIX Network Programming Nonblocking connect: Daytime Client int connect_nonb(int sockfd, const SA *saptr, socklen_t salen, int nsec) { intflags, n, error; socklen_tlen; fd_setrset, wset; struct timevaltval; flags = Fcntl(sockfd, F_GETFL, 0); Fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); error = 0; if ( (n = connect(sockfd, (struct sockaddr *) saptr, salen)) < 0) if (errno != EINPROGRESS) return(-1); /* Do whatever we want while the connect is taking place. */ if (n == 0) goto done;/* connect completed immediately */ FD_ZERO(&rset); FD_SET(sockfd, &rset); wset = rset; tval.tv_sec = nsec; tval.tv_usec = 0;

UNIX Network Programming Nonblocking connect: Daytime Client (cont.) if ( (n = Select(sockfd+1, &rset, &wset, NULL, nsec ? &tval : NULL)) == 0) { close(sockfd);/* timeout */ errno = ETIMEDOUT; return(-1); } if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) { len = sizeof(error); if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) return(-1);/* Solaris pending error */ } else err_quit("select error: sockfd not set"); done: Fcntl(sockfd, F_SETFL, flags);/* restore file status flags */ if (error) { close(sockfd);/* just in case */ errno = error; return(-1); } return(0); }

UNIX Network Programming nonblocking connect: web client A real-world example of nonblocking connects started with Netscape Web Client The client establishes an HTTP connection with a Web server and fetches a home page. On that page are often numerous references to other Web pages. Instead of fetching these other pages serially, one at a time, the client can fetch more than one at the same time, using nonblocking connects.

UNIX Network Programming nonblocking connect: web client (cont.)

UNIX Network Programming nonblocking connect: web client (cont.)

UNIX Network Programming21 %web 3 image1.gif image2.gif image3.gif image4.gif image5.gif image6.gif image7.gif –three simultaneous connection –server’s hostname –filename for the home page –the files to then read

UNIX Network Programming22 Figure web.h header #include"unp.h" #defineMAXFILES20 #defineSERV"80"/* port number or service name */ struct file { char*f_name;/* filename */ char*f_host;/* hostname or IPv4/IPv6 address */ int f_fd;/* descriptor */ int f_flags;/* F_xxx below */ } file[MAXFILES]; #defineF_CONNECTING1/* connect() in progress */ #defineF_READING2/* connect() complete; now reading */ #defineF_DONE4/* all done */ #defineGET_CMD"GET %s HTTP/1.0\r\n\r\n" /* globals */ intnconn, nfiles, nlefttoconn, nlefttoread, maxfd; fd_setrset, wset; /* function prototypes */ voidhome_page(const char *, const char *); voidstart_connect(struct file *); voidwrite_get_cmd(struct file *);

UNIX Network Programming23 Figure First part of stimultaneous connect int main(int argc, char **argv) { inti, fd, n, maxnconn, flags, error; charbuf[MAXLINE]; fd_setrs, ws; if (argc < 5) err_quit("usage: web..."); maxnconn = atoi(argv[1]); nfiles = min(argc - 4, MAXFILES); for (i = 0; i < nfiles; i++) { file[i].f_name = argv[i + 4]; file[i].f_host = argv[2]; file[i].f_flags = 0; } printf("nfiles = %d\n", nfiles); home_page(argv[2], argv[3]); FD_ZERO(&rset); FD_ZERO(&wset); maxfd = -1; nlefttoread = nlefttoconn = nfiles; nconn = 0;

UNIX Network Programming24 Figure home_page function. #include"web.h" void home_page(const char *host, const char *fname) { intfd, n; charline[MAXLINE]; fd = Tcp_connect(host, SERV);/* blocking connect() */ n = snprintf(line, sizeof(line), GET_CMD, fname); Writen(fd, line, n); for ( ; ; ) { if ( (n = Read(fd, line, MAXLINE)) == 0) break;/* server closed connection */ printf("read %d bytes of home page\n", n); /* do whatever with data */ } printf("end-of-file on home page\n"); Close(fd); }

UNIX Network Programming25 Figure Initiate nonblocking connect void start_connect(struct file *fptr) { intfd, flags, n; struct addrinfo*ai; ai = Host_serv(fptr->f_host, SERV, 0, SOCK_STREAM); fd = Socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); fptr->f_fd = fd; printf("start_connect for %s, fd %d\n", fptr->f_name, fd); /* 4Set socket nonblocking */ flags = Fcntl(fd, F_GETFL, 0); Fcntl(fd, F_SETFL, flags | O_NONBLOCK); /* Initiate nonblocking connect to the server. */ if ( (n = connect(fd, ai->ai_addr, ai->ai_addrlen)) < 0) { if (errno != EINPROGRESS) err_sys("nonblocking connect error"); fptr->f_flags = F_CONNECTING; FD_SET(fd, &rset);/* select for reading and writing */ FD_SET(fd, &wset); if (fd > maxfd) maxfd = fd; } else if (n >= 0)/* connect is already done */ write_get_cmd(fptr);/* write() the GET command */ }

UNIX Network Programming26 Figure Send an HTTP Get command to the server #include"web.h" void write_get_cmd(struct file *fptr) { intn; charline[MAXLINE]; n = snprintf(line, sizeof(line), GET_CMD, fptr->f_name); Writen(fptr->f_fd, line, n); printf("wrote %d bytes for %s\n", n, fptr->f_name); fptr->f_flags = F_READING;/* clears F_CONNECTING */ FD_SET(fptr->f_fd, &rset);/* will read server's reply */ if (fptr->f_fd > maxfd) maxfd = fptr->f_fd; }

UNIX Network Programming27 Figure Main loop of main function ( 1/3 ) while (nlefttoread > 0) { while (nconn 0) { /* 4find a file to read */ for (i = 0 ; i < nfiles; i++) if (file[i].f_flags == 0) break; if (i == nfiles) err_quit("nlefttoconn = %d but nothing found", nlefttoconn); start_connect(&file[i]); nconn++; nlefttoconn--; } rs = rset; ws = wset; n = Select(maxfd+1, &rs, &ws, NULL, NULL);

UNIX Network Programming28 Figure Main loop of main function ( 2/3 ) for (i = 0; i < nfiles; i++) { flags = file[i].f_flags; if (flags == 0 || flags & F_DONE) continue; fd = file[i].f_fd; if (flags & F_CONNECTING && (FD_ISSET(fd, &rs) || FD_ISSET(fd, &ws))) { n = sizeof(error); if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &n) < 0 || error != 0) { err_ret("nonblocking connect failed for %s", file[i].f_name); } /* 4connection established */ printf("connection established for %s\n", file[i].f_name); FD_CLR(fd, &wset);/* no more writeability test */ write_get_cmd(&file[i]);/* write() the GET command */

UNIX Network Programming29 Figure Main loop of main function ( 3/3 ) } else if (flags & F_READING && FD_ISSET(fd, &rs)) { if ( (n = Read(fd, buf, sizeof(buf))) == 0) { printf("end-of-file on %s\n", file[i].f_name); Close(fd); file[i].f_flags = F_DONE;/* clears F_READING */ FD_CLR(fd, &rset); nconn--; nlefttoread--; } else { printf("read %d bytes from %s\n", n, file[i].f_name); } exit(0); }

UNIX Network Programming Nonblocking accept int main(int argc, char **argv) { intsockfd; struct lingerling; struct sockaddr_inservaddr; if (argc != 2) err_quit("usage: tcpcli "); sockfd = Socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(SERV_PORT); Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); Connect(sockfd, (SA *) &servaddr, sizeof(servaddr)); ling.l_onoff = 1;/* cause RST to be sent on close() */ ling.l_linger = 0; Setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling)); Close(sockfd); exit(0); }

UNIX Network Programming31 TCP server If (FD_ISSET(listenfd, &rset)) { printf(“listening socket readable \n”); sleep(5); clilen = sizeof(cliaddr); connfd = Accept(listenfd, (SA *) &cliaddr, &clilen); }