Network Programming Chapter 12

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Chris Riesbeck, Fall 2007 Network Programming Topics –Sockets interface –Writing clients and servers.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
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.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
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
Network Programming Nov 21, 2002 Topics Programmer’s view of the Internet (review) Sockets interface Writing clients and servers class26.ppt “The.
Introduction to Project 1 Web Client and Server Jan 2006.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Recitation 12: 11/25/02 Outline Socket Interface –Echo Client/Server Http Protocol Evaluation Annie Luo Office Hours: Thursday.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
UNIX Socket Programming CS 6378
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
Operating Systems Chapter 9 Distributed Communication.
Elementary TCP Sockets
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Computer Systems II CSC 2405 Network Programming.
Sirak Kaewjamnong Computer Network Systems
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.
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Socket address structures Byte ordering IP address conversion
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Networking Programming A Tiny Web Server 1. Outline Review of Web Server The Tiny Web Server Some Practical Issues Suggested Reading: –11.5~
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 Networking Programming. 2 Outline Connection & Socket Sockets Interface –Functions –Echo Client and Server Suggested Reading: –11.4.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
CSE 333 – SECTION 8 Client-Side Network Programming.
Client-Server model. Socket programming 
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
Socket Programming (Cont.)
Instructors: Anthony Rowe, Seth Goldstein and Greg Kesden
Network Programming /18-243: Introduction to Computer Systems
CS 105 “Tour of the Black Holes of Computing”
Network Programming CSC- 341
Network Programming April 11, 2008
Chapter4 Elementary TCP Socket
Network Programming with Sockets
Tutorial on Socket Programming
Network programming Nov 27, 2001
Transport layer API: Socket Programming
Recitation 11 – 4/29/01 Outline Sockets Interface
Advanced Network Programming spring 2007
Networking Programming
Tutorial on Socket Programming
Network Programming: Part I CSCI 380: Operating Systems
Network Programming November 3, 2008
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Today’s topic: Basic TCP API
Lecture 23: Networking and IP
Presentation transcript:

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