Server-side Programming CSE 333 Winter 2019

Slides:



Advertisements
Similar presentations
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Advertisements

© 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.
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Lecture 6 TCP Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
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.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Sockets COS 518: Advanced Computer Systems Michael Freedman Fall
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,
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.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Elementary TCP Sockets
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
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,
Distributed Computing A Programmer’s Perspective.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Socket Programming Lab 1 1CS Computer Networks.
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.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
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.
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.
CSE 333 – SECTION 8 Client-Side Network Programming.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Assignment 3 A Client/Server Application: Chatroom
UNIX Sockets COS 461 Precept 1.
Week 13 - Friday CS222.
Socket Programming in C
Server-side Programming CSE 333 Spring 2018
Transport layer API: Socket Programming
Client-side Networking CSE 333 Spring 2018
Network Programming CSC- 341
UNIX Sockets Outline Homework #1 posted by end of day
Network Programming CSC- 341
UDP Sockets Programming
TCP Sockets Programming
Advanced Network Programming spring 2007
Issues in Client/Server Programming
Chapter 2 Application Layer
Server-side Programming CSE 333 Autumn 2018
Server-side Programming CSE 333 Summer 2018
Client-side Networking CSE 333 Summer 2018
Concurrency: Processes CSE 333 Summer 2018
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Client-side Networking CSE 333 Autumn 2018
Networking Introduction CSE 333 Winter 2019
Concurrency: Processes CSE 333 Autumn 2018
Sockets Programming Socket to me!.
C++ Constructor Insanity CSE 333 Winter 2019
Sockets Programming Socket to me!.
Client-side Networking CSE 333 Winter 2019
Internet Networking recitation #8
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
Concurrency: Processes CSE 333 Winter 2019
Today’s topic: Basic TCP API
Introduction to Concurrency CSE 333 Winter 2019
Client-side Networking CSE 333 Spring 2019
Presentation transcript:

Server-side Programming CSE 333 Winter 2019 Instructor: Hal Perkins Teaching Assistants: Alexey Beall Renshu Gu Harshita Neti David Porter Forrest Timour Soumya Vasisht Yifan Xu Sujie Zhou

Administrivia New exercise 16 released today, due Wednesday morning Server-side programming Ex13 (smart pointers) and Ex15 (TCP client) both still due Monday hw4 out today – due Thur. March 14 (last week of qtr) Demo today in class

Socket API: Server TCP Connection Pretty similar to clients, but with additional steps: Figure out the IP address and port on which to listen Create a socket bind() the socket to the address(es) and port Tell the socket to listen() for incoming clients accept() a client connection .read() and write() to that connection close() the client socket Analogy: bakery/coffee shop/boba shop Steps 1-3: Morning prep work (still closed) Step 4: Open shop and let customers in (queue) Step 5: “Next customer in line, please!” Step 6: Transaction occurs Step 7: Customer leaves shop or store refuses service

Servers Servers can have multiple IP addresses (“multihoming”) Usually have at least one externally-visible IP address, as well as a local-only address (127.0.0.1) The goals of a server socket are different than a client socket Want to bind the socket to a particular port of one or more IP addresses of the server Want to allow multiple clients to connect to the same port OS uses client IP address and port numbers to direct I/O to the correct server file descriptor https://en.wikipedia.org/wiki/Multihoming

Step 1: Figure out IP address(es) & Port Step 1: getaddrinfo() invocation may or may not be needed (but we’ll use it) Do you know your IP address(es) already? Static vs. dynamic IP address allocation Even if the machine has a static IP address, don’t wire it into the code – either look it up dynamically or use a configuration file Can request listen on all local IP addresses by passing NULL as hostname and setting AI_PASSIVE in hints.ai_flags Effect is to use address 0.0.0.0 (IPv4) or :: (IPv6)

Step 2: Create a Socket Step 2: socket() call is same as before Can directly use constants or fields from result of getaddrinfo() Recall that this just returns a file descriptor – IP address and port are not associated with socket yet

Step 3: Bind the socket Some specifics for addr: Looks nearly identical to connect()! Returns 0 on success, -1 on error Some specifics for addr: Address family: AF_INET or AF_INET6 What type of IP connections can we accept? POSIX systems can handle IPv4 clients via IPv6  Port: port in network byte order (htons() is handy) Address: specify particular IP address or any IP address “Wildcard address” – INADDR_ANY (IPv4), in6addr_any (IPv6) int bind(int sockfd, const struct sockaddr* addr, socklen_t addrlen); http://man7.org/linux/man-pages/man2/bind.2.html in6addr_any is lowercase because it is a variable instead of a constant

Step 4: Listen for Incoming Clients Tells the OS that the socket is a listening socket that clients can connect to backlog: maximum length of connection queue Gets truncated, if necessary, to defined constant SOMAXCONN The OS will refuse new connections once queue is full until server accept()s them (removing them from the queue) Returns 0 on success, -1 on error Clients can start connecting to the socket as soon as listen() returns Server can’t use a connection until you accept() it int listen(int sockfd, int backlog); http://man7.org/linux/man-pages/man2/listen.2.html

Example #1 See server_bind_listen.cc Takes in a port number from the command line Opens a server socket, prints info, then listens for connections for 20 seconds Can connect to it using netcat (nc)

Step 5: Accept a Client Connection Returns an active, ready-to-use socket file descriptor connected to a client (or -1 on error) sockfd must have been created, bound, and listening Pulls a queued connection or waits for an incoming one addr and addrlen are output parameters *addrlen should initially be set to sizeof(*addr), gets overwritten with the size of the client address Address information of client is written into *addr Use inet_ntop() to get the client’s printable IP address Use getnameinfo() to do a reverse DNS lookup on the client int accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen);

Example #2 See server_accept_rw_close.cc Takes in a port number from the command line Opens a server socket, prints info, then listens for connections Can connect to it using netcat (nc) Accepts connections as they come Echoes any data the client sends to it on stdout and also sends it back to the client EAGAIN and EWOULDBLOCK checks are for when accept() is called on nonblocking sockets and there are no connections present to be accepted.

Something to Note Our server code is not concurrent Single thread of execution The thread blocks while waiting for the next connection The thread blocks waiting for the next message from the connection A crowd of clients is, by nature, concurrent While our server is handling the next client, all other clients are stuck waiting for it 

hw4 demo Multithreaded Web Server (333gle) Don’t worry – multithreading has mostly been written for you ./http333d <port> <static files> <indices+> Some security bugs to fix, too Start up server on attu Navigate to attu.cs.washington.edu (incorrect port – nothing listening on port 80) Navigate to attu.cs.washington.edu:4444 (incorrect port) Navigate to attu.cs.washington.edu:3333 (correct port) Show source – you’ll have to send raw text back from server Run a few queries, show source for results table (hopefully mix of Wikipedia and local files) Lookup PID (ps –u from another terminal) Kill PID Refresh browser Start up server on local machine (maybe even kill wifi?)

Extra Exercise #1 Write a program that: Creates a listening socket that accepts connections from clients Reads a line of text from the client Parses the line of text as a DNS name Does a DNS lookup on the name Writes back to the client the list of IP addresses associated with the DNS name Closes the connection to the client