Application Programming Interfaces

Similar documents
Introduction to Socket Programming

ECE 435 Network Engineering Lecture 2

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

ECE 435 Network Engineering Lecture 2

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Client software design

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

CSE 333 SECTION 6. Networking and sockets

CSE 333 SECTION 8. Sockets, Network Programming

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

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

CSE 333 SECTION 7. Client-Side Network Programming

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

SOCKETS. COMP750 Distributed Systems

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

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

Network Programming November 3, 2008

CSE 333 Lecture 16 - network programming intro

Network Socket Programming - 2 BUPT/QMUL

Lab 0. Yvan Petillot. Networks - Lab 0 1

Programming with TCP/IP. Ram Dantu

CS 3516: Computer Networks

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

CS321: Computer Networks Socket Programming

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

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

Sockets. 1 Introduction. (Reference:, Gray Chapter 10) Network Programming Lecture Notes by. Turhan TUNALI

The User Datagram Protocol

Internetworking Nov 25, 2009"

Piotr Mielecki Ph. D.

Tutorial on Socket Programming

UDP CONNECT TO A SERVER

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

CS321: Computer Networks Introduction to Application Layer

Internet protocol stack. Internetworking II: Network programming. April 20, UDP vs TCP. Berkeley Sockets Interface.

Internetworking II: Network programming. April 20, 2000

CS307 Operating Systems Processes

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

CS4700/CS5700 Fundamentals of Computer Networking

1 /* client.c - adapted from code for example client program that uses TCP */ 2 /*Modified by Vincent Chu, Winter

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

CS4514 B08 HELP Session 1

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

Network Communication

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

CSE 333 Lecture 8 - file and network I/O

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

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

CSE 333 Lecture network programming intro

CompSci 356: Computer Network Architectures Lecture 3: Hardware and physical links References: Chap 1.4, 1.5 of [PD] Xiaowei Yang

System Programming. Sockets

Socket Programming TCP UDP

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

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

UNIX Network Programming. Overview of Socket API Network Programming Basics

CLIENT-SIDE PROGRAMMING

Linux Network Programming, Part 1

Network Socket Programming - 3 BUPT/QMUL

A Client-Server Exchange

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

Network Socket Programming - 3 BUPT/QMUL

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

Interprocess Communication Mechanisms

Client/Server. Networking Approach.

CS 640: Computer Networking

ICT 6544 Distributed Systems Lecture 5

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

CSE 333 SECTION 7. Client-Side Network Programming

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

Department of Computer Science

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

ECE322 Systems Programming Project 2: Networking with Matrix Multiplication in C Grant Kimes 12/16/15

TCP: Three-way handshake

Interprocess Communication Mechanisms

// socket for establishing connections

CSC 271 Software I: Utilities and Internals

Message passing systems are popular because they support client-server interactions, where: clients send messages to servers requesting a server.

Unix Network Programming

How to write a Measurement Telnet Server

The course that gives CMU its Zip! Internetworking Nov 20, 2001

EECS122 Communications Networks Socket Programming. Jörn Altmann

IP Addresses, DNS. CSE 333 Spring Instructor: Justin Hsia

Lecture 7. Berkeley Socket Programming

Communication. Communication. Distributed Systems. Networks and protocols Sockets Remote Invocation Messages Streams. Fall /10/2001 DoCS

TCP Network Programming in C

Communication. Sockets (Haviland Ch. 10)

IP Addresses, DNS. CSE 333 Summer Teaching Assistants: Renshu Gu William Kim Soumya Vasisht

CSE 124 Discussion Section Sockets Programming 10/10/17

CompSci 356: Computer Network Architectures. Lecture 3: Network Architecture Examples and Lab 1. Xiaowei Yang

A. Basic Function Calls for Network Communications

Client Server Computing

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

How to write a Measurement Telnet Server

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

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

Internetworking April 13, 2006

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

Socket Programming for TCP and UDP

Transcription:

Application Programming Interfaces Stefan D. Bruda Winter 2018

SYSTEM CALLS Machine 1 Machine 2 Application 1 Application 3 Application 4 Application 5 Application 2 API (system functions) API (system functions) Operating system (including TCP/IP software) Network Operating system (including TCP/IP software) Application Programming Interfaces (S. D. Bruda) Winter 2018 1 / 15

SYSTEM CALLS (CONT D) Where do we find API descriptions? In Section 2 of the manual pages Section 3 is dedicated to library functions, which are wrappers over APIs (implemented using calls to the respective API) Examples: man -S2 open (often man 2 open will also work) man -S3 printf (or man 3 printf) Some interesting (for us) APIs: Process management (fork and company already seen) File I/O Terminal I/O (cin, cout / scanf, printf) The TCP/IP implementation (Berkeley sockets) Application Programming Interfaces (S. D. Bruda) Winter 2018 2 / 15

SYSTEM CALLS (CONT D) Where do we find API descriptions? In Section 2 of the manual pages Section 3 is dedicated to library functions, which are wrappers over APIs (implemented using calls to the respective API) Examples: man -S2 open (often man 2 open will also work) man -S3 printf (or man 3 printf) Some interesting (for us) APIs: Process management (fork and company already seen) File I/O Terminal I/O (cin, cout / scanf, printf) library functions, not API! The TCP/IP implementation (Berkeley sockets) Application Programming Interfaces (S. D. Bruda) Winter 2018 2 / 15

FILE I/O Operation open close read write lseek Meaning prepares a file for I/O operations returns a file descriptor (int) used by all the other operations terminates the use of a previously opened file/device obtain data from a file/device write data to a file/device move to some position in the file/device (not applicable to all devices) For example, char result[256]; int file = open("echo",o_rdonly); if (file == -1) return 1; read(file,result,255); close(file); Application Programming Interfaces (S. D. Bruda) Winter 2018 3 / 15

FILE I/O (CONT D) #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <stdio.h> #include <unistd.h> int main (int argc, char** argv) { int file = open(argv[1],o_wronly O_CREAT O_APPEND,S_IRUSR S_IWUSR); int i = 0; char prefix[12]; char message[256] = "something"; if (file == -1) return 1; while (true) { i++; printf("message: "); fgets(message,255,stdin); if (message[strlen(message)-1] == \n ) message[strlen(message)-1] == \0 ; if (strlen(message) == 0) break; snprintf(prefix,12,"%d: ",i); write(file,prefix,strlen(prefix)); write(file,message,strlen(message)); write(file,"\n",1); close(file); Application Programming Interfaces (S. D. Bruda) Winter 2018 4 / 15

TEXT FILES int readline(int fd, char* buf_str, size_t max) { size_t i; for (i = 0; i < max; i++) { char tmp; int what = read(fd,&tmp,1); if (what == 0 tmp == \n ) { buf_str[i] = \0 ; return i; buf_str[i] = tmp; buf_str[i] = \0 ; return i; int dbf = open("myfile",o_rdonly); if (dbf == -1) { perror(myfile); exit(1); char message[256]; int nc = readline(dbf,message,255); Application Programming Interfaces (S. D. Bruda) Winter 2018 5 / 15

TEXT FILES (CONT D) const int recv_nodata = -2; int readline(const int fd, char* buf, const size_t max) { size_t i; int begin = 1; for (i = 0; i < max; i++) { char tmp; int what = read(fd,&tmp,1); if (what == -1) return -1; if (begin) { if (what == 0) return recv_nodata; begin = 0; if (what == 0 tmp == \n ) { buf[i] = \0 ; return i; buf[i] = tmp; buf[i] = \0 ; return i; Application Programming Interfaces (S. D. Bruda) Winter 2018 6 / 15

FILE DESCRIPTORS int dbf = open("myfile",o_rdonly); Descriptor table (one per process) 0: 1: 2: data structure for file 0 (stdin) data structure for file 1 (stdout) n: data structure for file 2 (stderr) data structure for file n ("myfile") Application Programming Interfaces (S. D. Bruda) Winter 2018 7 / 15

THE TCP/IP API TCP/IP is a loosely specified protocol In other words, the TCP/IP standard does not specify the API, only the functionality A TCP/IP interface must support the following operations: Allocate resources for communication Specify communication endpoints Initiate a connection (client) or wait for connection (server) Send and receive data Identify data arrival Generate urgent data and handle incoming urgent data Terminate a connection, handle connection termination, abort communication Handle error conditions Release resources We use one (by far the most popular) TCP/IP implementation called Berkeley sockets Application Programming Interfaces (S. D. Bruda) Winter 2018 8 / 15

BERKELEY SOCKETS Recall that an application that wants to access a file uses open Further recall that open returns a file descriptor An application that wanto to communicate creates a socket using socket socket also returns a descriptor int sd = socket(pf_inet,sock_stream,0) Descriptor table (one per process) 0: 1: 2: data structure for file 0 (stdin) data structure for file 1 (stdout) n: data structure for file 2 (stderr) data structure for a socket n Application Programming Interfaces (S. D. Bruda) Winter 2018 9 / 15

WHAT TO DO WITH SOCKETS We have a socket, what do we do with it? We can convince it to be either passive (for servers) or active (for clients). socket bind socket connect send recv close Active socket (client) listen accept recv send close Passive socket (server) Application Programming Interfaces (S. D. Bruda) Winter 2018 10 / 15

THE SOCKET API Who Call Meaning Both socket Creates a socket, returns a descriptor Client connect Connects to a remote server (client) Server bind Associates ( binds ) the socket to a local IP address and a port listen Place the socket into passive mode (also sets the length of the queue) accept Accept the next incoming connection (server) Both send Send data (can also use write) recv Receive data (can also use read) close Terminate communication and destroys the descriptor As well, do not forget the headers: #include <sys/types.h> #include <sys/socket.h> Application Programming Interfaces (S. D. Bruda) Winter 2018 11 / 15

SPECIFYING AN ADDRESS In principle, there exist address families to hold different type of addresses In practice, TCP/IP uses one family of addresses, called AF_INET An address (within an address family) should contain at least the address of some machine and a port number The TCP/IP API includes C structures for address endpoints: struct sockaddr_in {... sa_family_t sin_family; /* Address family, AF_INET for us */ unsigned short int sin_port; /* Port number */ struct in_addr sin_addr; /* IP address */... ; struct in_addr { unsigned int s_addr; /* 32 bits, i.e., 4 bytes */ ; Application Programming Interfaces (S. D. Bruda) Winter 2018 12 / 15

FINDING AN ADDRESS Usually, a server address is given as a machine name or an IP address in dotted decimal notation: cs.ubishops.ca or 207.162.103.183 No matter how the server is specified (as dotted address or name), we have to obtain the actual IP address gethostbyname does the job no matter how the server is specified and puts the result in the following structure: struct hostent { char *h_name; /* Official name of host */ char **h_aliases; /* Alias list */ int h_addrtype; /* Host address type */ int h_length; /* Length of address */ char **h_addr_list; /* List of addresses from name server */ #define h_addr h_addr_list[0] /* Address, for backward compatibility */ ; Application Programming Interfaces (S. D. Bruda) Winter 2018 13 / 15

FINDING AN ADDRESS (CONT D) Actually, h_addr_list is an array that contains the bytes of a network address They are given in network byte order (aka big endian, big end first ), i.e., exactly in the right order to be put in a sockaddr_in structure #include <stdio.h> #include <netdb.h> extern int h_errno; int main (int argc, char** argv) { struct hostent* host_info = gethostbyname(argv[1]); if (host_info!= NULL) { printf("%s is ", argv[1]); for (int i = 0; i < host_info -> h_length; i++) printf("%d.", (unsigned char)((host_info -> h_addr)[i])); printf("\n"); else printf("host lookup error %d: %s\n", h_errno, hstrerror(h_errno)); Application Programming Interfaces (S. D. Bruda) Winter 2018 14 / 15

FINDING A WELL-KNOWN PORT If you know the name of a standard service, you can find the port number associated to that service using getservbyname which puts the result in the following structure: struct servent { char *s_name; /* Official service name. */ char **s_aliases; /* Alias list. */ int s_port; /* Port number. */ char *s_proto; /* Protocol to use. */ ; For example: struct servent* service = getservbyname("ftp","tcp"); if (service!= NULL) { printf("%s\n%d\n%s\n", service -> s_name, service -> s_port, service -> s_proto); < godel:slides/code >./a.out ftp 21 tcp Application Programming Interfaces (S. D. Bruda) Winter 2018 15 / 15