Recitation 9 Greg Reshko

Slides:



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

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.
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
1 Carnegie Mellon Proxylab and stuff : Introduction to Computer Systems Recitation 13: November 19, 2012 Donald Huang (donaldh) Section M.
Today’s topic Issues about sending structures with TCP. Server design alternatives: concurrent server and multiplexed server. I/O multiplexing.
Ways to read data from disk to memory Tan Li. read, write read, write -- low level file access, it's an operation between two file discriptors. SYNOPSIS.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
Sockets COS 518: Advanced Computer Systems Michael Freedman Fall
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Recitation 11: I/O Problems Andrew Faulring Section A 18 November 2002.
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.
Assignment 3 A Client/Server Application: Chatroom.
Elementary TCP Sockets
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
University of Amsterdam Computer Systems – Iterative server Arnoud Visser 1 Computer Systems Iterative server.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
Operating Systems Recitation 9, May 19-20, Iterative server Handle one connection request at a time. Connection requests stored in queue associated.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
 Wind River Systems, Inc Chapter - 13 Network Programming.
Recitation 11: 11/18/02 Outline Robust I/O Chapter 11 Practice Problems Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean.
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
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.
Remote Shell CS230 Project #4 Assigned : Due date :
UNIX Files File organization and a few primitives.
1 COMP445 Fall 2006 Lab assignment 1. 2 STEP1: Get the name of the second party STEP2: Get phone number from white pages CALLERRECEIVER STEP1: Plug the.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
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.
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
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.
Review: – Why layer architecture? – peer entities – Protocol and service interface – Connection-oriented/connectionless service – Reliable/unreliable service.
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
Recitation 9: 11/04/02 Outline Error Handling I/O Linux man pages Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean 8402.
Carnegie Mellon Proxy & Networking : Introduction to Computer Systems – Recitation H April 11, 2011.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Netprog: Client/Server Issues1 Issues in Client/Server Programming Refs: Chapter 27.
OS interface: file and I/O system calls File operations in C/C++? –fopen(), fread(), fwrite(), fclose(), fseek() in C f.open(…), f.close(…) in C++ I/O.
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.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Error handling I/O Man pages
Assignment 3 A Client/Server Application: Chatroom
Robust I/O package Chapter 11 practice problems
Extending echo server HTTP Broken pipe error Feedback and evaluation
Socket Programming in C
Review: TCP Client-Server Interaction
Tutorial on Socket Programming
Recitation 11 – 4/29/01 Outline Sockets Interface
UDP Sockets Programming
Socket Programming in C
Lecture 11 Overview.
TCP Sockets Programming
Advanced Network Programming spring 2007
CSE 333 – Section 3 POSIX I/O Functions.
Issues in Client/Server Programming
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
CSE 333 – Section 3 POSIX I/O Functions.
Presentation transcript:

15-213 Recitation 9 Greg Reshko Office Hours: Wed 2:00-3:00PM April 21st, 2003

Outline File I/O Networking Lab 7

Unix File I/O All I/O devices are modeled as files Networks, disks, terminals, etc All input and output is performed by reading and writing the appropriate files Kernel maintains all information about files and just returns a file descriptor to an application that wants access to a given file

Unix File I/O: Read/Write #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, const void *buf, size_t count);

Unix File I/O: More Robust Robust I/O (RIO) Package: Unbuffered: ssize_t readn(int fd, void *buf, size_t count); ssize_t writen(int fd, const void *buf, size_t count); ssize_t readline(int fd, void *buf, size_t maxlen); Buffered (thread-safe): ssize_t readnb(int fd, void *buf, size_t count); ssize_t writenb(int fd, const void *buf, size_t count); ssize_t readlineb(int fd, void *buf, size_t maxlen);

Standard I/O vs. Unix I/O Standard I/O almost always preferred fopen, fclose, fseek, fread, fwrite, fscanf, fprintf, etc. A higher-level abstraction than UNIX I/O Models files as streams, like file descriptors in being full-duplex Restrictions exist that interact badly with restrictions on sockets Input function cannot follow an output function (or vice versa) without a call to fflush, fseek, fsetpos, rewind, or and end-of-file

Networking Two sides to networking Client / Server Distinction is fading Peer-to-peer systems mean a node is both

Client Code Path Clients typically connect to servers, perform a task, then close the connection Create a socket() Connect() Write()/read() Close()

Server Code Path Servers wait for connections and handle them as they arrive Create a socket() Bind() to a port Listen() for connections Select() a ready connection Accept() a connection Read()/Write() Close() the connection We’ll focus on TCP, but UDP is just as important

Server: Init void CreateTCPServer() { struct sockaddr_in myAddr; int myAddrLen; int reuseAddrFlg=1; tcpSocketfd = socket(AF_INET,SOCK_STREAM,0); bzero((void *)(&myAddr),sizeof(myAddr)); myAddr.sin_family = AF_INET; myAddr.sin_port = htons((unsigned short)atoi(chatTCPServerPort)); myAddr.sin_addr.s_addr = htonl(INADDR_ANY); setsockopt(tcpSocketfd,SOL_SOCKET,SO_REUSEADDR,(void *) &reuseAddrFlg,sizeof(reuseAddrFlg)); bind(tcpSocketfd,(struct sockaddr *)&myAddr,sizeof(myAddr)); listen(tcpSocketfd,MAX_BACKLOG); }

Server: Loop while(1) { readSet=allSet; numReadyDescriptors=select(maxfd+1,&readSet,NULL,NULL,NULL); if(FD_ISSET(tcpSocketfd,&readSet)) /* Incoming request for new connection */ struct sockaddr_in clientAddr; int clientAddrLen; slavefd=accept(tcpSocketfd,(struct sockaddr *) &clientAddr, &clientAddrLen); /* Save slavefd so we know to check it later */ FD_SET(slavefd,&allSet); if(slavefd > maxfd) maxfd=slavefd; fflush(stdout); numReadyDescriptors--; if(numReadyDescriptors <= 0) continue; }

Server: The Rest Check whether interesting socket descriptors are in the read/write set Do appropriate action if they are (i.e., read() or write()) Close() the connection when appropriate

Logging and Network I/O Have a server set up to receive requests and respond to them Read in request, parse, and return response Need to read/write to a network stream, and log events to a file for record-keeping purposes Main loop opens file for logging and spawns children threads Each thread has own network streams, but shares logging file stream Use of mutex to lock resources glossed over for now

Reading to/from Network & File void main () { FILE* log; /* open file */ log = fopen ( "log.txt“, "a“ ); while ( true ) /* * Main Loop (inside thread) */ } fclose ( log );

Reading to/from Network & File void *thread ( void *vargp ) { FILE* stream; char method[BUFSIZE], uri[BUFSIZE], version[BUFSIZE]; /* create mutex */ /* given argument, set file descriptor for proper access to streams */ childfd = *((int *)vargp); free(vargp); /* open file descriptors for reading network socket, set buffer type */ if ((stream = fdopen(childfd, "r+")) == NULL) error("ERROR on fdopen"); setvbuf(stream, NULL, _IONBF, 0); /* get header off the stream and save */ fgets(buf, BUFSIZE, stream); sscanf(buf, "%s %s %s\n", method, uri, version);

Reading to/from Network & File /* * Parse, error check, ready for server communication. */ * determine size of information to be written back * in this case, copying from some other stream (buf) * to the network stream (stream) /* get size of remaining buffer from stream */ temp = read ( serverfd, buf, BUFSIZE ); while ( temp > 0 ) { data.byte += temp; * write current buffered amount to network socket * get size of remaining buffer from stream fwrite ( buf, sizeof ( char ), temp, stream ); }

Reading to/from Network & File /** init mutex **/ /** lock mutex **/ /* write to log file */ fprintf(log, "%s %s %s %d\n", data.date, data.ip, data.address, data.byte); /** unlock mutex **/ /* close network buffer */ fclose(stream); }

Lab 7 – Logging Web Proxy Start early (yeah, that’s a new one…) Read the book carefully Everything you need for this lab is in the book – make sure you understand it Concurrency issues Test it with your web browser i.e. Internet Explorer This is the coolest part of the lab