CSE 333 Lecture network programming intro

Similar documents
CSE 333 Lecture 16 - network programming intro

CSE 333 Lecture 8 - file and network I/O

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

CSE 333 SECTION 6. Networking and sockets

CSE 333 SECTION 8. Sockets, Network Programming

CSE 333 SECTION 7. Client-Side Network Programming

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

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

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

CSE 333 SECTION 7. Client-Side Network Programming

Client-side Networking

CMPSC 311- Introduction to Systems Programming Module: Network Programming

CMPSC 311- Introduction to Systems Programming Module: Network Programming

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

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

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

CSE 124 Discussion Section Sockets Programming 10/10/17

ECE 435 Network Engineering Lecture 2

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

IPv6 Porting Applications

CLIENT-SIDE PROGRAMMING

Tutorial on Socket Programming

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

ECE 435 Network Engineering Lecture 2

CSE 333 Lecture server sockets

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

A Client-Server Exchange

CSE 333 Lecture server sockets

System Programming. Sockets

CSE 333 Lecture server sockets

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

CS321: Computer Networks Introduction to Application Layer

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

Network programming(i) Lenuta Alboaie

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

Programming with TCP/IP. Ram Dantu

CSE 333 Section 8 - Client-Side Networking

CS 3516: Computer Networks

Socket Programming(2/2)

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

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

Session NM056. Programming TCP/IP with Sockets. Geoff Bryant Process software

UNIX Network Programming. Overview of Socket API Network Programming Basics

CS321: Computer Networks Socket Programming

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

Network Programming Worksheet 2. Simple TCP Clients and Servers on *nix with C.

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

Application Programming Interfaces

CptS 360 (System Programming) Unit 17: Network IPC (Sockets)

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

Lab 0. Yvan Petillot. Networks - Lab 0 1

C Structures in Practice

Category: Informational J. Bound J. McCann Hewlett-Packard W. Stevens February 2003

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Unix Network Programming

Server-side Programming

CSE 333 SECTION 3. POSIX I/O Functions

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

IBM i Version 7.3. Programming Socket programming IBM

SOCKET PROGRAMMING. What is a socket? Using sockets Types (Protocols) Associated functions Styles

Networks class. CS144 Introduction to Computer Networking. Administrivia. Administrivia 2. Grading. Labs. Goal: Teach the concepts underlying networks

CS 640: Computer Networking

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

Network Socket Programming - 2 BUPT/QMUL

CSc 450/550 Computer Networks Network Architectures & Client-Server Model

Writing Network Applications using the TCP/IP Protocol Stack: Socket Programming

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

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

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

TCP/IP Sockets in C: Practical Guide for Programmers. Computer Chat. Internet Protocol (IP) IP Address. Transport Protocols. Ports

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

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme

TCP Network Programming in C

Introduction to Client-Server Model

Socket Programming for TCP and UDP

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

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

Network Socket Programming - 3 BUPT/QMUL

Internet applications

UNIT 1 TCP/IP PROGRAMMING CONCEPTS

Network Socket Programming - 3 BUPT/QMUL

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

Piotr Mielecki Ph. D.

Computer Network Programming

ICT 6544 Distributed Systems Lecture 5

Christian Tschudin (basierend auf einem Foliensatz von C. Jelger und T. Meyer) Departement Mathematik und Informatik, Universität Basel

Socket Programming TCP UDP

Elementary TCP Sockets

Lecture 7 Overview. IPv6 Source: Chapter 12 of Stevens book Chapter 31 of Comer s book

CSE 333 SECTION 3. POSIX I/O Functions

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

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2)

Client Server Computing

CS 326: Operating Systems. Networking. Lecture 17

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

EECS122 Communications Networks Socket Programming. Jörn Altmann

Server-side Programming

Introduction to Lab 2 and Socket Programming. -Vengatanathan Krishnamoorthi

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

Transcription:

CSE 333 Lecture 17 -- network programming intro Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Administrivia HW3 due Thursday night HW4 out Friday morning - Due last Thursday of the quarter - OK to use up to 2 late days if you still have them Section this week: networking (DNS, TCP client side, ) No more exercises due this week - Next exercise out Thur. afternoon, due Mon. morning

Today Network programming - dive into the Berkeley / POSIX sockets API

Files and file descriptors Remember open, read, write, and close? - POSIX system calls for interacting with files - open( ) returns a file descriptor an integer that represents an open file inside the OS, it s an index into a table that keeps track of any state associated with your interactions, such as the file position you pass the file descriptor into read, write, and close

Networks and sockets UNIX likes to make all I/O look like file I/O - the good news is that you can use read( ) and write( ) to interact with remote computers over a network! - just like with files... your program can have multiple network channels open at once you need to pass read( ) and write( ) a file descriptor to let the OS know which network channel you want to write to or read from - a file descriptor used for network communications is a socket

Pictorially 128.95.4.33 Web server fd 5 fd 8 fd 9 fd 3 file descriptor OS s descriptor table type connected to? 0 pipe stdin (console) 1 pipe stdout (console) 2 pipe stderr (console) 80 80 3 TCP socket local: 128.95.4.33:80 remote: 44.1.19.32:7113 index.html pic.png Internet 5 file index.html 8 file pic.png 9 TCP socket local: 128.95.4.33:80 remote: 102.12.3.4:5544 client client 102.12.3.4 : 5544 44.1.19.32 : 7113

Types of sockets Stream sockets - for connection-oriented, point-to-point, reliable bytestreams uses TCP, SCTP, or other stream transports Datagram sockets - for connection-less, one-to-many, unreliable packets uses UDP or other packet transports Raw sockets - for layer-3 communication (raw IP packet manipulation)

Stream sockets Typically used for client / server communications - but also for other architectures, like peer-to-peer client server 1. establish connection Client - an application that establishes a connection to a server client 2. communicate server Server - an application that receives connections from clients client 3. close connection server

Datagram sockets 1. create socket host Used less frequently than stream sockets - they provide no flow control, ordering, or reliability Often used as a building block host 1. create socket host 1. create socket host host - streaming media applications - sometimes, DNS lookups host 2. communicate host host

The sockets API Berkeley sockets originated in 4.2 BSD Unix circa 1983 - it is the standard API for network programming available on most OSs POSIX socket API - a slight updating of the Berkeley sockets API a few functions were deprecated or replaced better support for multi-threading was added

Let s dive into it! We ll start by looking at the API from the point of view of a client connecting to a server over TCP - there are five steps: 1. figure out the IP address and port to which to connect 2. create a socket 3. connect the socket to the remote server 4. read( ) and write( ) data using the socket 5. close the socket

Connecting from a client to a server. Step 1. Figure out the IP address and port to which to connect.

Network addresses For IPv4, an IP address is a 4-byte tuple - e.g., 128.95.4.1 (80:5f:04:01 in hex) For IPv6, an IP address is a 16-byte tuple - e.g., 2d01:0db8:f188:0000:0000:0000:0000:1f33 2d01:0db8:f188::1f33 in shorthand

IPv4 address structures // Port numbers and addresses are in *network order*. // A mostly-protocol-independent address structure. struct sockaddr { short int sa_family; // Address family; AF_INET, AF_INET6 char sa_data[14]; // 14 bytes of protocol address }; // An IPv4 specific address structure. struct sockaddr_in { short int sin_family; // Address family, AF_INET == IPv4 unsigned short int sin_port; // Port number struct in_addr sin_addr; // Internet address unsigned char sin_zero[8]; // Same size as struct sockaddr }; struct in_addr { uint32_t s_addr; // IPv4 address };

IPv6 address structures // A structure big enough to hold either IPv4 or IPv6 structures. struct sockaddr_storage { sa_family_t ss_family; // address family }; // a bunch of padding; safe to ignore it. char ss_pad1[_ss_pad1size]; int64_t ss_align; char ss_pad2[_ss_pad2size]; // An IPv6 specific address structure. struct sockaddr_in6 { u_int16_t sin6_family; // address family, AF_INET6 u_int16_t sin6_port; // Port number u_int32_t sin6_flowinfo; // IPv6 flow information struct in6_addr sin6_addr; // IPv6 address u_int32_t sin6_scope_id; // Scope ID }; struct in6_addr { unsigned char s6_addr[16]; // IPv6 address };

Generating these structures Often you have a string representation of an address - how do you generate one of the address structures? #include <stdlib.h> #include <arpa/inet.h> genaddr.cc int main(int argc, char **argv) { struct sockaddr_in sa; // IPv4 struct sockaddr_in6 sa6; // IPv6 // IPv4 string to sockaddr_in. inet_pton(af_inet, "192.0.2.1", &(sa.sin_addr)); // IPv6 string to sockaddr_in6. inet_pton(af_inet6, "2001:db8:63b3:1::3490", &(sa6.sin6_addr)); } return EXIT_SUCCESS;

Generating these structures How about going in reverse? #include <stdlib.h> #include <arpa/inet.h> #include <iostream> genstring.cc int main(int argc, char **argv) { struct sockaddr_in6 sa6; // IPv6 char astring[inet6_addrstrlen]; // IPv6 // IPv6 string to sockaddr_in6. inet_pton(af_inet6, "2001:db8:63b3:1::3490", &(sa6.sin6_addr)); // sockaddr_in6 to IPv6 string. inet_ntop(af_inet6, &(sa6.sin6_addr), astring, INET6_ADDRSTRLEN); std::cout << astring << std::endl; } return EXIT_SUCCESS;

DNS People tend to use DNS names, not IP addresses - the sockets API lets you convert between the two - it s a complicated process, though: a given DNS name can have many IP addresses many different DNS names can map to the same IP address an IP address will reverse map into at most one DNS name, and maybe none a DNS lookup may require interacting with many DNS servers You can use the dig Linux program to explore DNS - man dig

DNS hierarchy. -- root name servers. 198.41.0.4 (a.root-servers.net) 199.9.14.201 (b.root-servers.net) 202.12.27.33 (m.root-servers.net) com xxx uk org.com. -- top-level domain server google yahoo hulu Gribble apache fsf www mail docs finance seattle www

Resolving DNS names The POSIX way is to use getaddrinfo( ) - a pretty complicated system call; the basic idea... set up a hints structure with constraints you want respected e.g., IPv6, IPv4, or either tell getaddrinfo( ) which host and port you want resolved host: a string representation; DNS name or IP address getaddrinfo( ) gives you a list of results packet in an addrinfo struct free the addrinfo structure using freeaddrinfo( )

DNS lookup example see dnsresolve.cc

Connecting from a client to a server. Step 2. Create a socket.

Creating a socket Use the socket system call - creating a socket doesn t yet bind it to a local address or port #include <errno.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <iostream> socket.cc int main(int argc, char **argv) { int socket_fd = socket(pf_inet, SOCK_STREAM, 0); if (socket_fd == -1) { std::cerr << strerror(errno) << std::endl; return EXIT_FAILURE; } close(socket_fd); return EXIT_SUCCESS; }

Connecting from a client to a server. Step 3. Connect the socket to the remote server.

connect( ) The connect( ) system call establishes a connection to a remote host - you pass the following arguments to connect( ): the socket file descriptor you created in step 2 one of the address structures you created in step 1 - connect may take some time to return it is a blocking call by default the network stack within the OS will communicate with the remote host to establish a TCP connection to it this involves ~2 round trips across the network

connect example see connect.cc

Connecting from a client to a server. Step 4. read( ) and write( ) data using the socket.

read( ) By default, a blocking call - if there is data that has already been received by the network stack, then read will return immediately with it thus, read might return with less data than you asked for - if there is no data waiting for you, by default read( ) will block until some arrives pop quiz: how might this cause deadlock?

write( ) By default, a blocking call - but, in a more sneaky way - when write( ) returns, the receiver (i.e., the other end of the connection) probably has not yet received the data in fact, the data might not have been sent on the network yet! write( ) enqueues your data in a send buffer in the OS, and then returns; the OS will transmit the data in the background - if there is no more space left in the send buffer, by default write( ) will block how might this cause deadlock?

read/write example see sendreceive.cc

Connecting from a client to a server. Step 5. close( ) the socket.

See you on Friday!

Exercise 1 Write a client that: - reads DNS names, one per line, from stdin - translates each name to one or more IP addresses - prints out each IP address to stdout, one per line