1 Carnegie Mellon Networking Basics and Concurrent Programming Shiva (sshankar) Section M.

Slides:



Advertisements
Similar presentations
Review: –What functionality is supported by IP? –What IP does not do? –How many classes of IP addresses? –Explain fields in an IP header? –How subnet works?
Advertisements

Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
1 Carnegie Mellon Proxylab and stuff : Introduction to Computer Systems Recitation 13: November 19, 2012 Donald Huang (donaldh) Section M.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Tutorial 8 Socket Programming
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
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.
Carnegie Mellon 1 Proxy Web & Concurrency Ian Hartwig 15/ Section E Recitation 13 April 15 th, 2013.
Carnegie Mellon 1 Proxy Web & Concurrency Rohith Jagannathan April 13 th, /18-213: Introduction to Computer Systems Recitation 13.
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,
UNIX Sockets COS 461 Precept 1.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
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
Client-Side Network Programming
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.
IT1352-NETWORK PROGRAMMING AND MANAGEMENT
Lecture 4: Networked Applications & Sockets Reading: Chapter 1 ? CMSC 23300/33300 Computer Networks
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Threads and Processes.
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.
Remote Shell CS230 Project #4 Assigned : Due date :
1 Concurrent Programming. 2 Outline Concurrent programming –Processes –Threads Suggested reading: –12.1, 12.3.
Distributed Computing A Programmer’s Perspective.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Introduction to Socket
Week 16 (April 25 th ) Outline Thread Synchronization Lab 7: part 2 & 3 TA evaluation form Reminders Lab 7: due this Thursday Final review session Final.
Processes CSCI 4534 Chapter 4. Introduction Early computer systems allowed one program to be executed at a time –The program had complete control of the.
Socket Programming Lab 1 1CS Computer Networks.
Carnegie Mellon 1 Proxy: Web & Concurrency : Introduction to Computer Systems Recitation 13: Monday, Nov. 18 th, 2013 Marjorie Carlson Section A.
Carnegie Mellon 1 Proxy Recitation Jeffrey Liu Recitation 13: November 23, 2015.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Review: – Why layer architecture? – peer entities – Protocol and service interface – Connection-oriented/connectionless service – Reliable/unreliable service.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
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.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
Carnegie Mellon Recitation 11: ProxyLab Fall 2009 November 16, 2009 Including Material from Previous Semesters' Recitations.
Assignment 3 A Client/Server Application: Chatroom
Web Development Web Servers.
UNIX Sockets COS 461 Precept 1.
Threads Threads.
Concurrent Programming November 13, 2008
Socket Programming in C
Tutorial on Socket Programming
Concurrent Servers Topics Limitations of iterative servers
TCP Sockets Programming
Advanced Network Programming spring 2007
CSCI 380: Operating Systems Lecture #14 Instructor: William Killian
Server-side Programming CSE 333 Summer 2018
Concurrent Programming November 13, 2008
Internet Networking recitation #8
Today’s topic: Basic TCP API
Presentation transcript:

1 Carnegie Mellon Networking Basics and Concurrent Programming Shiva (sshankar) Section M

2 Carnegie Mellon Topics Networking Basics Concurrent Programming Introduction to Proxy Lab

3 Carnegie Mellon Sockets What is a socket?  To an application, a socket is a file descriptor that lets the application read/write from/to the network  (all Unix I/O devices, including networks, are modeled as files) Clients and servers communicate with each other by reading from and writing to socket descriptors The main difference between regular file I/O and socket I/O is how the application “opens” the socket descriptors

4 Carnegie Mellon Overview of the Sockets Interface Client / Server Session ClientServer socket bind listen rio_readlineb rio_writenrio_readlineb rio_writen Connection request rio_readlineb close EOF Await connection request from next client open_listenfd open_clientfd acceptconnect getaddrinfo

Carnegie Mellon 5 Host and Service Conversion: getaddrinfo getaddrinfo is the modern way to convert string representations of host, ports, and service names to socket address structures.  Replaces obsolete gethostbyname - unsafe because it returns a pointer to a static variable Advantages:  Reentrant (can be safely used by threaded programs).  Allows us to write portable protocol-independent code(IPv4 and IPv6)  Given host and service, getaddrinfo returns result that points to a linked list of addrinfo structs, each pointing to socket address struct, which contains arguments for sockets APIs. getnameinfo is the inverse of getaddrinfo, converting a socket address to the corresponding host and service.  Replaces obsolete gethostbyaddr and getservbyport funcs.

Sockets API int socket(int domain, int type, int protocol);  Create a file descriptor for network communication  used by both clients and servers  int sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);  One socket can be used for two-way communication int bind(int socket, const struct sockaddr *address, socklen_t address_len);  Associate a socket with an IP address and port number  used by servers  struct sockaddr_in sockaddr – family, address, port 6 Carnegie Mellon

Sockets API int listen(int socket, int backlog);  socket: socket to listen on  used by servers  backlog: maximum number of waiting connections  err = listen(sock_fd, MAX_WAITING_CONNECTIONS); int accept(int socket, struct sockaddr *address, socklen_t *address_len);  used by servers  socket: socket to listen on  address: pointer to sockaddr struct to hold client information after accept returns  return: file descriptor 7 Carnegie Mellon

Sockets API int connect(int socket, struct sockaddr *address, socklen_t address_len);  attempt to connect to the specified IP address and port described in address  used by clients int close(int fd);  used by both clients and servers  (also used for file I/O)  fd: socket fd to close 8 Carnegie Mellon

Sockets API ssize_t read(int fd, void *buf, size_t nbyte);  used by both clients and servers  (also used for file I/O)  fd: (socket) fd to read from  buf: buffer to read into  nbytes: buf length ssize_t write(int fd, void *buf, size_t nbyte);  used by both clients and servers  (also used for file I/O)  fd: (socket) fd to write to  buf: buffer to write  nbytes: buf length 9 Carnegie Mellon

10 Carnegie Mellon Topics Networking Basics Concurrent Programming Introduction to Proxy Lab

11 Carnegie Mellon Threads Threads enable light-weight concurrency by sharing much of the same address space Similarities to processes  each thread has its own logical control flow (its own registers, so its own EIP and ESP)  multiple threads can be in the middle of running at the same time, possibly on different cores  the kernel decides when to context switch to and from a thread (or a thread can voluntarily give up its share of cpu time by calling sleep, pause, sigsuspend, etc) Differences with processes  threads share code and data; processes generally don’t  threads are lesser overhead than processes (to create and reap)

12 Carnegie Mellon Threads: pthreads interface Creating/reaping threads  pthread_create  pthread_join To get your thread ID  pthread_self Terminating threads  pthread_cancel  pthread_exit synchronizing access to shared variables  pthread_mutex_init  pthread_mutex_[un]lock  pthread_rwlock_init  pthread_rwlock_[wr]rdlock

13 Carnegie Mellon Thread exit A thread terminates implicitly when its top-level thread routine returns A thread terminates explicitly by calling pthread_exit(NULL) pthread_exit(NULL) only terminates the current thread, NOT the process exit(0) terminates ALL the threads in the process (meaning the whole process terminates) pthread_cancel(tid) terminates the thread with id equal to tid

14 Carnegie Mellon Threads - Reaping Joinable threads can be reaped and killed by other threads  must be reaped with pthread_join to free memory and resources Detached threads cannot be reaped or killed by other threads  resources are automatically reaped on termination Default state is joinable  use pthread_detach(pthread_self()) to make detached

Carnegie Mellon ‹#› 15 Carnegie Mellon Topics Networking Basics Concurrent Programming Introduction to Proxy Lab

Carnegie Mellon ‹#› In the “textbook” version of the web, there are clients and servers.  Clients send requests.  Servers fulfill them. Reality is more complicated. In this lab, you’re writing a proxy.  A server to the clients.  A client to the server(s). What is a Proxy?

Carnegie Mellon ‹#› What is a Proxy?

Carnegie Mellon ‹#› Why and How? Proxies are handy for a lot of things.  To filter content … or to bypass content filtering.  For anonymity, security, firewalls, etc.  For caching — if someone keeps accessing the same web resource, why not store it locally? So how do you make a proxy?  It’s a server and a client at the same time.  You’ve seen code in the textbook for a client and for a server; what will code for a proxy look like?  Ultimately, the control flow of your program will look more like a server’s. However, when it’s time to serve the request, a proxy does so by forwarding the request onwards and then forwarding the response back to the client.

Carnegie Mellon ‹#› Step:1 Implement Proxy Lab What you end up with will resemble: Server (port 80) Client Client socket address :51213 Server socket address :80 Proxy Proxy server socket address :15213 Proxy client socket address :52943

Carnegie Mellon ‹#› Proxy Lab Your proxy should handle HTTP/1.0 GET requests.  Luckily, that’s what the web uses most, so your proxy should work on the vast majority of sites.  Reddit, Vimeo, CNN, YouTube, NY Times, etc. Features that require a POST operation (i.e., sending data to the server) will not work.  Logging in to websites, sending Facebook messages, etc. HTTPS is expected not to work.  Google (and some other popular websites) now try to push users to HTTPS by default; watch out for that. Your server should be robust. It shouldn’t crash if it receives a malformed request, a request for an item that doesn’t exist, etc. etc.

Carnegie Mellon ‹#› Sequential Proxy In the textbook version of the web, a client requests a page, the server provides it, and the transaction is done. A sequential server can handle this. We just need to serve one page at a time. This works great for simple text pages with embedded styles (a.k.a., the Web circa 1997). Web server Web client (browser)

Carnegie Mellon ‹#› Step 2: Concurrent Proxy Let’s face it, what your browser is really doing is a little more complicated than that.  A single HTML page may depend on 10s or 100s of support files (images, stylesheets, scripts, etc.).  Do you really want to load each of those one at a time?  Do you really want to wait for the server to serve every other person looking at the web page before they serve you? To speed things up, you need concurrency.  Specifically, concurrent I/O, since that’s generally slower than processing here.  You want your server to be able to handle lots of requests at the same time. That’s going to require threading. (Yay!)

Carnegie Mellon ‹#› Step 3: Cache Web Objects Your proxy should cache previously requested objects.  Don’t panic! This has nothing to do with cache lab. We’re just storing things for later retrieval, not managing the hardware cache.  Cache individual objects, not the whole page – so, if only part of the page changes, you only refetch that part.  The handout specifies a maximum object size and a maximum cache size.  Use an LRU eviction policy.  Your caching system must allow for concurrent reads while maintaining consistency. Concurrency? Shared Resource?

24 Carnegie Mellon Questions? (come to office hours if you need help)