CS321: Computer Networks Introduction to Application Layer

Similar documents
CS321: Computer Networks Socket Programming

UNIX Network Programming. Overview of Socket API Network Programming Basics

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

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

CS 3516: Computer Networks

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

ECE 435 Network Engineering Lecture 2

Network Socket Programming - 3 BUPT/QMUL

Network Socket Programming - 3 BUPT/QMUL

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

A Client-Server Exchange

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

Tutorial on Socket Programming

ECE 435 Network Engineering Lecture 2

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

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1. Interprocess Communication (IPC) Work Individually (no groups)

Elementary TCP Sockets

ICT 6544 Distributed Systems Lecture 5

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

CLIENT-SIDE PROGRAMMING

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

CS 640: Computer Networking

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

Lab 0. Yvan Petillot. Networks - Lab 0 1

Socket Programming TCP UDP

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

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

Unix Network Programming

Introduction to Socket Programming

Introduction to Client-Server Model

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

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

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

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

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

EECS122 Communications Networks Socket Programming. Jörn Altmann

WinSock. What Is Sockets What Is Windows Sockets What Are Its Benefits Architecture of Windows Sockets Network Application Mechanics

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

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

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

TCP: Three-way handshake

CSE 124 Discussion Section Sockets Programming 10/10/17

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

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

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

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

The User Datagram Protocol

Network Communication

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

Network programming(i) Lenuta Alboaie

Introduction to Socket Programming

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

Socket Programming(2/2)

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

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

UDP CONNECT TO A SERVER

System Programming. Sockets

How do we Communicate? Introduction to Unix Network Programming. What does Alice do? What does Bob do? Two simplest networking programs

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

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

Programming with TCP/IP. Ram Dantu

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

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

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

CSE 333 SECTION 6. Networking and sockets

Interprocess Communication Mechanisms

Network Programming Week #1. K.C. Kim

Topics for this Week

Socket Programming for TCP and UDP

Internet applications

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

CSE 333 Lecture 16 - network programming intro

Applications and Layered Architectures. Chapter 2 Communication Networks Leon-Garcia, Widjaja

Client Server Computing

TCP Network Programming in C

Distributed Systems. 02. Networking. Paul Krzyzanowski. Rutgers University. Fall 2017

Computer Networks Prof. Ashok K. Agrawala

Interprocess Communication Mechanisms

IPv6 Porting Applications

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

Piotr Mielecki Ph. D.

CSE 333 SECTION 8. Sockets, Network Programming

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

C Structures in Practice

Networked Applications: Sockets. End System: Computer on the Net

Sockets 15H2. Inshik Song

Application Architecture

CS321: Computer Networks FTP, TELNET, SSH

Ken French HELP Session 1 CS4514

Network Programming Appendix Network Programming Labs

Network Programming and Protocl Design. Nixu Ltd.

Network Programming November 3, 2008

CSE 333 Lecture network programming intro

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

Transcription:

CS321: Computer Networks Introduction to Application Layer Dr. Manas Khatua Assistant Professor Dept. of CSE IIT Jodhpur E-mail: manaskhatua@iitj.ac.in

Basic Application layer provides services to the user, and takes services from Transport layer. Communication is provided using a logical connection. The actual communication takes place through several physical devices (Alice, R2, R4, R5, R7, and Bob) and channels. 31-03-2017 Dr. Manas Khatua 2

Providing Services Application layer provides services to the user, and takes services from Transport layer. The application layer, however, is somewhat different from other layers. The protocols can be removed from this layer easily as they only receives services from Transport layer but does not provide any service to that layer. The protocols used in the first four layers of the TCP/IP suite need to be standardized and documented. So, normally comes with Operating Systems (OS). There are several application-layer protocols that have been standardized and documented by the Internet authority. E.g., DHCP, SMTP, FTP, HTTP, TELNET 31-03-2017 Dr. Manas Khatua 3

Application-Layer Paradigms We need two application programs to interact with each other. What the relationship should be between these two programs? client-server paradigm peer-to-peer paradigm 31-03-2017 Dr. Manas Khatua 4

Client-Server Paradigm the service provider is an application program, called the server process; it runs continuously, waiting for another application program, called the client process, to make a connection through the Internet and ask for service. The server process must be running all the time; the client process is started when the client needs to receive service. Several traditional services are still using this paradigm, e.g., WWW, HTTP, FTP, SSH, E-mail, and so on. Problems: the server should be a powerful computer there should be a service provider willing to accept the cost and create a powerful server for a specific service 31-03-2017 Dr. Manas Khatua 5

Peer-to-Peer Paradigm There is no need for a server process to be running all the time and waiting for the client processes to connect. The responsibility is shared between peers. E.g.: Internet telephony, BitTorrent, Skype, IPTV Advantages: easily scalable and cost-effective in eliminating the need for expensive servers to be running and maintained all the time Challenges: more difficult to create secure communication between distributed services 31-03-2017 Dr. Manas Khatua 6

Client-Server Programming Runs two processes: a client and a server A client is a running program that initializes the communication by sending a request; A server is another application program that waits for a request from a client. A client program is started and stopped by the user whenever it requires. A service provider continuously runs the server program lifetime of a server is infinite. lifetime of a client is finite. 31-03-2017 Dr. Manas Khatua 7

Application Programming Interface How can a client process communicate with a server process? Let, Computer Programming Uses a computer language to define what to do? Has set of instructions for mathematical operations, string manipulation, input/output operation, etc. Application Programming Set of instructions to talk with the lowest four layers (in OS) instructs to open a connection, send and receive data, close the connection Set of instruction of this kind is API 31-03-2017 Dr. Manas Khatua 8

API Several APIs have been designed for communication Three most common APIs Socket interface Transport Layer Interface (TLI) STREAM 31-03-2017 Dr. Manas Khatua 9

Socket Interface Socket interface started in the early 1980s at UC Berkeley as part of a UNIX environment. The socket interface is a set of instructions that provide communication between the application layer and the OS. The idea of sockets allows us to use the set of all instructions already designed in a programming language for other sources and sinks. For example, in C, C++, or Java, we have several instructions that can read and write data to other sources and sinks; a keyboard (a source), a monitor (a sink), or a file (source and sink). We are adding only new sources and sinks to the programming language without changing the way we send or receive data. 31-03-2017 Dr. Manas Khatua 10

Cont Socket is not a physical entity like them (files, keyboard, etc.); it is an abstraction 31-03-2017 Dr. Manas Khatua 11

Cont Communication between a client process and a server process is communication between two sockets Need a pair of socket addresses for communication: a local socket address and a remote socket address. 31-03-2017 Dr. Manas Khatua 12

Cont A socket address should first define the computer on which a client or a server is running. a computer in the Internet is uniquely defined by its IP address Then, we need another identifier to define the specific client or server involved in the communication an application program can be defined by a port number So, a socket address should be a combination of an IP address and a port number 31-03-2017 Dr. Manas Khatua 13

Finding Socket Addresses How can a client or a server find a pair of socket addresses for communication? In Server Site: The server needs a local (server) and a remote (client) socket address for communication. In Client Site: The client also needs a local (client) and a remote (server) socket address for communication. 31-03-2017 Dr. Manas Khatua 14

Cont In Server Site Local Socket Address (server): Provided by the OS; IP and Port number needs to be defined. For standard services, port numbers are well-known. Remote Socket Address (client): The server can find this socket address from the REQ packet when a client tries to connect to the server. In Client Site Local Socket Address (client): Provided by the OS; The port number is assigned to a client process each time the process needs to start the communication; the ephemeral port numbers are assigned to client Remote Socket Address (server): We know port-number of standard application, but don t know IP. We know only URL (e.g. www.gmail.com), and DNS gives server socket address corresponding to URL. 31-03-2017 Dr. Manas Khatua 15

Use Services of Transport Layer The choice of the transport layer protocol seriously affects the capability of the application processes. use UDP if it is sending small messages if the simplicity and speed is more important for the application than reliability use TCP if it needs to send long messages and require reliability 31-03-2017 Dr. Manas Khatua 16

Iterative Communication Using UDP several client programs can access the same server program at the same time the server program can be designed to respond Iteratively (i.e. one by one) Concurrently In UDP communication, the client and server use only one socket each The socket created at the server site lasts forever; the socket created at the client site is closed (destroyed) when the client process terminates. 31-03-2017 Dr. Manas Khatua 17

Cont Each client is served in each iteration of the loop in the server. Each client sends a single datagram and receives a single datagram. If a client wants to send two datagrams, it is considered as two clients for the server. 31-03-2017 Dr. Manas Khatua 18

Flow 31-03-2017 Dr. Manas Khatua 19

Iterative Communication Using TCP The TCP server uses two different sockets one for connection establishment (listen socket) the other for data transfer (socket) 31-03-2017 Dr. Manas Khatua 20

Flow Diagram 31-03-2017 Dr. Manas Khatua 21

Iterative Socket Programming the role of a socket in communication has no buffer to store data to be sent or received is capable of neither sending nor receiving data acts as a reference or a label buffers and necessary variables are created inside OS The C language defines a socket as a structure. 31-03-2017 Dr. Manas Khatua 22

Cont Family: defines the protocol family (PF). The common values are PF_INET (for current Internet), Type: defines four types of sockets SOCK_STREAM (for TCP), SOCK_DGRAM (for UDP), SOCK_SEQPACKET (for SCTP), SOCK_RAW (for directly use the IP) Protocol: defines the specific protocol in the family (e.g., 0 => TCP/IP) 31-03-2017 Dr. Manas Khatua 23

Cont Local socket address: defines the local socket address Length: size of the socket address Family: AF_INET for TCP/IP; (AF: address family) Port Number: it defines the process IP Address: it defines the host on which the process is running Unused: for future use Remote socket address: defines the remote socket address 31-03-2017 Dr. Manas Khatua 24

Echo application using UDP The client program sends a short string of characters to the server; the server echoes back the same string to the client. headerfiles.h : need to use the definition of the socket and all procedures (functions) defined in the interface, 31-03-2017 Dr. Manas Khatua 25

Socket struct sockaddr_in { short sin_family; // e.g. AF_INET unsigned short sin_port; // e.g. htons(3490) struct in_addr sin_addr; // see struct in_addr, below. It is IP address. char sin_zero[8]; // zero this if you want to }; struct in_addr { unsigned long s_addr; // load with inet_aton() }; struct sockaddr_in myaddr; int s; myaddr.sin_family = AF_INET; myaddr.sin_port = htons(3490); inet_aton("63.161.169.137", &myaddr.sin_addr.s_addr); //inet_aton:: convert from a struct in_addr to a string in dots-and-numbers s = socket(pf_inet, SOCK_STREAM, 0); bind(s, (struct sockaddr*)myaddr, sizeof(myaddr)); 31-03-2017 Dr. Manas Khatua 26

Echo Server Program II UDP echo server program #include "headerfiles.h // All header files required for socket programming int main (void) { II Declare and define variables int s; II Socket descriptor (reference) int len; II Length of string to be echoed char buffer [256]; II Data buffer struct sockaddr_in servaddr; II Server (local) socket address struct sockaddr_in cintaddr; II Client (remote) socket address int cintaddrlen; II Length of client socket address II Build local (server) socket address memset (&servaddr, 0, sizeof (servaddr)); II Allocate memory servaddr.sin_family = AF_INET; II Default Family field servaddr.sin_port = htons (SERVER_PORT) II Default port number servaddr.sin_addr.s_addr = htonl (INADDR_ANY); II Default IP address 31-03-2017 Dr. Manas Khatua 27

Cont II Create socket if ((s = socket (PF_INET, SOCK_DGRAM, 0) < 0); { perror ("Error: socket failed! "); exit (1); } II Bind socket to local address and port If (bind (s, (struct sockaddr*) &servaddr, sizeof (servaddr)) < 0); { perror ("Error: bind failed!"); exit (1); } for ( ; ; ) II Run forever { II Receive String len = recvfrom (s, buffer, sizeof (buffer), 0, (struct sockaddr*)&cintaddr, &cintaddrlen); II Send String sendto (s, buffer, len, 0, (struct sockaddr*)&cintaddr, sizeof(clntaddr)); } II End of for loop } II End of echo server program 31-03-2017 Dr. Manas Khatua 28

Echo Client Program //UDP echo client program #inciude "headerfiles.h int main (int argc, char* argv[ ]) // Three arguments to be checked later { //Declare and define variables int s; // Socket descriptor int len; // Length of string to be echoed char* servname; // Server name int servport; // Server port char* string; // String to be echoed char buffer [256+ 1]; // Data buffer struct sockaddr_in servaddr; // Server socket address // Check and set program arguments if(argc!=3) { printf ("Error: three arguments are needed!"); exit(1); } servname = argv[1]; servport = atoi (argv[2]); string = argv[3]; 31-03-2017 Dr. Manas Khatua 29

Cont // Build server socket address memset (&servaddr, 0, sizeof (servaddr)); servaddr.sin_family = AF_INET; //call DNS to find the server IP corresponding to server name inet_pton (AF_INET, servname, &servaddr.sin_addr); servaddr.sin_port = htons (servport); // Create socket If ((s = socket (PF_INET, SOCK_DGRAM, 0) < 0); { perror ("Error: Socket failed!"); exit (1); } 31-03-2017 Dr. Manas Khatua 30

Cont //Send echo string len = sendto (s, string, strlen (string), 0, (struct sockaddr)&servaddr, sizeof(servaddr)); //Receive echo string recvfrom (s, buffer, len, 0, NULL, NULL); // NULL: as we don t need client socket address and length //Print and verify echoed string buffer [len] = '\0'; printf ("Echo string received: ); fputs (buffer, stdout); //Close the socket close (s); //Stop the program exit (0); } // End of echo client program 31-03-2017 Dr. Manas Khatua 31

Flow Diagram TCP 31-03-2017 Dr. Manas Khatua 32

Iterative Programming Using TCP Before sending or receiving data, a connection needs to be established between the client and the server. Flow diagram for the client and server data-transfer boxes 31-03-2017 Dr. Manas Khatua 33

Cont We need to control how many bytes of data we have received and where the next chunk of data is stored. 31-03-2017 Dr. Manas Khatua 34

Echo server program using TCP II Echo server program #include "headerfiles.h int main (void) { II Declare and define int Is; II Listen socket descriptor int s; II Socket descriptor (reference) char buffer [256]; II Data buffer char* ptr = buffer; II Data buffer int len = 0; II Number of bytes to send or receive int maxlen = sizeof (buffer); II Maximum number of bytes int n = 0; II Number of bytes for each recv call int waitsize = 16; II Size of waiting clients struct sockaddr _in serveraddr; II Server address struct sockaddr_in clientaddr; II Client address int clntaddrlen; II Length of client address 31-03-2017 Dr. Manas Khatua 35

Cont // Create local (server) socket address memset (&servaddr, 0, sizeof (servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(inaddr_any); II Default IP address servaddr.sin_port = htons (SERV_PORT); II Default port II Create listen socket if (Is = socket (PF_INET, SOCK_STREAM, 0) < 0) { perror ("Error: Listen socket failed!"); exit (1); } II Bind listen socket to the local socket address if (bind (Is, &servaddr, sizeof (servaddr)) < 0) { perror ("Error: binding failed!"); exit (1); } 31-03-2017 Dr. Manas Khatua 36

Cont // Listen to connection requests if (listen (Is, waitsize) < 0) { perror ("Error: listening failed!"); exit (1); } // Handle the connection for ( ; ; ) // Run forever { II Accept connections from client if (s = accept (Is, &cintaddr, &clntaddrlen) < 0) { perror ("Error: accepting failed!); exit (1); } 31-03-2017 Dr. Manas Khatua 37

Cont // Data transfer section while ((n = recv (s, ptr, maxlen, 0)) > 0) { ptr += n ; II Move pointer along the buffer maxlen -= n; II Adjust maximum number of bytes to receive len += n; II Update number of bytes received } send (s, buffer, len, 0); // Send back (echo) all bytes received II Close the socket close (s); } II End of for loop } II End of echo server program 31-03-2017 Dr. Manas Khatua 38

Echo client program using TCP II TCP echo client program #include "headerfiles.h int main (int argc, char* argv[ ]) { II Declare and define int s; II Socket descriptor int n; II Number of bytes in each recv call char* servname; II Server name int servport; II Server port number char* string; II String to be echoed int len; II Length of string to be echoed char buffer [256 + 1]; II Buffer char* ptr = buffer; II Pointer to move along the buffer struct sockaddr_in serveraddr; II Server socket address II Check and set arguments if (argc!= 3) { printf ("Error: three arguments are needed!"); exit (1); } 31-03-2017 Dr. Manas Khatua 39

Cont servname = arg [1]; servport = atoi (arg [2]); string = arg [3]; II Create remote (server) socket address memset (&servaddr, 0, sizeof(servaddr); serveaddr.sin_family = AF _INET; inet_pton (AF_INET, servname,&serveaddr.sin_addr); serveaddr.sin_port = htons (servport); // Server IP // Server port //Create socket if ((s = socket (PF_INET, SOCK_STREAM, 0) < 0); { perror ("Error: socket creation failed!"); exit (1); } 31-03-2017 Dr. Manas Khatua 40

Cont II Connect to the server if (connect (sd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) { perror ("Error: connection failed!"); exit (1); } II Data transfer section send (s, string, strlen(string), 0); while ((n = recv (s, ptr, maxlen, 0)) > 0) { ptr + = n; II Move pointer along the buffer maxlen - = n; II Adjust the maximum number of len += n; II Update the length of string received } 31-03-2017 Dr. Manas Khatua 41

Cont II Print and verify the echoed string buffer [Ien] = '\0'; printf ("Echoed string received: "); fputs (buffer, stdout); II Close socket close (s); // Stop program exit (0); } // End of echo client program 31-03-2017 Dr. Manas Khatua 42

31-03-2017 Dr. Manas Khatua 43