Computer Network Programming

Similar documents
CSE 333 Section 8 - Client-Side Networking

UNIX System Programming Lecture 19: IP Sockets

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

last time redo logging copy-on-write filesystems / snapshots distributed systems motivation, etc.

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

Introduction to Lab 2 and Socket Programming. -Vengatanathan Krishnamoorthi

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

Socket Programming. #In the name of Allah. Computer Engineering Department Sharif University of Technology CE443- Computer Networks

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

CSE 333 SECTION 6. Networking and sockets

Azblink API for Sending XMPP Messages via HTTP POST

Introduction to Computer Networks

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

Sockets and Parallel Computing. CS439: Principles of Computer Systems April 11, 2018

CSE 333 SECTION 7. Client-Side Network Programming

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

CSE 333 SECTION 8. Sockets, Network Programming

CSE 333 SECTION 7. Client-Side Network Programming

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

UNIX Sockets. COS 461 Precept 1

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

Client-side Networking

CSCI 4061: Sockets and Network Programming

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

Redesde Computadores(RCOMP)

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

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

Category: Informational J. Bound J. McCann Hewlett-Packard W. Stevens February 2003

CSE 124 Discussion Section Sockets Programming 10/10/17

Porting IPv4 applications to IPv4/v6 dual stack. Owen DeLong

Networks and Their Abstractions. CS439: Principles of Computer Systems April 9, 2018

5. System Calls or Bust

목포해양대해양컴퓨터공학과. IPv6 적용

2007 Microsoft Corporation. All rights reserved.

The exam is closed book, closed notes, closed electronics, closed telepathy, open mind.

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Sockets 15H2. Inshik Song

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

Network Communication

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

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

Write your answer on the next page. You may remove this page for reference while working if you wish.

#include <dirent.h> int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen); DIR *opendir(const char *name);

Socket Programming TCP UDP

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Networks class. CS144 Introduction to Computer Networking. Administrivia. Administrivia 2. Grading. Labs. Goal: Teach the concepts underlying networks

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

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

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

EECS122 Communications Networks Socket Programming. Jörn Altmann

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

// print product names in alphabetical order with total // number sold of each product using format name: total void PrintTotals();

Elementary TCP Sockets

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

Randall Stewart, Cisco Systems Phill Conrad, University of Delaware

Tutorial on Socket Programming

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

CS 4400 Fall 2017 Final Exam Practice

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

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

Interprocess Communication Mechanisms

A Client-Server Exchange

Interprocess Communication Mechanisms

Server-side Programming

IPv6 Porting Applications

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

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

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

CSE 333 Lecture 16 - network programming intro

#include <sys/socket.h> int listen(int socket, int backlog); socket. socket. backlog

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

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

Unix Network Programming

TCP: Three-way handshake

Unix Network Programming

Winsock Server adding Multiple clients support C++

A case study of IPv6 deployment. tcd.ie. David Malone. 22 May 2003

Group-A Assignment No. 6

Piotr Mielecki Ph. D.

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

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

Network Programming: Part I

Raising the Datagram API to Support Transport Protocol Evolution

CSE 333 Lecture network programming intro

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

CLIENT-SIDE PROGRAMMING

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

CS307 Operating Systems Processes

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

Internet Engineering Task Force (IETF) July 2011

A Case Study of IPv6 Deployment in tcd.ie

The User Datagram Protocol

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

Agenda. Before we start: Assignment #1. Routing in a wide area network. Protocols more concepts. Internetworking. Congestion control

Interprocess Communication Mechanisms

shared storage These mechanisms have already been covered. examples: shared virtual memory message based signals

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

Socket Programming for TCP and UDP

Transcription:

Practical Programming Computer Network Programming Marwan Burelle & David Bouchet david.bouchet.epita@gmail.com 1

Quick Overview 1.IP and Protocol Stack 2.TCP Concepts 3.Client / Server Concepts 4.Socket API 5.Code 2

Computer Network Programming Connecting computers and networks to each other 3

Computer Network Programming How can we connect these two computers?? 4

The Internet Protocol IP Goals: Abstracting heterogeneous networks Interconnected Networks Unified address space over the whole network Provides a global logical net over physical ones Routing: Selecting paths in networks 5

The Internet Protocol IP Each computer has an IP address IPv4: 32-bit address IPv6: 128-bit address 128.12.97.65 routing... 247.125.109.87 6

Network Packets Data is split into packets 110010 128.12.97.65 011010 packet forwarding... 010001 247.125.109.87 7

Protocol Stack IP relies on a whole stack of protocols Horizontal logical connexion Vertical concrete communication 8

Protocol Stack Application Logical link Application Transport Layer (TCP/UDP...) Transport Layer (TCP/UDP...) IP IP Low level Protocol (ethernet) Low level Protocol (ethernet) Device Driver Device Driver Physical Layer (wires) Physical Layer (wires) Physical link 9

Protocol Stack Physical Layer: Made up of electronic circuits Device Driver: Software interface to drive the physical layer Low Level Protocol: Transfers data between adjacent or local networks Internet Protocol: Routing and packet forwarding. 10

Protocol Stack Transport Layer: Protocols (TCP, UDP, etc.) that provide services: delivery to application, connected or not, same or different order delivery, reliability or unreliability, flow control, etc. Application Layer: Specifies communication protocols and interface methods (FTP, SMTP, HTTP, Telnet, IMAP, etc.) 11

Transport Layer TCP Stream oriented Connected Acknowledgement Retransmissions Packets ordering Timeouts UDP Datagram oriented Not connected No reception check Fire and forget No ordering 12

Client / Server Model Client Server User of the service Service provider Initiates connexion Waits for connexion Ends connexion (TCP) Serves several clients Uses dynamic ports Uses fixed ports 13

Client / Server Model Example Client (The Web Browser) The Internet Server (Apache, IIS) 14

TCP triple hand-check Server Client socket connect (block) SYN J socket,bind,listen accept (block) k J+1 c a, K N SY connect returns ack K+1 accept returns read (block) 15

Port Numbers Port Number: 16-bit unsigned integer (from 0 to 65,535) IP addresses identify hosts Applications identified by port numbers All TCP (or UDP) communications use ports A connexion is identified by the double pair: ( (IP1, port1), (IP2, port2) ) 16

Socket API Most used API for TCP / UDP connections Compatible with classic C Input / Output Once established, a socket is just an FD Use recv(2) / send(2) Or read(2) / write(2) 17

Network Address and Service Translation int getaddrinfo(const char *node, getaddrinfo node const char *service, service const struct addrinfo *hints, hints struct addrinfo **res); res void freeaddrinfo(struct addrinfo *res); freeaddrinfo res const char *gai_strerror(int errcode); gai_strerror errcode getaddrinfo(3) 18

Using getaddrinfo(3) getaddrinfo() returns one or more addrinfo structures, each of which contains an Internet address that can be specified in a call to bind(2) or connect(2). connect(2) struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; socklen_t ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }; getaddrinfo(3) 19

Using getaddrinfo(3) If hints is not NULL it points to an addrinfo structure whose ai_flags, ai_flags ai_family, ai_family ai_socktype, ai_socktype and ai_protocol specify criteria that limit the set of socket addresses returned by getaddrinfo(). getaddrinfo() All the other fields in the structure pointed to by hints must contain either 0 or a null pointer, as appropriate. getaddrinfo(3) 20

TCP Client Connexion Use getaddrinfo(). For each addrinfo struct, try to: Create the socket (man 2 socket). Connect the socket (man 2 connect). Stop when a connection is established. Read from / write to the socket. Close the connexion. 21

TCP Client Connexion struct addrinfo hints; struct addrinfo *result; int addrinfo_error; memset(&hints, 0, sizeof (struct addrinfo)); hints.ai_family = AF_INET; // IPv4 only, AF_UNSPEC if you want v4 and/or v6 hints.ai_socktype = SOCK_STREAM; // TCP // Get your info addrinfo_error = getaddrinfo(name, port, &hints, &result); // Error management if ( addrinfo_error!= 0 ) { errx(exit_failure, "Fail getting address for %s on port %s: %s", name, port, gai_strerror(addrinfo_error)); } 22

TCP Client Connexion // try to connect on each result // result is a linked list for (rp = result; rp!= NULL; rp = rp->ai_next) { cnx = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (cnx == -1) continue; if ( connect(cnx, rp->ai_addr, rp->ai_addrlen)!= -1 ) break; close(cnx); } freeaddrinfo(result); if (rp == NULL) { errx(exit_failure, "Couldn't connect"); } 23

TCP Client Connexion (old way) void client() { int sockfd; struct sockaddr_in addr; struct hostent *server; // Socket creation sockfd = socket(af_inet, SOCK_STREAM, 0); // Get server address server = gethostbyname("example.com"); // Init sockaddr struct memset(&addr, 0, sizeof (struct sockaddr_in)); addr.sin_family = AF_INET; memcpy(&addr.sin_addr.s_addr, server->h_addr_list[0], server->h_length); addr.sin_port = htons(80); // Connection connect(sockfd, (struct sockaddr*)&addr, sizeof (struct sockaddr_in)); // read/write on sockfd // Done close(sockfd); } 24

TCP Server Connexion Use getaddrinfo() with: hints.ai_flags = AI_PASSIVE AI_ADDRCONFIG; For each addrinfo struct, try to: Create the socket (man 2 socket). Bind the socket (man 2 bind). Use listen() to listen for connection (man 2 listen). In an infinite loop: Use accept() to accept incoming connexion (man 2 accept). Read from / write to the socket. Close the connexion. 25

More Than One Connexion Idea: Handling a connexion while waiting for others. // after init... listen(fd_accept, 5); for (;;) { // Accept a cnx fdcnx = accept(fd_accept, (struct sockaddr*)&remote, &rlen); if (fork()) { // father close(fdcnx); continue; } // child close(fd_accept); // Read/Write... Wait for EOF from client side close(fdcnx); } close(fd_accept); 26

Managing Zombies After each connexion, the handling process becomes a zombie. We shall catch SIGCHLD to clear that. void chldhandler(int sig) { wait(null); } void server(uint16_t portno) { //... signal(sigchld, chldhandler); //... } or void server(uint16_t portno) { //... signal(sigchld, SIG_IGN); //... } 27

Managing Zombies man 2 sigaction [...] POSIX.1-1990 disallowed setting the action for SIGCHLD to SIG_IGN. POSIX.1-2001 and later allow this possibility, so that ignoring SIGCHLD can be used to prevent the creation of zombies (see wait(2)). Nevertheless, the historical BSD and System V behaviors for ignoring SIGCHLD differ, so that the only completely portable method of ensuring that terminated children do not become zombies is to catch the SIGCHLD signal and perform a wait(2) or similar. [...] 28