Sockets, part 2 (Addresses, poll())

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
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.
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.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
CS3771 Today: network programming with sockets  Previous class: network structures, protocols  Next: network programming Sockets (low-level API) TODAY!
1 Data Communications and Networking Socket Programming Part II: Design of Server Software Reference: Internetworking with TCP/IP, Volume III Client-Server.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
An Introduction to Internetworking. Algorithm for client-server communication with UDP (connectionless) A SERVER A CLIENT Create a server-socket (listener)and.
Process-to-Process Delivery:
University of Calgary – CPSC 441.  UDP stands for User Datagram Protocol.  A protocol for the Transport Layer in the protocol Stack.  Alternative to.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
6.1. Transport Control Protocol (TCP) It is the most widely used transport protocol in the world. Provides reliable end to end connection between two hosts.
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.
Internet Applications and Network Programming Dr. Abraham Professor UTPA.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
Socket programming. Sockets are a protocol independent method of creating a connection between processes. Sockets can be either: connection based or connectionless:
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.
Chapter 2 Applications and Layered Architectures Sockets.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
Network Programming Eddie Aronovich mail:
Distributed Computing Systems Project 3 – Nutella: A P2P Streaming Movie System Due: Tuesday, April 15 th.
1 COMP/ELEC 429/556 Introduction to Computer Networks Creating a Network Application Some slides used with permissions from Edward W. Knightly, T. S. Eugene.
Advanced Sockets API-II Vinayak Jagtap
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
Socket Programming Lab 1 1CS Computer Networks.
The Client-Server Model And the Socket API. Client-Server (1) The datagram service does not require cooperation between the peer applications but such.
Today’s topic: UDP Reliable communication over UDP.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
@Yuan Xue CS 283Computer Networks Spring 2013 Instructor: Yuan Xue.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
SOCKET PROGRAMMING Presented By : Divya Sharma.
More Project 1 and HW 1 stuff! Athula Balachandran Wolfgang Richter
Object-Orientated Analysis, Design and Programming
Assignment 3 A Client/Server Application: Chatroom
Echo Networking COMP
CSCE 313 Network Socket MP8 DUE: FRI MAY 5, 2017
UNIX Sockets COS 461 Precept 1.
Beyond HTTP Up to this point we have been dealing with software tools that run on browsers and communicate to a server that generates files that can be.
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Review: TCP Client-Server Interaction
Imam Ahmad Trinugroho, ST., MMSI
Java Byte IPC: Part 6-Summary
Transport layer API: Socket Programming
Sockets, part 3 (nonblocking tcp using streams)
CSE 451: Operating Systems Winter 2006 Module 20 Remote Procedure Call (RPC) Ed Lazowska Allen Center
Network Programming CSC- 341
Process-to-Process Delivery:
CSE 451: Operating Systems Winter 2007 Module 20 Remote Procedure Call (RPC) Ed Lazowska Allen Center
Starting TCP Connection – A High Level View
Internet Applications and Network Programming
CSE 451: Operating Systems Winter 2004 Module 19 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CSE 451: Operating Systems Spring 2012 Module 22 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CSE 451: Operating Systems Autumn 2009 Module 21 Remote Procedure Call (RPC) Ed Lazowska Allen Center
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
An Introduction to Internetworking
CSE 451: Operating Systems Autumn 2010 Module 21 Remote Procedure Call (RPC) Ed Lazowska Allen Center
Internet Networking recitation #8
in unistd.h: int gethostname(char * name, int maxlen)
Process-to-Process Delivery: UDP, TCP
Socket Programming with UDP
CSE 451: Operating Systems Messaging and Remote Procedure Call (RPC)
Presentation transcript:

Sockets, part 2 (Addresses, poll()) Eric Freudenthal

Problem: adding network to programming model What’s needed? Way to: Represent stream connections (read, write) Like files, are just an ordered sequence of bytes Lots of libraries & programmers already know files Try to emulate files as much as possible Represent stream listeners (accept) Accept connections Factory for stream connections Portals for datagrams (sendto, recvfrom) Permit sending and receiving of datagrams Same for many stream and datagram protocols Make as universal as possible

Specifying addresses Servers need to specify addresses So clients can contact them Clients may not need this (unless p2p) Hosts may have multiple addresses Multiple interfaces If appropriate, socket should be able to communicate to only one, or many Protocol may implement “ports” at an address Big idea: “universal” address specifier Contains protocol, address, (and if appropriate) port identifier

Making addresses generic Low level Easy in OO languages Messy in C Define multiple struct types With identical prolog containing discriminator word Reference using pointers, let socket figure things out Higher level: using strings 10.1.1.15 or cs.utep.edu Nicely human readable Nuisance methods convert strings to generic addr struct Described in detail within Donohoo book

Stream socket operations Stream listener (server) sockets: Bind(fd, ...) to address Accept(fd, …) client connections (returns stream) Connected stream socket knows address of client Stream client sockets Optional: bind(fd, …) to address Connect(fd, …) to listener (specify server address) Once connected: connected stream support Read(fd, …) & write(fd, …) data; close(fd) connection Can receive enqueued data if other end already closed Request addr of other end

Datagram socket operations Bind(fd, …) to address May only be done for server Sendto(fd, buffer, address) Recvfrom(fd, buffer, address) Close(fd)

Dealing with asynchrony Problem: waiting for data That may arrive from multiple sockets that may not arrive (in time) Classic (inefficient and messy) approach Use threads: One thread waits for data on each input socket Other threads worry about data not arriving “in time” Getting the interactions right is messy and error-prone Rarely is any benefit derived from apparent parallelism Modern approach: select(), poll(), and epoll() Non-blocking socket requests Operations never block – either work or fail immediately OS told all requirements Program tells OS which socket states matter & timeout Lets program know when socket is ready or if timeout occurred first We will examine & use poll

Big idea 1: classify socket events Types of socket events Rec’d data available for reading (POLLIN) Also accept for listener sockets Ready to accept data for sending (POLLOUT) Note datagram sockets always ready for this! No more data will be rec’d (POLLRDHUP) Closed (POLLHUP) Error (POLLERR) Invalid request (e.g. fd closed: POLLNVAL) More, but esotheric for us, see: man 2 poll Each specified by a different bit

Big idea 2: pollfd vector Role: represent sockets & needed events struct pollfd { int fd; /* file descriptor */ short events; /* requested events */ short revents; /* returned events */ }; Struct #define FdLimit 32 // hack struct pollfd pollvec[FdLimit];

Selecting next action based upon poll system call int poll(struct pollfd *pollFds, nfds_t nfds, int timeout); Parameters: pollFds: vector of pollfds (each contains fd, request, return) Nfds: # of entries in pollfds[] (length of array) Timeout: in milliseconds Upon return Return entries in pollFds are set Return value = #events satisfied Zero for timeout Or negative on failure (see errno)

Lab 2 Create a reliable file-transfer system using udp Server: Client: awaits and responds to requests Obtains file from filesystem Sends data reliably to client Client: User specifies requested filename & name to store File obtained using UDP and stored in filesystem Ideally protocol and implementation robust to message loss You should hand in a tarfile containing only: Source files, Makefile, and readme.txt

Rest of the slides are on epoll Epoll is not so widely available, but is likely to eventually replace poll. Feel free to use epoll if your system supports it.

Big idea 1: classify socket events Types of socket events Rec’d data available for reading (EPOLLIN) Ready to accept data for sending (EPOLLOUT) Note datagram sockets always ready for this! No more data will be rec’d (EPOLLRDHUP) Closed (EPOLLHUP) Error (EPOLLERR) More, but esotheric for us, see: man 2 epoll_ctl Each specified by a different bit

Big idea 2: event descriptor Role: register sockets & needed events Epoll_create(size): see man 2 epoll_create Size: hint indicating how many sockets to manage Returns: epfd, which serves as an event descriptor Epoll_set(epfd, op, fd, epoll_event) Epfd: epoll event descripter Fd: fd of interest Op: EPOLL_CTL_ADD, _MOD, or _DEL Event: from previous slide, and extra integer See: man 2 epoll_create

Details of an epoll_event typedef union epoll_data { // optional void *ptr; int fd; uint32_t u32; uint64_t u64; }epoll_data_t; struct epoll_event { uint32_t events; // Epoll events epoll_data_t data; // User data var }

Selecting next action using epoll Epoll_wait(epfd, eventReportArray, timeout) Epfd: already configured epoll event descriptor eventReportArray: two parameters Pointer to array of empty epoll events Length of this array (maximum # of events reported) Will be filled in with reported events Timeout: in milliseconds Returns: # of reported events (0 on timeout) Program should check the report array Negative value if error See: man 2 epoll_wait