#include <sys/socket.h> int listen(int socket, int backlog); socket. socket. backlog

Similar documents
Network Programming: Part II

Network Programming: Part II

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Network Programming: Part I

Serving dynamic content. Issues in serving dynamic content. add.com: THE Internet addition portal! CGI. Page 2

Network Programming November 3, 2008

15213 Recitation Section C

Sockets and Parallel Computing. CS439: Principles of Computer Systems April 11, 2018

CSE 333 Section 8 - Client-Side Networking

Network Programming / : Introduc2on to Computer Systems 21 st Lecture, April. 4, Instructors: Todd Mowry and Anthony Rowe

Computer Network Programming

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

Logistics. Recitation 11: I/O Problems. Today s Plan. Why Use Robust I/O. Andrew Faulring Section A 18 November 2002

Concurrent Servers Dec 2, 2009"

CS 4400 Fall 2017 Final Exam Practice

Network Programming /18-243: Introduc3on to Computer Systems 22 nd Lecture, 13 April Instructors: Bill Nace and Gregory Kesden

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

Concurrent Programming November 28, 2007

System-Level I/O. Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O

Azblink API for Sending XMPP Messages via HTTP POST

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

UNIX System Programming Lecture 19: IP Sockets

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

CSE 333 SECTION 7. Client-Side Network Programming

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

Lecture #23 Concurrent Programming

CSE 333 SECTION 6. Networking and sockets

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Socket Programming. #In the name of Allah. Computer Engineering Department Sharif University of Technology CE443- Computer Networks

CSE 333 SECTION 8. Sockets, Network Programming

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Networks and Their Abstractions. CS439: Principles of Computer Systems April 9, 2018

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

CSE 333 SECTION 7. C++ Virtual Functions and Client-Side Network Programming

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

CONCURRENT PROGRAMMING. Lecture 5

CSCI 4061: Sockets and Network Programming

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Concurrent Programming April 21, 2005

2007 Microsoft Corporation. All rights reserved.

CSE 333 SECTION 7. Client-Side Network Programming

Concurrent Programming

System-Level I/O Nov 14, 2002

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

A Typical Hardware System The course that gives CMU its Zip! System-Level I/O Nov 14, 2002

Iterative Servers The course that gives CMU its Zip! Iterative servers process one request at a time. Concurrent Servers

5. System Calls or Bust

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

System-Level I/O April 6, 2006

CSE 333 Section 3. Thursday 12 April Thursday, April 12, 12

Introduction to Lab 2 and Socket Programming. -Vengatanathan Krishnamoorthi

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

// print product names in alphabetical order with total // number sold of each product using format name: total void PrintTotals();

Client-side Networking

Introduction to Computer Systems , fall th Lecture, Oct. 19 th

Three Basic Mechanisms for Creating Concurrent Flows. Systemprogrammering 2009 Föreläsning 10 Concurrent Servers. Process-Based Concurrent Server

Introduction to Computer Networks

System-Level I/O. William J. Taffe Plymouth State University. Using the Slides of Randall E. Bryant Carnegie Mellon University

Network Programming September 6, 2005

The exam is closed book, closed notes, closed electronics, closed telepathy, open mind.

last time redo logging copy-on-write filesystems / snapshots distributed systems motivation, etc.

CSE 124 Discussion Section Sockets Programming 10/10/17

System-Level I/O March 31, 2005

System-Level I/O : Introduction to Computer Systems 16 th Lecture, March 23, Instructors: Franz Franchetti & Seth Copen Goldstein

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

Server-side Programming

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

Unix Network Programming Chapter 4. Elementary TCP Sockets 광운대학교컴퓨터과학과 정보통신연구실 석사과정안중현

Write your answer on the next page. You may remove this page for reference while working if you wish.

Network Communication

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

TCP: Three-way handshake

Concurrent Programming

call connect call read call connect ret connect call fgets Client 2 blocks waiting to complete its connection request until after lunch!

6 장과제샘플코드 - 다자간채팅 (udp 버전 1) 목포해양대해양컴퓨터공학과

Networked Applications: Sockets. End System: Computer on the Net

Foundations of Computer Systems

Concurrent Programming is Hard! Concurrent Programming. Reminder: Iterative Echo Server. The human mind tends to be sequential

Ports under 1024 are often considered special, and usually require special OS privileges to use.

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

Unix programming interface for file I/O operations and pipes

Carnegie Mellon Concurrency & Proxy Lab

Introduction to Computer Systems. Final Exam. May 3, Notes and calculators are permitted, but not computers. Caching. Signals.

Socket Programming TCP UDP

Sockets 15H2. Inshik Song

Introduction to Client-Server Model

목포해양대해양컴퓨터공학과. IPv6 적용

Concurrent Programming

System- Level I/O /18-243: Introduc3on to Computer Systems 16 th Lecture, Mar. 15, Instructors: Gregory Kesden and Anthony Rowe

UNIX Sockets. COS 461 Precept 1

Concurrent Programming

Redesde Computadores(RCOMP)

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

Chapter 6. The Transport Layer. Transport Layer 3-1

CSE 333 Lecture 16 - network programming intro

Transcription:

connection listener

#include <sys/socket.h> int listen(int socket, int backlog); socket socket backlog

#include <sys/socket.h> int accept(int socket, struct sockaddr *addr, socklen_t *addr_len); socket socketlisten addr addr_len

#include <sys/types.h> #include <sys/socket.h> int connect(int socket, struct sockaddr *addr, socklen_t addr_len); socket addr addr_len accept

getaddrinfo socket getaddrinfo socket bind listen connect accept read write read write close EOF EOF close Computer Systems: A Programmer s Perspective

getaddrinfo socket getaddrinfo socket bind listen connect accept shutdown read close write EOF read write EOF close Computer Systems: A Programmer s Perspective

open_clientfd getaddrinfo socket connect getaddrinfo socket bind listen accept open_listenfd read write read write close EOF EOF close Computer Systems: A Programmer s Perspective

getaddrinfo socket getaddrinfo socket getaddrinfo socket bind listen connect accept connect read write read write read write read write close EOF EOF close close EOF EOF close Computer Systems: A Programmer s Perspective

... memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_INET; /* Request IPv4 */ hints.ai_socktype = SOCK_STREAM; /* Accept TCP connections */ hints.ai_flags = AI_PASSIVE; /*... on any IP address */ Getaddrinfo(NULL, portno, &hints, &addrs); tcp_server.c ls = Socket(addrs->ai_family, addrs->ai_socktype, addrs->ai_protocol); Bind(ls, addrs->ai_addr, addrs->ai_addrlen); Freeaddrinfo(addrs); Listen(ls, 5); while (1) {... s = Accept(ls, (struct sockaddr *)&addr, &len); amt = Read(s, buffer, MAXBUF); write(1, buffer, amt); write(1, "\n", 1); write(s, &amt, sizeof(amt)); Close(s); }...

... memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_INET; /* Request IPv4 */ hints.ai_socktype = SOCK_STREAM; /* TCP connection */ Getaddrinfo(hostname, portno, &hints, &addrs); tcp_client.c s = Socket(addrs->ai_family, addrs->ai_socktype, addrs->ai_protocol); Connect(s, addrs->ai_addr, addrs->ai_addrlen); Freeaddrinfo(addrs); copies = ((argc == 5)? atoi(argv[4]) : 1); len = strlen(argv[3]); while (copies--) { amt = Write(s, argv[3], len); if (amt!= len) app_error("incomplete write"); } got = 0; amt = Read(s, &got, sizeof(got)); printf("server got %ld\n", got); Close(s);...

csapp.c ssize_t rio_readn(int fd, void *usrbuf, size_t n); ssize_t rio_writen(int fd, void *usrbuf, size_t n); read write

tcp_server2.c... s = Accept(ls, (struct sockaddr *)&addr, &len); while (1) { char buffer[maxbuf]; size_t amt = Rio_readn(s, buffer, MAXBUF); if (amt == 0) { printf("client is done\n"); Rio_writen(s, &total_amt, sizeof(total_amt)); break; } else { Rio_writen(1, buffer, amt); Rio_writen(1, "\n", 1); total_amt += amt; } } Close(s);...

tcp_client2.c... while (copies--) Rio_writen(s, argv[3], len); Shutdown(s, SHUT_WR); got = 0; amt = Rio_readn(s, &got, sizeof(got)); if (amt!= sizeof(got)) app_error("response truncated"); printf("server got %ld\n", got); Close(s);...

tcp_server3.c... s = Accept(ls, (struct sockaddr *)&addr, &len); amt = Rio_readn(s, &total_amt, sizeof(total_amt)); if (amt!= sizeof(total_amt)) app_error("amount truncated"); buffer = malloc(total_amt); amt = Rio_readn(s, buffer, total_amt); Rio_writen(1, buffer, amt); Rio_writen(1, "\n", 1); free(buffer); write(s, &amt, sizeof(amt)); Close(s);...

tcp_client3.c... len = strlen(argv[3]); amt = copies * len; Rio_writen(s, &amt, sizeof(amt)); while (copies--) Rio_writen(s, argv[3], len); got = 0; amt = Rio_readn(s, &got, sizeof(got)); if (amt!= sizeof(got)) app_error("response truncated"); printf("server got %ld\n", got); Close(s);...

server client lnr = Open_listenfd(...); fd = Accept(lnr,...); fd = Open_clientfd(...);

server "Hi\n" client lnr = Open_listenfd(...); fd = Accept(lnr,...); fd = Open_clientfd(...); Rio_writen(fd, "Hi\n", 3);

server "Hi\n" "Hi\n" client lnr = Open_listenfd(...); fd = Accept(lnr,...); fd = Open_clientfd(...); Rio_writen(fd,...); Rio_writen(fd, "Hi\n", 3);

server "Hi\n" "Hi\n" client lnr = Open_listenfd(...); fd = Accept(lnr,...); fd = Open_clientfd(...); while (readline(fd,...)) Rio_writen(fd,...); Rio_writen(fd, "Hi\n", 3);...

server "Hi\n" "Hi\n" client lnr = Open_listenfd(...); fd = Accept(lnr,...); while (readline(fd,...)) Rio_writen(fd,...); fd = Open_clientfd(...); Rio_writen(fd, "Hi\n", 3);...

#include "csapp.h" void rio_readinitb(rio_t *rp, int fd); ssize_t rio_readlineb(rio_t *rp, void *buf, size_t maxlen); ssize_t rio_readnb(rio_t *rp, void *buf, size_t n); rio_initb rio_readlineb rio_readnb rio_readn

echo.c int main(int argc, char **argv) { int listenfd, connfd; char client_hostname[maxline], client_port[maxline]; struct sockaddr_storage clientaddr; /* Enough room for any addr */ listenfd = Open_listenfd(argv[1]); while (1) { socklen_t clientlen = sizeof(struct sockaddr_storage); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); Getnameinfo((SA *) &clientaddr, clientlen, client_hostname, MAXLINE, client_port, MAXLINE, 0); printf("connected to (%s, %s)\n", client_hostname, client_port); echo(connfd); Close(connfd); } }... Computer Systems: A Programmer s Perspective

echo.c... void echo(int connfd) { size_t n; char buf[maxline]; rio_t rio; Rio_readinitb(&rio, connfd); } while((n = Rio_readlineb(&rio, buf, MAXLINE))!= 0) { printf("server received %ld bytes\n", n); Rio_writen(connfd, buf, n); } Computer Systems: A Programmer s Perspective

telnet locahost4567 $ telnet localhost 4567 quit

browser client http://www.eng.utah.edu/~cs4400/

www.eng.utah.edu:80 web server browser client http://www.eng.utah.edu/~cs4400/

www.eng.utah.edu:80 web server browser client http://www.eng.utah.edu/~cs4400/

www.eng.utah.edu:80 web server GET /~cs4400/ HTTP/1.0 browser client http://www.eng.utah.edu/~cs4400/

www.eng.utah.edu:80 web server GET /~cs4400/ HTTP/1.0 <html> </html> browser client http://www.eng.utah.edu/~cs4400/

scheme://host:port/path?query#fragment scheme http https ftp http

scheme://host:port/path?query#fragment scheme host www.eng.utah.edu google.com 127.0.0.1

scheme://host:port/path?query#fragment scheme host port 80 http://localhost/ localhost http://localhost:8090/ localhost

scheme://host:port/path?query#fragment scheme host port path path

scheme://host:port/path?query#fragment scheme host port path http://www.eng.utah.edu/~cs4400/network.pdf https://goo.gl/om5fjq

scheme://host:port/path?query#fragment scheme host port path querypath key=value&; http://www.youtube.com/watch?v=kfyugnewhj4 http://twitter.com/search?q=utah&src=typd query

scheme://host:port/path?query#fragment scheme host port path querypath fragment path fragmentclient http://www.eng.utah.edu/~cs4400/#(part._staff)

scheme://host:port/path?query#fragment path? query# valuequery& Percent encoding %XX X 0xXX + lost+founduserme&you http://server.com/lost%2bfound?user=me%26you

http://host:port/path?query#fragment host:port GET POST

http://host:port/path?query#fragment host:port GET /path?query HTTP/1.0CRLF field: valuecrlf header CRLF CRLF

http://host:port/path?query#fragment host:port HTTP/1.1 GET /path?query HTTP/1.0CRLF field: valuecrlf header CRLF CRLF

http://host:port/path?query#fragment host:port GET /path?query HTTP/1.0CRLF field: valuecrlf header CRLF CRLF

http://host:port/path?query#fragment host:port GET /path?query HTTP/1.0CRLF field: valuecrlf header CRLF echo.c

http://host:port/path?query#fragment host:port GET /path?query HTTP/1.0CRLF field: valuecrlf header CRLF HTTP/1.0 status status-messagecrlf field: valuecrlf header CRLF data

http://host:port/path?query#fragment host:port GET /path?query HTTP/1.0CRLF field: valuecrlf header CRLF HTTP/1.0 status status-messagecrlf field: valuecrlf header CRLF data

http://host:port/path?query#fragment host:port GET /path?query HTTP/1.0CRLF field: valuecrlf header CRLF HTTP/1.0 status status-messagecrlf field: valuecrlf header CRLF data status status-description 200 OK 301 Moved permanently 400 Bad request 501 Not implemented

http://host:port/path?query#fragment host:port GET /path?query HTTP/1.0CRLF field: valuecrlf header CRLF HTTP/1.0 status status-messagecrlf field: valuecrlf header CRLF data telnet

http://host:port/path?query#fragment host:port POST /path?query HTTP/1.0CRLF field: valuecrlf header CRLF data GET

Content-LengthPOST Content-TypePOST text/html text/html; charset=utf-8 application/x-www-form-urlencoded query Connection close

TINY Web Server /path?query /path/cgi-bin path Content-Length Content-Type /path/cgi-bin path QUERY_STRING query

tiny.c int main(int argc, char **argv) { int listenfd, connfd; char hostname[maxline], port[maxline]; struct sockaddr_storage clientaddr; listenfd = Open_listenfd(argv[1]); while (1) { socklen_t clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0); printf("accepted connection from (%s, %s)\n", hostname, port); doit(connfd); } }... Close(connfd); Computer Systems: A Programmer s Perspective

tiny.c void doit(int fd) { int is_static; char buf[maxline], method[maxline], uri[maxline], version[maxline]; char filename[maxline], cgiargs[maxline]; rio_t rio; Rio_readinitb(&rio, fd); if (!Rio_readlineb(&rio, buf, MAXLINE)) return; sscanf(buf, "%s %s %s", method, uri, version); read_requesthdrs(&rio); is_static = parse_uri(uri, filename, cgiargs); }... Computer Systems: A Programmer s Perspective

tiny.c void read_requesthdrs(rio_t *rp) { char buf[maxline]; } Rio_readlineb(rp, buf, MAXLINE); while (strcmp(buf, "\r\n")) Rio_readlineb(rp, buf, MAXLINE); Computer Systems: A Programmer s Perspective

tiny.c int parse_uri(char *uri, char *filename, char *cgiargs) {... } if (!strstr(uri, "cgi-bin")) {... if (uri[strlen(uri)-1] == '/') strcat(filename, "home.html");... return 1; /* => static content */ } else {... return 0; /* => dynamic content */ } Computer Systems: A Programmer s Perspective

void doit(int fd) { struct stat sbuf; tiny.c... if (stat(filename, &sbuf) < 0) { clienterror(fd, filename, "404", "Not found",...); return; } } if (is_static) {... serve_static(fd, filename, sbuf.st_size); } else {... serve_dynamic(fd, filename, cgiargs); } Computer Systems: A Programmer s Perspective

tiny.c void serve_static(int fd, char *filename, int filesize) { int srcfd; char *srcp, filetype[maxline], buf[maxbuf]; get_filetype(filename, filetype); sprintf(buf, "HTTP/1.0 200 OK\r\n"); sprintf(buf, "%sserver: Tiny Web Server\r\n", buf); sprintf(buf, "%sconnection: close\r\n", buf); sprintf(buf, "%scontent-length: %d\r\n", buf, filesize); sprintf(buf, "%scontent-type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); } srcfd = Open(filename, O_RDONLY, 0); srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); Rio_writen(fd, srcp, filesize); Munmap(srcp, filesize); Computer Systems: A Programmer s Perspective

tiny.c void get_filetype(char *filename, char *filetype) { if (strstr(filename, ".html")) strcpy(filetype, "text/html"); else if (strstr(filename, ".gif")) strcpy(filetype, "image/gif"); else if (strstr(filename, ".png")) strcpy(filetype, "image/png"); else if (strstr(filename, ".jpg")) strcpy(filetype, "image/jpeg"); else strcpy(filetype, "text/plain"); } Computer Systems: A Programmer s Perspective

tiny.c void serve_dynamic(int fd, char *filename, char *cgiargs) { char buf[maxline], *emptylist[] = { NULL }; /* Return first part of HTTP response */ sprintf(buf, "HTTP/1.0 200 OK\r\n"); Rio_writen(fd, buf, strlen(buf)); sprintf(buf, "Server: Tiny Web Server\r\n"); Rio_writen(fd, buf, strlen(buf)); } if (Fork() == 0) { setenv("query_string", cgiargs, 1); Dup2(fd, STDOUT_FILENO); /* Redirect stdout to client */ Execve(filename, emptylist, environ); } Wait(NULL); Computer Systems: A Programmer s Perspective