UNIX Network Programming1 Chapter 13. Advanced I / O Functions.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

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.
TELE 402 Lecture 11: Advanced UDP… 1 by Dr Z. Huang Overview Last Lecture –Nonblocking I/O and ioctl operations –Source: Chapter 16 & 17 of Stevens’ book.
I/O Multiplexing Road Map: 1. Motivation 2. Description of I/O multiplexing 3. Scenarios to use I/O multiplexing 4. I/O Models  Blocking I/O  Non-blocking.
Lecture 16 Overview. Creating a TCP socket int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); int mysock; struct sockaddr_in myaddr;
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.
Sockets Basics Conectionless Protocol. Today IPC Sockets Basic functions Handed code Q & A.
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.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
1 Advanced I/O Computer Network Programming. 2 Regarding Project1 –Use RCS to store different revisions of your programs –this is a requirement! –you.
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.
Additional API functions and data-structures A look at how some information from lower-layers of the TCP/IP protocol stack could be accessed.
1) The server should be concurrent. This implies that it should loop infinitely, listening for clients requests. It should NOT terminate after accepting.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Computer Network Architecture and Programming
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.
Broadcast and Multicast. Unicast Host 2Host 1 Broadcast Packet received by every host on network (including the sender!)
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
1 Introduction to Raw Sockets 2 IP address Port address MAC address TCP/IP Stack 67 Bootp DHCP OSPF protocol frame type UDP Port # TCP Port.
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)
More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c.
Nonblocking I/O Blocking vs. non-blocking I/O Nonblocking input, output, accept, and connect Readings –UNP Ch16 1.
UNIX Network Programming1 Chapter 15. Nonblocking I/O.
Review: How to create a TCP end point? What is the right format for sin_port and sin_addr in the sockaddr_in data structure? How many different ways we.
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.
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.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Advanced Sockets API-II Vinayak Jagtap
Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol ,
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
TELE 402 Lecture 9: Daemon … 1 by Dr Z. Huang Overview Last Lecture –Broadcast and multicast This Lecture –Daemon processes and advanced I/O functions.
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.
Today’s topic: UDP Reliable communication over UDP.
Review: –Concurrent server and multiplexed server How they work? Which one is better?
Sockets Introduction Socket address structures Value-result arguments
CMPT 471 Networking II Network Programming © Janice Regan,
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
CSCI 330 UNIX and Network Programming Unit XVII: Socket Programming Detail.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
1 UDP Sockets Programming Creating UDP sockets.Creating UDP sockets. –Client –Server Sending data.Sending data. Receiving data.Receiving data. Connected.
I/O Multiplexing.
Socket Option.
Chapter 5. TCP Client-Server Example
Elementary UDP Sockets
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.
UDP Sockets Programming
Lecture 11 Overview.
Advanced Network Programming spring 2007
Elementary UDP Sockets connectionless, unreliable, datagram
Advanced I/O Functions
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,
Socket Programming with UDP
Presentation transcript:

UNIX Network Programming1 Chapter 13. Advanced I / O Functions

UNIX Network Programming Introduction Socket Timeouts recv and send Functions readv and writev Functions recvmsg and sendmsg Function Ancillary Data How much Data is Queued? Sockets and Standard I/O T/TCP

UNIX Network Programming Socket Timeouts Three ways to place a timeout on an I/O operation involving a socket –Call alarm, which generates the SIGALRM signal when the specified time has expired. –Block waiting for I/O in select, which has a time limit built in, instead of blocking in a call to read or write. –Use the newer SO_RCVTIMEO and SO_SNDTIMEO socket options.

UNIX Network Programming4 Connect with a Timeout Using SIGALRM (figure 13.1) #include"unp.h" static voidconnect_alarm(int); int connect_timeo(int sockfd, const SA *saptr, socklen_t salen, int nsec) { Sigfunc*sigfunc; intn; sigfunc = Signal(SIGALRM, connect_alarm); if (alarm(nsec) != 0) err_msg("connect_timeo: alarm was already set"); if ( (n = connect(sockfd, (struct sockaddr *) saptr, salen)) < 0) { close(sockfd); if (errno == EINTR) errno = ETIMEDOUT; } alarm(0);/* turn off the alarm */ Signal(SIGALRM, sigfunc);/* restore previous signal handler */ return(n); } static void connect_alarm(int signo) { return;/* just interrupt the connect() */ }

UNIX Network Programming5 recvfrom with a Timeout Using SIGALRM (figure 13.2) #include"unp.h" static voidsig_alrm(int); void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen) { intn; charsendline[MAXLINE], recvline[MAXLINE + 1]; Signal(SIGALRM, sig_alrm); while (Fgets(sendline, MAXLINE, fp) != NULL) { Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen); alarm(5); if ( (n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL)) < 0) { if (errno == EINTR) fprintf(stderr, "socket timeout\n"); else err_sys("recvfrom error"); } else { alarm(0); recvline[n] = 0;/* null terminate */ Fputs(recvline, stdout); } static void sig_alrm(int signo) { return;/* just interrupt the recvfrom() */ }

UNIX Network Programming6 recvfrom with a Timeout Using select (figure 13.3) #include"unp.h" int readable_timeo(int fd, int sec) { fd_setrset; struct timevaltv; FD_ZERO(&rset); FD_SET(fd, &rset); tv.tv_sec = sec; tv.tv_usec = 0; return(select(fd+1, &rset, NULL, NULL, &tv)); /* 4> 0 if descriptor is readable */ }

UNIX Network Programming7 recvfrom with a Timeout Using the SO_RCVTIMEO Socket Option (figure 13.5) void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen) { intn; charsendline[MAXLINE], recvline[MAXLINE + 1]; struct timevaltv; tv.tv_sec = 5; tv.tv_usec = 0; Setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); while (Fgets(sendline, MAXLINE, fp) != NULL) { Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen); n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL); if (n < 0) { if (errno == EWOULDBLOCK) { fprintf(stderr, "socket timeout\n"); continue; } else err_sys("recvfrom error"); } recvline[n] = 0;/* null terminate */ Fputs(recvline, stdout); }

UNIX Network Programming recv and send Functions #include ssize_t recv (int sockfd, void *buff, size_t nbytes, int flags); ssize_t send (int sockfd, const void *buff, size_t nbytes, int flags);

UNIX Network Programming readv and writev Functions –readv and writev let us read into or write from one or more buffers with a single function call. are called scatter read and gather write. #include ssize_t readv (int filedes, const struct iovec *iov, int iovcnt); ssize_t writev (int filedes, const struct iovec *iov, int iovcnt); Struct iovec { void*iov_base; /* starting address of buffer */ size_tiov_len; /* size of buffer */ };

UNIX Network Programming recvmsg and sendmsg Functions #include ssize_t recvmsg (int sockfd, struct msghdr *msg, int flags); ssize_t sendmsg (int sockfd, struct msghdr *msg, int flags); Struct msghdr { void *msg_name; /* starting address of buffer */ socklen_tmsg_namelen; /* size of protocol address */ struct iovec*msg_iov; /* scatter/gather array */ size_tmsg_iovlen; /* # elements in msg_iov */ void*msg_control; /* ancillary data; must be aligned for a cmsghdr structure */ socklen_tmsg_controllen; /* length of ancillary data */ intmsg_flags; /* flags returned by recvmsg() */ };

UNIX Network Programming recvmsg and sendmsg Functions (cont.)

UNIX Network Programming recvmsg and sendmsg Functions (cont.)

UNIX Network Programming recvmsg and sendmsg Functions (cont.)

UNIX Network Programming Ancillary Data Ancillary data can be sent and received using the msg_control and msg_controllen members of the msghdr structure with sendmsg and recvmsg functions.

UNIX Network Programming Ancillary Data (cont.)

UNIX Network Programming Ancillary Data (cont.)

UNIX Network Programming How Much Data Is Queued? Three techniques - page 365.

UNIX Network Programming Sockets and Standard I/O The standard I/O stream can be used with sockets, but there are a few items to consider. –A standard I/O stream can be created from any desciptor by calling the fdopen function. Similarly, given a standard I/O stream, we can obtain the corresponding descriptor by calling fileno. –fseek, fsetpos, rewind functions is that they all call lseek, which fails on a socket. –The easiest way to handle this read-write problem is to open two standard I/O streams for a given socket: one for reading, and one for writing.

UNIX Network Programming Sockets and Standard I/O Example : str_echo Function using standard I/O #include"unp.h" void str_echo(int sockfd) { charline[MAXLINE]; FILE*fpin, *fpout; fpin = Fdopen(sockfd, "r"); fpout = Fdopen(sockfd, "w"); for ( ; ; ) { if (Fgets(line, MAXLINE, fpin) == NULL) return; /* connection closed by other end */ Fputs(line, fpout); }

UNIX Network Programming T/TCP: TCP for Transactions T/TCP is a slight modification to TCP that can avoid the three- way handshake between hosts that have communicated with each other recently. Benefit –all the reliability of TCP is retained –maintains TCP’s slow start and congestion avoidance, features that are often missing from UDP applications.

UNIX Network Programming T/TCP: TCP for Transactions (cont.)