CSCE 515: Computer Network Programming ------ Select Wenyuan Xu Department of Computer Science and Engineering.

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.
Sop, Fan Reference: Daniel Spangenberger Computer Networks PPT-4 Socket Programming.
I/O Models Satish Krishnan. I/O Models Blocking I/O Non-blocking I/O I/O Multiplexing Signal driven I/O Asynchronous I/O.
I/O Multiplexing: select and poll
Chapter 6 I/O Multiplexing: select and poll function.
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.
Today’s topic Issues about sending structures with TCP. Server design alternatives: concurrent server and multiplexed server. I/O multiplexing.
CSCE 515: Computer Network Programming TCP Details Wenyuan Xu Department of Computer Science and Engineering.
Lecture 12 Overview. UDP Connected mode A UDP socket can be used in a call to connect() This simply tells the O.S. the address of the peer No handshake.
Lecture 16 Overview. Creating a TCP socket int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); int mysock; struct sockaddr_in myaddr;
Computer Networks Sockets. Sockets and the OS F An end-point for Internet connection –What the application “plugs into” –OS provides Application Programming.
TDC561 Network Programming Camelia Zlatea, PhD Week 3: Unix Asynchronous Events; Signals and Alarms API.
CS4514-Help Session By Huahui Wu. Description In this project, you are supposed to implement a PAR (Positive Acknowledgement with Retransmission)
I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
1 Data Communications and Networking Socket Programming Part II: Design of Server Software Reference: Internetworking with TCP/IP, Volume III Client-Server.
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.
CS4514 Assignment 2 Help Session – Data Link Layer Client and Server Processes Speaker: Hao Shang Date: Nov. 11th, 2004.
CSCE 515: Computer Network Programming TCP Details Wenyuan Xu Department of Computer Science and Engineering.
Computer Networks Sockets. Outline F Socket basics F Socket details.
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.
IP Multiplexing Ying Zhang EECS 489 W07.
CS4516: Medical Examiner Client/Server Evan Frenn 1.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
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.
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)
Signal-Driven I/O Concepts and steps for using signal-driven I/O
Socket Models Different ways to manage your connections.
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.
Elementary TCP Sockets –The presentation will provide sufficient information to build a COMPLETE TCP client and server. –In addition the topic of concurrency.
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.
Chapter18 broadcasting. contents Introduction broadcast address unicast versus broadcast dg_cli function using broadcasting Race conditions.
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.
I/O Multiplexing. TCP Echo Client: I/O operation is sequential !! tcpcliserv/tcpcli01.c: lib/str_cli.c: TCP Client TCP Server stdin stdout fgets fputs.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
I/O Multiplexing. What is I/O multiplexing? When an application needs to handle multiple I/O descriptors at the same time –E.g. file and socket descriptors,
Single Process, Concurrent, Connection-Oriented Servers (TCP) (Chapter 12)
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
CMPT 471 Networking II Network Programming © Janice Regan,
UNIX Network Programming1 Chapter 13. Advanced I / O Functions.
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
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.
I/O Multiplexing Chap 6. I/O Models  Blocking I/O Model  Nonblocking I/O Model  I/O Multiplexing Model  Signal Driven I/O Model  Asynchronous I/O.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
I/O Multiplexing.
Chapter 5. TCP Client-Server Example
Elementary UDP Sockets
Pertemuan 7 I/O Multiplexing
Review: TCP Client-Server Interaction
Lecture 4 Socket Programming Issues
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
UDP Sockets Programming
Lecture 11 Overview.
Lecture 17 Overview.
Socket Programming.
TCP/IP Socket Programming in C
Elementary UDP Sockets connectionless, unreliable, datagram
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:

CSCE 515: Computer Network Programming Select Wenyuan Xu Department of Computer Science and Engineering University of South Carolina

CSCE515 – Computer Network Programming 2007 fgets TCP Client/Server Example TCP client TCP server writenread readlinewritenfputs stdin stdout

CSCE515 – Computer Network Programming 2007 Echo client int sockfd; struct sockaddr_in server; socket = Socket(AF_INET, SCOK_STREAM, 0); server.sin_family = AF_INET; server.sin_port = htons(SERV_PORT); Inet_pton(AF_INET, argv[1], &server.sin_addr); Connect(sockfd,(sockaddr *)&server,sizeof(servaddr)); str_cli(stdin, sockfd); exit(0);

CSCE515 – Computer Network Programming 2007 Echo client (cont.) str_cli(FILE *fp, int sockfd) { char sendline[MAXLINE], recvline[MAXLINE]; while (Fgets(sendline, MACLINE,fp) != NULL) { Writen(sockfd, sendline, strlen(sendline)); if (Readline(sockfd, recvline, MAXLINE)) == 0 err_quit(“str_cli: server terminated prematurely”); Fputs(recvline, stdout); }

CSCE515 – Computer Network Programming 2007 ServerClient FIN SN=X FIN SN=X 1 ACK=X+1 2 RST 4 Data 3... “I have no more data for you” FIN_WAIT_1 “ OK, I understand you are done sending.” CLOSE_WAIT “OK – one more line”. “I’ve quit already!” TCP Termination FIN_WAIT_2 “oops, something wrong”

CSCE515 – Computer Network Programming 2007 Problem Server sends FIN Client TCP responds with ACK After that:  Server: FIN_WAIT2  Client: CLOSE_WAIT The client process is blocked in fgets when FIN arrives on the socket The client is working with two descriptor, while it should not block on one of them:  Socket  User input

CSCE515 – Computer Network Programming 2007 I/O Multiplexing We often need to be able to monitor multiple descriptors:  a generic TCP client (like telnet)  A server that handles both TCP and UDP  Client that can make multiple concurrent requests (browser?).

CSCE515 – Computer Network Programming 2007 Example - generic TCP client Input from standard input should be sent to a TCP socket. Input from a TCP socket should be sent to standard output. How do we know when to check for input from each source?

CSCE515 – Computer Network Programming 2007 Generic TCP Client STDIN STDOUT TCP SOCKET

CSCE515 – Computer Network Programming 2007 Options Use nonblocking I/O.  use fcntl() to set O_NONBLOCK Use alarm and signal handler to interrupt slow system calls. Use multiple processes/threads. Use functions that support checking of multiple input sources at the same time.

CSCE515 – Computer Network Programming 2007 Non blocking I/O use fcntl() to set O_NONBLOCK : int flags; flags = fcntl(sock,F_GETFL,0); fcntl(sock,F_SETFL,flags | O_NONBLOCK); Now calls to read() (and other system calls) will return an error and set errno to EWOULDBLOCK.

CSCE515 – Computer Network Programming 2007 while (! done) { if ( (n=read(STDIN_FILENO,…)<0)) if (errno != EWOULDBLOCK) /* ERROR */ else write(tcpsock,…) if ( (n=read(tcpsock,…)<0)) if (errno != EWOULDBLOCK) /* ERROR */ else write(STDOUT_FILENO,…) }

CSCE515 – Computer Network Programming 2007 The problem with nonblocking I/O Using blocking I/O allows the Operating System to put your process to sleep when nothing is happening (no input). Once input arrives, the OS will wake up your process and read() (or whatever) will return. With nonblocking I/O, the process will chew up all available processor time!!!

CSCE515 – Computer Network Programming 2007 Using alarms signal(SIGALRM, sig_alrm); alarm(MAX_TIME); read(STDIN_FILENO,…);... signal(SIGALRM, sig_alrm); alarm(MAX_TIME); read(tcpsock,…);... A function you write

CSCE515 – Computer Network Programming 2007 Alarming Problem What will happen to the response time ? What is the ‘right’ value for MAX_TIME?

CSCE515 – Computer Network Programming 2007 Select() The select() system call allows us to use blocking I/O on a set of descriptors (file, socket, …). For example, we can ask select to notify us when data is available for reading on either STDIN or a TCP socket.

CSCE515 – Computer Network Programming 2007 Select() Return when  Any of the descriptors in the set {1,4,5} are ready for reading  Any of the descriptors in the set {2,7} are ready for writing  Any of the descriptors in the set {1,4} have an exception condition pending Specify what descriptors we are interested in and how long to wait

CSCE515 – Computer Network Programming 2007 select() int select( int maxfd, fd_set *readset, fd_set *writeset, fd_set *excepset, const struct timeval *timeout); maxfd : highest number assigned to a descriptor. readset : set of descriptors we want to read from. writeset : set of descriptors we want to write to. excepset : set of descriptors to watch for exceptions. timeout : maximum time select should wait

CSCE515 – Computer Network Programming 2007 struct timeval struct timeval { long tv_sec;/* seconds */ long tv_usec;/* microseconds */ } struct timeval max = {1,0}; struct timeval forever = NULL; struct timeval polling = {0,0}

CSCE515 – Computer Network Programming 2007 fd_set Implementation is not important Operations you can use with an fd_set : void FD_ZERO( fd_set *fdset); void FD_SET( int fd, fd_set *fdset); void FD_CLR( int fd, fd_set *fdset); int FD_ISSET( int fd, fd_set *fdset);

CSCE515 – Computer Network Programming 2007 Using select() Create fd_set Clear the whole thing with FD_ZERO Add each descriptor you want to watch using FD_SET. Call select when select returns, use FD_ISSET to see if I/O is possible on each descriptor.