Server-side Programming

Similar documents
Server-side Programming

CSE 333 Lecture server sockets

CSE 333 Lecture server sockets

CSE 333 Lecture server sockets

IP Addresses, DNS. CSE 333 Spring Instructor: Justin Hsia

CSE 333 SECTION 8. Sockets, Network Programming

Concurrency and Threads

Client-side Networking

CSE 124 January 12, Winter 2016, UCSD Prof. George Porter

CSE 124 January 18, Winter 2017, UCSD Prof. George Porter

C++ Inheritance I. CSE 333 Spring Instructor: Justin Hsia

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

C++ Constructor Insanity

CSE 333 SECTION 7. Client-Side Network Programming

CSE 333 SECTION 6. Networking and sockets

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

CSE 124 Discussion Section Sockets Programming 10/10/17

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

Review. Preview. Closing a TCP Connection. Closing a TCP Connection. Port Numbers 11/27/2017. Packet Exchange for TCP Connection

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

UNIX System Programming Lecture 19: IP Sockets

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

Sockets 15H2. Inshik Song

CSE 333 Section 8 - Client-Side Networking

Memory and Arrays. CSE 333 Spring Instructor: Justin Hsia

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

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

C++ Inheritance II, Casting

Socket Programming for TCP and UDP

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

Elementary TCP Sockets

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

The Heap and Structs. CSE 333 Spring Instructor: Justin Hsia

Socket Programming TCP UDP

STUDY OF SOCKET PROGRAMMING

TCP: Three-way handshake

CSE 333 Lecture network programming intro

EEC-484/584 Computer Networks

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

Introduction to Computer Systems. Networks 2. c Theodore Norvell. The Sockets API

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

CSE 333 SECTION 7. Client-Side Network Programming

sottotitolo Socket Programming Milano, XX mese 20XX A.A. 2016/17 Federico Reghenzani

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

UNIT IV- SOCKETS Part A

UNIX Sockets. COS 461 Precept 1

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

Oral. Total. Dated Sign (2) (5) (3) (2)

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

A. Basic Function Calls for Network Communications

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

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

CSCI 4061: Sockets and Network Programming

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

Network Communication

ECE 435 Network Engineering Lecture 2

Computer Network Programming

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

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

IPv6 Porting Applications

Introduction to Computer Networks

CLIENT-SIDE PROGRAMMING

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

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

Unix Network Programming

System Programming. Sockets

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

CSE 333 Final Exam June 6, Name ID #

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

IP Addresses, DNS. CSE 333 Summer Teaching Assistants: Renshu Gu William Kim Soumya Vasisht

A Client-Server Exchange

CS118 Discussion 1B, Week 1. Taqi Raza BUNCHE 1209B, Fridays 12:00pm to 1:50pm

CSE 333 Lecture 16 - network programming intro

ECE 435 Network Engineering Lecture 2

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

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

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

CSE 43: Computer Networks Structure, Threading, and Blocking. Kevin Webb Swarthmore College September 14, 2017

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

Computer Networks Prof. Ashok K. Agrawala

Introduction to Socket Programming

Introduction to Socket Programming

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

Group-A Assignment No. 6

CSE 333 Lecture 8 - file and network I/O

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

Overview. Administrative. * HW# 5 Due next week. * HW# 5 : Any Questions. Topics. * Client Server Communication. * 12.

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

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

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

Communication. Sockets (Haviland Ch. 10)

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Assignment 2 Group 5 Simon Gerber Systems Group Dept. Computer Science ETH Zurich - Switzerland

Transcription:

Server-side Programming CSE 333 Spring 2018 Instructor: Justin Hsia Teaching Assistants: Danny Allen Dennis Shao Eddie Huang Kevin Bi Jack Xu Matthew Neldam Michael Poulain Renshu Gu Robby Marver Waylon Huang Wei Lin

Administrivia Exercise 15 released yesterday, due Monday (5/21) Client-side programming Exercise 16 released today, due Wednesday (5/23) Server-side programming hw4 posted and files will be pushed to repos today Due last Thursday of quarter (5/31) Demo today 2

Socket API: Server TCP Connection Pretty similar to clients, but with additional steps: 1) Figure out the IP address and port on which to listen 2) Create a socket 3) bind() the socket to the address(es) and port 4) Tell the socket to listen() for incoming clients 5) accept() a client connection 6).read() and write() to that connection 7) close() the socket 3

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 Server reassigns client connections to different internal ports to differentiate 4

Steps 1-2 Step 1: getaddrinfo() invocation may or may not be needed Do you know your IP address(es) already? Static vs. dynamic IP address allocation Can request all local IP addresses by passing NULL as hostname and setting AI_PASSIVE in hints.ai_flags 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 needed yet 5

Step 3: Bind the socket int bind(int sockfd, const struct sockaddr* addr, socklen_t addrlen); 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) 6

Step 4: Listen for Incoming Clients int listen(int sockfd, int backlog); 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 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 7

Pseudocode Time Assume we have set up struct addrinfo hints to get both IPv4 and IPv6 addresses Write pseudocode to bind to and listen on the first socket that works Pieces you can use: Error(); // error msg and exit retval = getaddrinfo(..., &res); freeaddrinfo(res); fd = socket(...); retval = bind(fd,...); retval = listen(fd, SOMAXCONN); close(fd); 8

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) 9

Step 5: Accept a Client Connection int accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen); 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 10

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 Echos any data the client sends to it on stdout 11

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 12

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 13

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 14