Network Programming Chapter 12 Lecture 12 Network Programming Chapter 12 2018/12/3
Review Client Server Programming Model Networks Global IP Internet Socket Interface Web Servers 2018/12/3
Client Server Programming Model When a client needs service, it initiates a transaction The server receives the request, interprets and manipulates The server sends a response to the client and waits for the next request The client receives the response and manipulates it. 1. client sends request 2. server processes request Client process server process resource 4. client processes response 3. server sends response 2018/12/3
Hardware and Software Organisations Internet Client Host Internet Server Host Server Client user code TCP/IP TCP/IP Kernel code Network Adaptor Network Adaptor Hardware code 2018/12/3
Global IP Internet The previous page shows the basic hardware and software organisation of an Internet client-server application. Each Internet Host runs software that implements TCP/IP protocol Internet clients and servers communicates using a mix of sockets interface functions and Unix I/O functions 2018/12/3
Programmer’s perspective on Internet The set of hosts is mapped to a set of 32-bit IP addresses The set of IP addresses is mapped to a set of identifiers called Internet domain names A process on one Internet host can communicate with a process on any other Internet host over a connection 2018/12/3
Internet Address Structure An IP address is an unsigned 32-bit integer as follows: (addresses in IP address structures are always stored in big endian, while the host byte order is little endian.) /* Internet Address Structure */ struct in_addr { unsigned int s_addr; /*network byte order */ }; 2018/12/3
Big endian and little endian Little endian Byte3 Byte2 Byte1 Byte0 will be arranged in memory as follows: Base Address+0 Byte0 Base Address+1 Byte1 Base Address+2 Byte2 Base Address+3 Byte3 Host Big endian is: Base Address+0 Byte3 Base Address+1 Byte2 Base Address+2 Byte1 Base Address+3 Byte0 IP address 2018/12/3
IP dotted decimal notation We can use ipconfig under DOS to determine your PC’s IP address in dotted notation. 2018/12/3
aton and ntoa (aton: application to network, a sub-routine under Unix Hex Address Dotted decimal address 0x01 0.0.0.1 0xffffffff 255.255.255.255 0x0f0f0f00 16.16.16.0 #include <arpa/inet.h> Int inet_aton(const char *cp, struct in_addr *inp); Char *inet_ntoa(struct in_addr in); 2018/12/3
Program that converts hex arguments to a dotted-decimal #include “caspp.h” void main (int argc, char **argv) { Struct in_addr inaddr /* addr in network byte order */ unsigned int addr; sscanf(argv[1], “%x”, &addr); inaddr.s_addr = htonl(addr); printf(“%s\n”, inet_ntoa(inaddr)); } 2018/12/3
Internet Domain Names 2018/12/3 /* DNS entry structure */ Struct hostnet { char *h_name; /* official domain name of host */ char **h_aliases /* null-terminated array of domain name */ int h_addrtype; /* host address */ Int h_length; /* length of an address in bytes */ char **h_addr_list; /*null terminated array of in_addr structs */ }; 2018/12/3
Internet Connection Internet Clients and severs communicate by sending and receiving streams of bytes over connection. A connection is point-to-point. A connection is full duplex in the sense that data can flow in both directions. A socket is an end point connection. 2018/12/3
Socket Connection Server Client server socket address 201.12.13.14:80 Client socket address 121.2.3.4:12345 Server Client server host address 201.12.13.14 client host address 121.2.3.4 Each socket has a corresponding socket address that consists of IP address and 16-bit integer port. It is denoted by address: port (such as address:port 121.2.3.4:12345) 2018/12/3
Socket Interface Socket Socket bind listen Connect accept close close open_clientfd open_listenfd listen connection request Connect accept Rio_written Rio_readlineb await connection request from next client Rio_readlineb Rio_written EOF close Rio_readlineb close 2018/12/3
Socket Address Structures /* Generic socket address structure (for connect, bind and accept) */ Struct sockaddr { Unsigned short sa_family; Char sa_data[14]; }; /* Internet-style socket address structure */ struct sockaddr_in { Unsigned short sin_family; /* address family (always AF_INET) */ Unsigned short sin_port; /*port number in network byte order */ Struct in_addr sin_addr; /* Ip address in network byte order */ Unsigned cahr sin_zero[8]; /* pad to sizeof(struct sockaddr) */ 2018/12/3
Socket Function 2018/12/3 /* Socket descriptor */ #include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, int protocol); /* return nonegative is okay */ /* Connect Function */ #include <sys/socket.h> Int connect (int sockfd, struct sockaddr *serv_addr, int addrlen); /* return o if okay or -1 */ 2018/12/3
Socket Function /* open_clientfd function */ #include “csapp.h” Int open_clientfd(char *hostname, int port); /* return -1 on Unix error, -2 on DNS error */ /* bind function */ #include <sys/socket.h> Int bind (int sockfd, struct sockaddr *my_addr, int addrlen); /* return 0 is okay, -1 is error */ 2018/12/3
Socket Function /* listen function #include <sys/socket.h> int listen (int sockfd, int backlog); /* return -1 on Unix error, -2 on DNS error */ /* accept function */ #include <sys/socket.h> int accept (int listenfd, struct sockaddr, *addr, int *addrlen); /* return -1 on Unix error, -2 on DNS error */ 2018/12/3
Role of The listening and Connected Descriptors listenfd(3) 1. server blocks in accpet. waiting for connection request Client Server clientfd listenfd(3) Client Server 2. client makes connection request by calling and blocking in connect clientfd listenfd(3) 3. server returns connfd from acept. Client returns from connect. Client Server 2018/12/3 clientfd connfd(4)
Web Server – Web Basics Web basics: Web clients and servers interact using a text-based application –level protocol known as HTTP. A Web client (IE) opens an Internet Connection to a server and requests some content. The server responds with the requested content and then close the connection. 2018/12/3
Web Server – Web Content To Web clients and servers, content is a sequence of bytes with an associated (Multipurpose Internet Mail Extensions) type. Some examples are: MIME Type Description Text/html HTML pages Text/plain Unformatted text Application/postscript Postscript document Image/gif Binary image in GIF format 2018/12/3
HTTP Transactions - HTTP Request http://www. perldoc. com/perl5 HTTP::Request is a class encapsulating HTTP style requests It consists of a request line, some headers, and some (potentially empty) content. $ua = LWP::UserAgent->new; $request = HTTP::Request->new(GET => 'http://www.oslonett.no/'); $response = $ua->request($request); 2018/12/3
HTTP Transactions - HTTP Response The HTTP:: Response class encapsulates HTTP style responses. A response consists of a response line, some headers, and (potentially empty) content. #... $response = $ua->request($request) if ($response->is_success) { print $response->content; } else { print $response->error_as_HTML; } 2018/12/3
How Does the Client Pass Program Arguments to the server? For example, GET /cgi-bin/adder?15000&213 HTTP/1.1 It uses GET filename: /cgi-bin/adder ? Is used to separate the file name from the arguments & each argument is separated by a ‘&’ Space is represented by %20 2018/12/3
How does the server pass arguments to the child After a server receives a request such as: GET /cgi-bin/adder?15000&213 HTTP/1.1 The server fork (creates) a child process and calls execve to run /cgi-bin/adder program Adder is a CGI programs Before the call to execv, the child process sets the CGI environment variable QUERY_STRING to “15000&213” 2018/12/3
How does the child send its output? A CGI program send its dynamic content to the standard output Before the child process loads and runs the CGI program, it uses the Unix dup2 function to redirect standard output to the connected descriptor. Next slide shows a simple program that sums its two arguments and returns an HTML file with the result to the client. 2018/12/3
Program of CGI program 2018/12/3 #include “csapp.h” Int main(void) { Char *buf, *p; Char arg1[MAXLINE], arg2[MAXLINE], content[MAXLINE]; Int n1 = 0, n2 = 0; If ((buf =getenv(“QUERY_STRING”)) !=NULL) { P = strchr(buf, ‘&’); *p = ‘\0’; strcpy(arg1, buf); strcpy(arg2, p+1); n1 = atoi(arg1); n2 = atoi(arg2); sprint(content, “The answer is %d + %d = %d\r\n<p>”, content, n1, n2, n1 + n2); ….. } 2018/12/3
Summary Every network application is a client-server model. With this model, an application consists of a server and one or more clients. Client and servers communicate over the Internet Each Internet host has a unique 32-bit name called IP address The set of IP address is mapped to a set of Internet domain names Processes on different Internet hosts can communicate with each other over connections Clients and servers establish connections by using the sockets interface Web servers and their clients communicate with each other using the HTTP protocol. 2018/12/3