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

Similar documents
CSE 333 SECTION 7. Client-Side Network Programming

CSE 333 SECTION 8. Sockets, Network Programming

CSE 333 SECTION 6. Networking and sockets

CSE 333 SECTION 7. Client-Side Network Programming

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

CSE 333 Lecture 16 - network programming intro

CSE 333 Lecture network programming intro

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 Lecture 8 - file and network I/O

CSE 333 Section 8 - Client-Side Networking

Client-side Networking

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

CSE 124 Discussion Section Sockets Programming 10/10/17

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

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

CLIENT-SIDE PROGRAMMING

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

IPv6 Porting Applications

CMPSC 311- Introduction to Systems Programming Module: Network Programming

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

CMPSC 311- Introduction to Systems Programming Module: Network Programming

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

Tutorial on Socket Programming

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

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

TCP: Three-way handshake

UNIX System Programming Lecture 19: IP Sockets

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Socket Programming(2/2)

Network programming(i) Lenuta Alboaie

Socket Programming TCP UDP

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

Introduction to Lab 2 and Socket Programming. -Vengatanathan Krishnamoorthi

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

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

Azblink API for Sending XMPP Messages via HTTP POST

System Programming. Sockets

Application Programming Interfaces

Server-side Programming

Programming with TCP/IP. Ram Dantu

Computer Network Programming

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

CS 3516: Computer Networks

Unix Network Programming

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

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

Network Socket Programming - 3 BUPT/QMUL

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

CS321: Computer Networks Socket Programming

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

Network Socket Programming - 3 BUPT/QMUL

UNIX Network Programming. Overview of Socket API Network Programming Basics

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

Introduction to Socket Programming

ECE 435 Network Engineering Lecture 2

Server-side Programming

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

CS 640: Computer Networking

Redesde Computadores(RCOMP)

CS321: Computer Networks Introduction to Application Layer

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

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

Piotr Mielecki Ph. D.

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

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

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

C Structures in Practice

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

A Client-Server Exchange

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

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

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

Elementary TCP Sockets

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

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

#include <dirent.h> int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen); DIR *opendir(const char *name);

5. System Calls or Bust

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

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

ECE 435 Network Engineering Lecture 2

Communication. Sockets (Haviland Ch. 10)

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

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

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

Socket Programming for TCP and UDP

Lab 0. Yvan Petillot. Networks - Lab 0 1

CSCI 4061: Sockets and Network Programming

Network Programming: Part I

Internet applications

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

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

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

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

Programming guidelines on transition to IPv6

Network Socket Programming - 2 BUPT/QMUL

UNIT IV- SOCKETS Part A

Transcription:

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

Overview Virtual functions summary and worksheet Domain Name Service (DNS) Client side network programming steps and calls dig and ncat tools

C++ virtual functions Dynamic dispatch decide at runtime what code to invoke Most derived function gets called The virtual keyword is sticky vtable Function pointers to each virtual function of the class Pointers to most derived function for that class vptr virtual table pointer for each object instance

Section Exercise 1 (5 min) Virtual functions worksheet

Network Programming Application Presentation Session Transport Network Data Physical Sender OSI model Application Presentation Session Transport Network Data Physical Receiver

Berkeley/POSIX Sockets API What is a socket? Endpoint or an interface for sending and receiving data at a node You can use read() and write() to send and receive data The socket() system call creates a socket and returns a file descriptor

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: 10.12.3.4:5544 client client 10.12.3.4 : 5544 44.1.19.32 : 7113

Network programming for the client side The five steps to communicate with a server: 1. Domain name lookup to figure out IP address and port to talk to 2. Create a socket 3. Connect to the server 4. Read and Write to transfer data through the socket 5. Close the socket

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

DNS Domain Name System/Service A hierarchical distributed naming system any resource connected to the Internet or a private network. Resolves queries for names into IP addresses. The sockets API lets you convert between the two. Aside: getnameinfo() is the inverse of getaddrinfo() Is on the application layer on the Internet protocol suite.

Dig demo dig +trace attu.cs.washington.edu

Resolving DNS names The POSIX way is to use getaddrinfo( ). Set up a hints structure with constraints, 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 in an addrinfo struct.

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

getaddrinfo() and structures int getaddrinfo(const char *hostname, // hostname to look up const char *servname, // service name const struct addrinfo *hints, // desired output type struct addrinfo **res); // result structure // Hints and results take the same form. Hints are optional. struct addrinfo { int ai_flags; // Indicate options to the function int ai_family; // AF_INET, AF_INET6, or AF_UNSPEC int ai_socktype; // Socket type, (use SOCK_STREAM) int ai_protocol; // Protocol type size_t ai_addrlen; // INET_ADDRSTRLEN, INET6_ADDRSTRLEN struct sockaddr *ai_addr; // Address (input to inet_ntop) char *ai_canonname; // canonical name for the host struct addrinfo *ai_next; // Next element (It s a linked list) }; // Converts an address from network format to presentation format const char *inet_ntop(int af, // family (see above) const void * restrict src, // in_addr or in6_addr char * restrict dest, // return buffer socklen_t size); // length of buffer

Generating these structures #include <stdlib.h> #include <arpa/inet.h> 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 #include <stdlib.h> #include <arpa/inet.h> 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); printf( %s\n, astring); return EXIT_SUCCESS;

DNS Resolution Demo dnsresolve.cc

Network programming for the client side Recall the five steps, here are the corresponding calls: 1. getaddrinfo() to figure out IP address and port to talk to 2. socket() for creating a socket 3. connect() to connect to the server 4. read() and write() to transfer data through the socket 5. close() to close the socket

Network programming for the client side Recall the five steps, here s the corresponding calls: 1. getaddrinfo() to figure out IP address and port to talk to 2. socket() for creating a socket 3. connect() to connect to the server 4. read() and write() to transfer data through the socket 5. close() to close the socket

socket() Create the socket #include <sys/types.h> #include <sys/socket.h> int socket(int domain, // e.g. AF_NET, AF_NET6 int type, // e.g. SOCK_STREAM, SOCK_DGRAM int protocol); // Usually 0 Note that socket() just creates a socket, it isn t bound yet to a local address.

Demo socket.cc

Network programming for the client side Recall the five steps, here s the corresponding calls: 1. getaddrinfo() to figure out IP address and port to talk to 2. socket() for creating a socket 3. connect() to connect to the server 4. read() and write() to transfer data through the socket 5. close() to close the socket

connect() Establish the connection #include <sys/types.h> #include <sys/socket.h> int connect(int sockfd, // socket fd from step 2 struct sockaddr *serv_addr, // server info // from step 1 int addrlen); // size of serv_addr struct

Demo (Along with ncat demo) connect.cc (nc lv 5454 to create listener)

Network programming for the client side Recall the five steps, here s the corresponding calls: 1. getaddrinfo() to figure out IP address and port to talk to 2. socket() for creating a socket 3. connect() to connect to the server 4. read() and write() to transfer data through the socket 5. close() to close the socket

read() and write() By default, both are blocking calls read() will wait for some data to arrive, then immediately read whatever data has been received by the network stack Might return less data read than asked for Blocks while data isn t received Conversely, write() queues your data to OS s send buffer, then returns while OS does the rest in the background When write returns the receiver probably hasn t received the data yet When the send buffer fills up, write() will also block

Demo (Along with more ncat) sendreceive.cc (nc l 5454 to create listener)

Network programming for the client side Recall the five steps, here s the corresponding calls: 1. getaddrinfo() to figure out IP address and port to talk to 2. socket() for creating a socket 3. connect() to connect to the server 4. read() and write() to transfer data through the socket 5. close() to close the socket

close() Close the connection #include <unistd.h> int close(int sockfd); Remember to close the socket when you re done!

Network programming for the client side Recall the five steps, here s the corresponding calls: 1. getaddrinfo() to figure out IP address and port to talk to 2. socket() for creating a socket 3. connect() to connect to the server 4. read() and write() to transfer data through the socket 5. close() to close the socket

Section Exercise The TA has set up a game server for you to communicate with (gameserver.py) Using the sample client code from section/lecture and what you know about I/O calls in C++, your job is to implement a C++ client called gameclient.cc such that you can communicate with the game server much like you can with the netcat tool