OS view of networking – Sockets API (an exercise in planning for the future) David E. Culler CS162 – Operating Systems and Systems Programming Lecture.

Slides:



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

End2End Design – The Internet Architecture David E. Culler CS162 – Operating Systems and Systems Programming Lecture 32.
© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
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.
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 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
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.
McGraw-Hill©The McGraw-Hill Companies, Inc., 2004 Application Layer PART VI.
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)
What Is TCP/IP? The large collection of networking protocols and services called TCP/IP denotes far more than the combination of the two key protocols.
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
Server Design Discuss Design issues for Servers Review Server Creation in Linux.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE453 – Introduction to Computer Networks Lecture 15 – Transport Layer (II)
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
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
CS162 Operating Systems and Systems Programming Lecture 4 Introduction to I/O, Sockets, Networking September 9 th, 2015 Prof. John Kubiatowicz
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
Sirak Kaewjamnong Computer Network Systems
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.
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
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)
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
Distributed Computing A Programmer’s Perspective.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
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.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
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.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
CS162 Operating Systems and Systems Programming Lecture 4 Introduction to I/O (Continued), Sockets, Networking February 2 nd, 2015 Prof. John Kubiatowicz.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
1 K. Salah Application Layer Module K. Salah Network layer duties.
Socket Programming Jignesh Patel Palanivel Rathinam connecting processes.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Sockets and Beginning Network Programming
CS162 Operating Systems and Systems Programming Lecture 4 Introduction to I/O, Sockets, Networking September 7th, 2016 Prof. Anthony D. Joseph
CS 1652 Jack Lange University of Pittsburgh
Elementary UDP Sockets
Network and Sockets Today: Get started doing network programming
Socket Programming in C
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
TCP Sockets Programming
Advanced Network Programming spring 2007
TCP/IP Socket Programming in C
Chapter 2 Application Layer
Socket Programming(1/2)
Internet Networking recitation #8
September 10th, 2018 Prof. Ion Stoica
Presentation transcript:

OS view of networking – Sockets API (an exercise in planning for the future) David E. Culler CS162 – Operating Systems and Systems Programming Lecture 5 Sept. 10, 2014 Reading: OSC 2.7, 3.6 HW: 1 is out, due 9/15 Proj: Adjustment on Culler Office Hours: - Tue 9-10, Wed 2-3, Th 1-2 in 449 Soda

Real Reading Unix Network Programming. The Sockets Networking API, Stevens (et al), Ch 3-5 “Elementary Sockets” Lots of on-line tutorials This lecture and the code /10/14cs162 fa14 L52

Communication between processes Producer and Consumer of a file may be distinct processes May be separated in time (or not) 9/10/14cs162 fa14 L53 write(wfd, wbuf, wlen); n = read(rfd,rbuf,rmax);

Communication Across the world looks like file IO But what’s the analog of open? What is the namespace? How are they connected in time? 9/10/14cs162 fa14 L54 write(wfd, wbuf, wlen); n = read(rfd,rbuf,rmax);

Request Response Protocol 9/10/14cs162 fa14 L55 write(rqfd, rqbuf, buflen); n = read(rfd,rbuf,rmax); Client (issues requests)Server (performs operations) requests responses write(wfd, respbuf, len); n = read(resfd,resbuf,resmax); service request wait

Request Response Protocol 9/10/14cs162 fa14 L56 write(rqfd, rqbuf, buflen); n = read(rfd,rbuf,rmax); Client (issues requests)Server (performs operations) requests responses write(wfd, respbuf, len); n = read(resfd,resbuf,resmax); service request wait

Client-Server Models File servers, web, FTP, Databases, … Many clients accessing a common server 9/10/14cs162 fa14 L57 Server Client 1 Client 2 Client n ***

Sockets Mechanism for inter-process communication Data transfer like files – Read / Write against a descriptor Over ANY kind of network – Local to a machine – Over the internet (TCP/IP, UDP/IP) – OSI, Appletalk, SNA, IPX, SIP, NS, … 9/10/14cs162 fa14 L58

Silly Echo Server – running example 9/10/14cs162 fa14 L59 write(fd, buf,len); n = read(fd,buf,); Client (issues requests) Server (performs operations) requests responses write(fd, buf,); n = read(fd,rcvbuf, ); print wait gets(fd,sndbuf, …); print

Echo client-server example 9/10/14cs162 fa14 L510 void client(int sockfd) { int n; char sndbuf[MAXIN]; char rcvbuf[MAXOUT]; getreq(sndbuf, MAXIN); /* prompt */ while (strlen(sndbuf) > 0) { write(sockfd, sndbuf, strlen(sndbuf)); /* send */ memset(rcvbuf,0,MAXOUT); /* clear */ n=read(sockfd, rcvbuf, MAXOUT-1); /* receive */ write(STDOUT_FILENO, rcvbuf, n); /* echo */ getreq(sndbuf, MAXIN); /* prompt */ } void server(int consockfd) { char reqbuf[MAXREQ]; int n; while (1) { memset(reqbuf,0, MAXREQ); n = read(consockfd,reqbuf,MAXREQ-1); /* Recv */ if (n <= 0) return; n = write(STDOUT_FILENO, reqbuf, strlen(reqbuf)); n = write(consockfd, reqbuf, strlen(reqbuf)); /* echo*/ }

Prompt for input 9/10/14cs162 fa14 L511 char *getreq(char *inbuf, int len) { /* Get request char stream */ printf("REQ: "); /* prompt */ memset(inbuf,0,len); /* clear for good measure */ return fgets(inbuf,len,stdin); /* read up to a EOL */ }

Socket creation and connection File systems provide a collection of permanent objects in structured name space – Processes open, read/write/close them – Files exist independent of the processes Sockets provide a means for processes to communicate (transfer data) to other processes. Creation and connection is more complex Form 2-way pipes between processes – Possibly worlds away 9/10/14cs162 fa14 L512

Sockets in concept 9/10/14cs162 fa14 L513 Client Server read response Close Client Socket Create Client Socket Connect it to server (host:port) Create Server Socket Bind it to an Address (host:port) Listen for Connection Close Connection Socket Close Server Socket Accept connection read request Connection Socket write request write response

Client Protocol 9/10/14cs162 fa14 L514 char *hostname; int sockfd, portno; struct sockaddr_in serv_addr; struct hostent *server; server = buildServerAddr(&serv_addr, hostname, portno); /* Create a TCP socket */ sockfd = socket(AF_INET, SOCK_STREAM, 0) /* Connect to server on port */ connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr) printf("Connected to %s:%d\n",server->h_name, portno); /* Carry out Client-Server protocol */ client(sockfd); /* Clean up on termination */ close(sockfd);

Server Protocol (v1) 9/10/14cs162 fa14 L515 /* Create Socket to receive requests*/ lstnsockfd = socket(AF_INET, SOCK_STREAM, 0); /* Bind socket to port */ bind(lstnsockfd, (struct sockaddr *)&serv_addr,sizeof(serv_addr)); while (1) { /* Listen for incoming connections */ listen(lstnsockfd, MAXQUEUE); /* Accept incoming connection, obtaining a new socket for it */ consockfd = accept(lstnsockfd, (struct sockaddr *) &cli_addr, &clilen); server(consockfd); close(consockfd); } close(lstnsockfd);

Administrative break 9/10/14cs162 fa14 L516

How does the server protect itself? Isolate the handling of each connection By forking it off as another process 9/10/14cs162 fa14 L517

Sockets in concept 9/10/14cs162 fa14 L518 Client Server Create Client Socket Connect it to server (host:port) write request read response Close Client Socket Create Server Socket Bind it to an Address (host:port) Listen for Connection Accept connection read request write response Close Connection Socket Close Server Socket Connection Socket child Close Connection Socket Close Listen Socket Parent Wait for child

Server Protocol (v2) 9/10/14cs162 fa14 L519 while (1) { listen(lstnsockfd, MAXQUEUE); consockfd = accept(lstnsockfd, (struct sockaddr *) &cli_addr, &clilen); cpid = fork(); /* new process for connection */ if (cpid > 0) { /* parent process */ close(consockfd); tcpid = wait(&cstatus); } else if (cpid == 0) { /* child process */ close(lstnsockfd); /* let go of listen socket */ server(consockfd); close(consockfd); exit(EXIT_SUCCESS); /* exit child normally */ } close(lstnsockfd);

Concurrent Server Listen will queue requests Buffering present elsewhere But server waits for each connection to terminate before initiating the next 9/10/14cs162 fa14 L520

Sockets in concept 9/10/14cs162 fa14 L521 Client Server Create Client Socket Connect it to server (host:port) write request read response Close Client Socket Create Server Socket Bind it to an Address (host:port) Listen for Connection Accept connection read request write response Close Connection Socket Close Server Socket Connection Socket child Close Connection Socket Close Listen Socket Parent

Server Protocol (v3) 9/10/14cs162 fa14 L522 while (1) { listen(lstnsockfd, MAXQUEUE); consockfd = accept(lstnsockfd, (struct sockaddr *) &cli_addr, &clilen); cpid = fork(); /* new process for connection */ if (cpid > 0) { /* parent process */ close(consockfd); //tcpid = wait(&cstatus); } else if (cpid == 0) { /* child process */ close(lstnsockfd); /* let go of listen socket */ server(consockfd); close(consockfd); exit(EXIT_SUCCESS); /* exit child normally */ } close(lstnsockfd);

Server Address - itself Simple form Internet Protocol accepting any connections on the specified port In “network byte ordering” 9/10/14cs162 fa14 L523 memset((char *) &serv_addr,0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno);

Client: getting the server address 9/10/14cs162 fa14 L524 struct hostent *buildServerAddr(struct sockaddr_in *serv_addr, char *hostname, int portno) { struct hostent *server; /* Get host entry associated with a hostname or IP address */ server = gethostbyname(hostname); if (server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(1); } /* Construct an address for remote server */ memset((char *) serv_addr, 0, sizeof(struct sockaddr_in)); serv_addr->sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&(serv_addr->sin_addr.s_addr), server->h_length); serv_addr->sin_port = htons(portno); return server; }

Namespaces for communication Hostname – IP address – (ipv6?) Port Number – are “well known” or “system” portswell known Superuser privileges to bind to one – 1024 – are “registered” ports (registry)registry Assigned by IANA for specific services – 49152–65535 ( to 2 16 −1) are “dynamic” or “private” Automatically allocated as “ephemeral Ports” 9/10/14cs162 fa14 L525

Recall: UNIX Process Management UNIX fork – system call to create a copy of the current process, and start it running – No arguments! UNIX exec – system call to change the program being run by the current process UNIX wait – system call to wait for a process to finish UNIX signal – system call to send a notification to another process 9/10/14cs162 fa14 L526

Signals – infloop.c 9/10/14cs162 fa14 L527 #include void signal_callback_handler(int signum) { printf("Caught signal %d - phew!\n",signum); exit(1); } int main() { signal(SIGINT, signal_callback_handler); while (1) {} } Got top?

Process races: fork.c 9/10/14cs162 fa14 L528 if (cpid > 0) { mypid = getpid(); printf("[%d] parent of [%d]\n", mypid, cpid); for (i=0; i<100; i++) { printf("[%d] parent: %d\n", mypid, i); // sleep(1); } } else if (cpid == 0) { mypid = getpid(); printf("[%d] child\n", mypid); for (i=0; i>-100; i--) { printf("[%d] child: %d\n", mypid, i); // sleep(1); }

BIG OS Concepts so far Processes Address Space Protection Dual Mode Interrupt handlers (including syscall and trap) File System – Integrates processes, users, cwd, protection Key Layers: OS Lib, Syscall, Subsystem, Driver – User handler on OS descriptors Process control – fork, wait, signal --- exec Communication through sockets Client-Server Protocol 9/10/14cs162 fa14 L529

Course Structure: Spiral 9/10/14cs162 fa14 L530 intro