Client-Side Network Programming

Slides:



Advertisements
Similar presentations
IPv6 Intro & Demo Yuxin Ouyang Yuhui Feng. Why IPv6 Space allowed by IPv4 is saturating IPv4 is not secure by itself Data prioritization in IPv4 is not.
Advertisements

CSE 333 – SECTION 8 Networking and sockets. Overview Network Sockets IP addresses and IP address structures in C/C++ DNS – Resolving DNS names Demos.
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
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.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Tutorial 8 Socket Programming
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
1 Advanced Name and Address Conversions getaddrinfo, getnameinfo, gai_strerror, freeaddrinfo host_serv, tcp_connect, tcp_listen, udp_client, udp_connect,
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE453 – Introduction to Computer Networks Lecture 15 – Transport Layer (II)
Assignment 3 A Client/Server Application: Chatroom.
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.
Class A Addresses The 1 st bit of a class A address is 0 The 1 st byte has a value from (128.x.x.x would not be a class A) 127.x.x.x is reserved.
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.
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
TELE 402 Lecture 6: Name and address conversions 1 Overview Last Lecture –Socket Options and elementary UDP sockets This Lecture –Name and address conversions.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
Socket Programming(2/2) 1. Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Chapter 11 Advanced Name and Address Conversion. Introduction gethostbyname, gethostbyaddr: protocol dependent getaddrinfo: –a function providing protocol.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
CSE 333 – SECTION 8 Client-Side Network Programming.
UNIX Sockets COS 461 Precept 1.
Socket Programming (Cont.)
CS 105 “Tour of the Black Holes of Computing”
Class A Addresses The 1st bit of a class A address is 0
Network Programming CSC- 341
CS 105 “Tour of the Black Holes of Computing!”
Week 13 - Friday CS222.
Socket Programming in C
Network Programming with Sockets
Tutorial on Socket Programming
Client-Side Network Programming
Transport layer API: Socket Programming
Chapter 3 Sockets Introduction
Client-side Networking CSE 333 Spring 2018
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
ECS152b Behrooz Khorashadi
IP Addresses, DNS CSE 333 Spring 2018
Lecture 2 Socket Programming
IP Addresses, DNS CSE 333 Summer 2018
Client-side Networking CSE 333 Summer 2018
IP Addresses, DNS CSE 333 Autumn 2018
Network Programming: Part I CSCI 380: Operating Systems
Client-side Networking CSE 333 Autumn 2018
IP Addresses, DNS CSE 333 Winter 2019
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Client-side Networking CSE 333 Winter 2019
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Today’s topic: Basic TCP API
Client-side Networking CSE 333 Spring 2019
Presentation transcript:

Client-Side Network Programming CSE 333 – Section 8 Client-Side Network Programming

Overview Domain Name Service (DNS) Review Client side programming steps and calls dig and ncat tools

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

Network programming for the client side Recall the five steps, here’s the corresponding calls: getaddrinfo() to figure out IP address and port to talk to socket() for creating a socket connect() to connect to the server read() and write() to transfer data through the socket close() to 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. Break down attu.cs.washington.edu Unefficient so usually device remembers IP addresses for DNS services that again end in similar endings

Dig demo ANSWER – IP addresses AUTHORITY – Name servers it knows of? www.google.com attu.cs.washington.edu dig +trace www.google.com - shows how it goes through the heirarchy dig –x 128.208.1.138

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 Network sockets are network interfaces Endpoints in an interprocess communication flow Socket address = IP address + port number

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 #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; }

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 char *ai_cananname; // canonical name for the host struct sockaddr *ai_addr; // Address (input to inet_ntop) 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, // sockaddr char * restrict dest, // return buffer socklen_t size); // length of buffer Demo: dnsresolve.cc

Demo dnsresolve.cc

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

socket() – Create the socket #include <sys/types.h> #include <sys/socket.h> int socket(int domain, // e.g. PF_NET, PF_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. domain - the protocol family type - the socket type protocol - the protocol type, - Normally only a single protocol exists to support a particular socket type within a given protocol family, in which case protocol can be specified as 0. However, it is possible that many protocols may exist, in which case a particular protocol must be specified in this manner.

Demo socket.cc

Network programming for the client side Recall the five steps, here’s the corresponding calls: getaddrinfo() to figure out IP address and port to talk to socket() for creating a socket connect() to connect to the server read() and write() to transfer data through the socket 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 May be a bit slow, it’s a blocking call by default so it’ll communicate with the host to establish a TCP connection to it. – involves two round trips across the network IF server is listening, connect() will wait, but if the server isn’t listening it’ll fail immediately

Demo (Along with ncat demo) connect.cc Sample transcript: In attu4, run the command, nc –lv 7777 the -l means that I’m going to bind and listen from this port number the -v means verbose, to show connect.cc in action! connect to some port that isn’t open to connect On your local machine (preferably), compile and run connect.cc, ./connect attu4.cs.washington.edu 7777

Pictorially Web server OS’s descriptor table fd 5 fd 8 fd 9 fd 3 file descriptor type connected to? pipe stdin (console) 1 stdout (console) 2 stderr (console) 3 TCP socket local: 128.95.4.33:80 remote: 44.1.19.32:7113 5 index.html 8 pic.png 9 remote: 102.12.3.4:5544 128.95.4.33 Web server fd 5 fd 8 fd 9 fd 3 80 80 pic.png index.html How the descriptor table looks like after sockets are made and set up. This is from the serverside Internet client client 10.12.3.4 : 5544 44.1.19.32 : 7113

Network programming for the client side Recall the five steps, here’s the corresponding calls: getaddrinfo() to figure out IP address and port to talk to socket() for creating a socket connect() to connect to the server read() and write() to transfer data through the socket 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() enqueues your data to OS’ 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 Points to make: Using nc –l here instead of nc –lv because the server will actually be receiving data from the client, once after sendreceive excutes nc –l closes automatically if the client closes Another thing about ncat, running nc [hostname] [port] will create a continuous connection to the given server, which you can send messages to, for example: nc www.google.com 80 Type the following HTTP request and press enter twice: GET / HTTP/1.1 Or do this:, listen on a port (with –k flag as well), go to that website on browser, return the following: HTTP/1.0 200 OK Content-Type: text/html <h1> What’s up </h1> <a href=“http://www.cs.washington.edu”>CSE Home Page</a> (ctrl-D)

Network programming for the client side Recall the five steps, here’s the corresponding calls: getaddrinfo() to figure out IP address and port to talk to socket() for creating a socket connect() to connect to the server read() and write() to transfer data through the socket 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! May be a bit slow, it’s a blocking call by default so it’ll communicate with the host to establish a TCP connection to it. – involves two round trips across the network