Socket Programming. Daniel Zappala. CS 360 Internet Programming Brigham Young University

Similar documents
Concurrent HTTP Proxy Server. CS425 - Computer Networks Vaibhav Nagar(14785)

Advanced Topicson Network Socket Programming

CS 3411 Systems Programming

Computer Networks ( Classroom Practice Booklet Solutions)

Knocking down the HACIENDA with TCP Stealth

Knocking down the HACIENDA with TCP Stealth

Overlay Transport Virtualization (OTV) Unicast-Mode Transport Infrastructure Deployment

MCS 260 Exam 2 13 November In order to get full credit, you need to show your work.

Administrivia. Course Objectives. Overview. Lecture Notes Week markem/cs333/ 2. Staff. 3. Prerequisites. 4. Grading. 1. Theory and application

CS 425 / ECE 428 Distributed Systems Fall Indranil Gupta (Indy) Oct. 5, 2017 Lecture 12: Time and Ordering All slides IG

Troubleshooting Replication and Geodata Services. Liz Parrish & Ben Lin

Estimation of DNS Source and Cache Dynamics under Interval-Censored Age Sampling

Redesde Computadores(RCOMP)

Boost UDP Transaction Performance

Distributed Systems Principles and Paradigms. Chapter 06: Synchronization

Troubleshooting Replication and Geodata Service Issues

Do we have a quorum?

Slides for Chapter 14: Time and Global States

A NEW SYSTEM FOR THE GENERATION OF UTC(CH)

TSCCLOCK: A LOW COST, ROBUST, ACCURATE SOFTWARE CLOCK FOR NETWORKED COMPUTERS

Leopold Franzens University Innsbruck. Responding to Spurious Loss Events in TCP/IP. Master Thesis. Institute of Computer Science

E8 TCP. Politecnico di Milano Scuola di Ingegneria Industriale e dell Informazione

Distributed Systems. Time, Clocks, and Ordering of Events

Time in Distributed Systems: Clocks and Ordering of Events

Networked Control Systems

Distributed Systems Principles and Paradigms

INF Models of concurrency

DISTRIBUTED COMPUTER SYSTEMS

Outline F eria AADL behavior 1/ 78

The Analysis of Microburst (Burstiness) on Virtual Switch

MODBUS Protocol for CS141 network card families

An Optimal Index Policy for the Multi-Armed Bandit Problem with Re-Initializing Bandits

Deterministic Networking for Real-Time Systems

A study of entropy transfers

Agreement. Today. l Coordination and agreement in group communication. l Consensus

Reaxys Pipeline Pilot Components Installation and User Guide

An introduction to flash memory in Linux

Model-based Prototyping of an Interoperability Protocol for Mobile Ad-hoc Networks

Distributed systems Lecture 4: Clock synchronisation; logical clocks. Dr Robert N. M. Watson

Antonio Falabella. 3 rd nternational Summer School on INtelligent Signal Processing for FrontIEr Research and Industry, September 2015, Hamburg

Operating Systems. VII. Synchronization

Congestion Control. Need to understand: What is congestion? How do we prevent or manage it?

SDS developer guide. Develop distributed and parallel applications in Java. Nathanaël Cottin. version

IEEE C /058r3

ECEN 449: Microprocessor System Design Department of Electrical and Computer Engineering Texas A&M University

A Methodology for Clock Benchmarking

Unreliable Failure Detectors for Reliable Distributed Systems

CS505: Distributed Systems

NovaToast SmartVision Project Requirements

4th year Project demo presentation

FLAME Flexible Lightweight Active Measurement Environment

Time. Lakshmi Ganesh. (slides borrowed from Maya Haridasan, Michael George)

CS276 Homework 1: ns-2

Distributed Systems. Time, clocks, and Ordering of events. Rik Sarkar. University of Edinburgh Spring 2018

10:00 12:30. Do not open this problem booklet until the start of the examination is announced.

Performance Analysis of Priority Queueing Schemes in Internet Routers

Deadlock. CSE 2431: Introduction to Operating Systems Reading: Chap. 7, [OSC]

Infrared Tire Temperature Sensor, IRTS-V2 - Datasheet

2IN35 VLSI Programming Lab Work Communication Protocols: A Synchronous and an Asynchronous One

Chapter 11 Time and Global States

Approximate Fairness with Quantized Congestion Notification for Multi-tenanted Data Centers

Lan Performance LAB Ethernet : CSMA/CD TOKEN RING: TOKEN

Part I. System call overview. Secure Operating System Design and Implementation System Calls. Overview. Abstraction. Jon A. Solworth.

Capacity management for packet-switched networks with heterogeneous sources. Linda de Jonge. Master Thesis July 29, 2009.

Go Tutorial. Ian Lance Taylor. Introduction. Why? Language. Go Tutorial. Ian Lance Taylor. GCC Summit, October 27, 2010

CS418 Operating Systems

Figure 10.1 Skew between computer clocks in a distributed system

Congestion Control. Phenomenon: when too much traffic enters into system, performance degrades excessive traffic can cause congestion

Min Congestion Control for High- Speed Heterogeneous Networks. JetMax: Scalable Max-Min

CS 3411 Systems Programming

Coordination. Failures and Consensus. Consensus. Consensus. Overview. Properties for Correct Consensus. Variant I: Consensus (C) P 1. v 1.

Modern Functional Programming and Actors With Scala and Akka

Innovation. The Push and Pull at ESRI. September Kevin Daugherty Cadastral/Land Records Industry Solutions Manager

PC Based Precision Timing Without GPS

FlowSpec. Frédéric Gabut-Deloraine. FRnOG - 2 décembre 2011 NEO TELECOMS

Data byte 0 Data byte 1 Data byte 2 Data byte 3 Data byte 4. 0xA Register Address MSB data byte Data byte Data byte LSB data byte

STRIBOB : Authenticated Encryption

CALIFORNIA INSTITUTE OF TECHNOLOGY Control and Dynamical Systems

BTstack Getting Started. Using MSP430 Examples. Dr. sc. Milanka Ringwald Dr. sc. Matthias Ringwald

CS 347 Parallel and Distributed Data Processing

Análise e Modelagem de Desempenho de Sistemas de Computação

Process Scheduling. Process Scheduling. CPU and I/O Bursts. CPU - I/O Burst Cycle. Variations in Bursts. Histogram of CPU Burst Times

High Performance Computing

THE SPATIAL DATA SERVER BASED ON OPEN GIS STANDARDS IN HETEROGENEOUS DISTRIBUTED ENVIRONMENT

Shared on QualifyGate.com

These are special traffic patterns that create more stress on a switch

COMP9334: Capacity Planning of Computer Systems and Networks

Clocks in Asynchronous Systems

Our Problem. Model. Clock Synchronization. Global Predicate Detection and Event Ordering

Solutions to COMP9334 Week 8 Sample Problems

Recursive InterNetworking Architecture (RINA) Boston University Prototype Programming Manual (version 1.0)

Implementation of the IEEE 1588 Precision Time Protocol for Clock Synchronization in the Radio Detection of Ultra-High Energy Neutrinos

Inter-Reactive Kotlin Applications

Network Traffic Characteristic

Trivadis Integration Blueprint V0.1

Distributed Systems Fundamentals

Wireless Internet Exercises

Patrol: Revealing Zero-day Attack Paths through Network-wide System Object Dependencies

Network Configuration Example

Mininet on OpenBSD. Using rdomains for Interactive SDN Testing and Development. Ayaka Koshibe. BSDCan

Transcription:

Socket Programming Daniel Zappala CS 360 Internet Programming Brigham Young University

Sockets, Addresses, Ports

Clients and Servers 3/33 clients request a service from a server using a protocol need an API for sending and receiving data need an abstraction for the Internet a reliable connection: TCP an unreliable message service: UDP these are provided by the BSD socket API

Processes and Sockets 4/33 inter-process communication uses messages sent on sockets socket API defines how to open, close, read, and write to socket which transport protocol to use various communication parameters

Socket API 5/33 BSD Socket API - the dominant socket API on Linux, BSD, Windows Use the man pages! shows C syntax, with include files gives a description of how the system call works lists return values lists errors lists other relevant man pages man man man socket the socket API is in section 2 of the manual, so you will need to specify the section if the same command exists in an earlier section: man 2 bind

Addresses and Ports 6/33 to talk to a process on another machine, you need to identify it IP address: identifies the machine port number: identifies the socket the process is using

IP Addresses 7/33 IPv4 address identifies an interface/link on a host or router 32 bits dotted-decimal notation: each part is 8 bits

Ports 8/33 identifier for a socket on which a process is listening 16 bits a process may listen on more than one socket; each must be on a separate port Operating systems (such as Linux) designate some ports as privileged, meaning only the superuser can listen on them (typically ports less than 1024) To help find common servers, the IANA designates well-known ports for many protocols HTTP: 80 SMTP: 25 SSH: 22 NTP: 123

ClientAPI

Client API 10/33 1 socket(): create a socket endpoint 2 connect(): connect to a server 3 send(): send data 4 recv(): receive data 5 close(): close the socket

Creating a Socket 11/33 1 i n t s o c k e t ( i n t domain, i n t type, i n t p r o t o c o l ) ; 2 3 domain = PF INET 4 t y p e = SOCK STREAM f o r TCP, SOCK DGRAM f o r UDP 5 p r o t o c o l = 0 on success returns a socket descriptor

Connecting to a Server 12/33 1 i n t c o n n e c t ( i n t sockfd, c o n s t s t r u c t s o c k a d d r s e r v a d d r, 2 s o c k l e n t a d d r l e n ) ; 3 4 s o c k f d = s o c k e t you c r e a t e d 5 s e r v a d d r = p o i n t e r to s o c k e t a d d r e s s s t r u c t u r e 6 s o c k l e n t = l e n g t h o f s o c k e t a d d r e s s connects to a server uses the socket address structure to pass an IP address and port on success returns zero on error returns -1 and sets errno

Sending and Receiving Data 13/33 usually the client sends a request to the server and the server sends a reply socket operations are similar to reading from and writing to a file a socket descriptor acts like a file handle sending writing receiving reading

Send and Receive Syntax 14/33 1 s s i z e t send ( i n t s, c o n s t v o i d buf, s i z e t len, i n t f l a g s ) ; 2 s s i z e t r e c v ( i n t s, v o i d buf, s i z e t len, i n t f l a g s ) ; s = socket buf = pointer to buffer len = size of buffer in bytes on success returns the number of bytes actually sent or received if it is less than what you expected, then you must repeat the system call until all data is sent or received recv() will return 0 if the socket has been closed on error returns -1 and sets errno see man pages for more advanced options

Closing a Socket 15/33 1 #i n c l u d e <u n i s t d. h> 2 3 i n t c l o s e ( i n t f d ) ; fd = socket on success returns zero on error returns -1 and sets errno releases the socket file descriptor so it can be re-used!

Example 16/33 See simple echo client on github. GitHub

Socket Addresses

Socket Addresses 18/33 to connect a socket, you must supply a socket address structure that includes the IP address and port of the server you are connecting to 1 use DNS to convert host name to IP address 2 initialize a socket address structure

Generic Socket Address Structure 19/33 1 s t r u c t s o c k a d d r { 2 s a f a m i l y t s a f a m i l y ; // a d d r e s s f a m i l y 3 c h a r s a d a t a [ 1 4 ] ; // a d d r e s s 4 } used to represent a generic connection between processes potentially provides access to many different addressing standards address family can be AF UNIX : local UNIX socket AF INET : Internet socket

IPv4 Socket Address Structure 20/33 1 s t r u c t s o c k a d d r i n { 2 s a f a m i l y t s i n f a m i l y ; // a d d r e s s f a m i l y 3 u i n t 1 6 t s i n p o r t ; // p o r t 4 s t r u c t i n a d d r s i n a d d r ; // I n t e r n e t a d d r e s s 5 c h a r s i n z e r o [ 8 ] ; // unused 6 } can cast IPv4 socket into a generic socket port is 2 bytes, address is 4 bytes, zero is 8 bytes = 14 bytes IPv4 address structure: 1 s t r u c t i n a d d r { 2 u i n t 3 2 t s a d d r ; // a d d r e s s 3 } ;

Network Byte Order 21/33 you must store the port and address in network byte order (most significant byte sent first) provides interoperability among Internet hosts use htons() for the port and inet aton, inet addr() or inet makeaddr() for the address 1 s t r u c t s o c k a d d r i n s e r v e r ; 2 s e r v e r. s i n p o r t = htons ( 8 0 ) ; 3 i f (! i n e t a t o n ( i p a d d r e s s,& s e r v e r. s i n a d d r ) ) 4 p r i n t f ( i n e t a d d r ( ) c o n v e r s i o n e r r o r \n ) ;

Example 22/33 See simple echo client on GitHub. GitHub

Server API

Server API 24/33 1 create a socket 2 bind: associate the socket with an address and port 3 listen: convert socket to listen for incoming connections 4 accept: accept an incoming client connection 5 send: send data 6 recv: receive data

Binding the Socket 25/33 1 i n t b i n d ( i n t sockfd, c o n s t s t r u c t s o c k a d d r my addr, 2 s o c k l e n t a d d r l e n ) ; 3 4 s o c k f d = s o c k e t you c r e a t e d 5 my addr = p o i n t e r to s o c k e t a d d r e s s s t r u c t u r e 6 a d d r l e n = l e n g t h o f s o c k e t a d d r e s s associates an address with a socket uses the socket address structure to pass an IP address and port on success returns zero, -1 and errno otherwise

Listening on the Socket 26/33 1 i n t l i s t e n ( i n t sockfd, i n t b a c k l o g ) ; 2 3 s o c k f d = s o c k e t you c r e a t e d and bound 4 b a c k l o g = maximum w a i t i n g c o n n e c t i o n s converts a socket to a passive socket (one that accepts connections rather than connecting) backlog is the maximum number of waiting connections the kernel should hold in a queue maximum value in Linux is 128 on success returns zero, -1 and errno otherwise

Accepting a Connection 27/33 1 i n t a c c e p t ( i n t sockfd, s t r u c t s o c k a d d r addr, 2 s o c k l e n t a d d r l e n ) ; 3 4 s o c k f d = s o c k e t you c r e a t e d and bound 5 addr = p o i n t e r to empty s o c k e t a d d r e s s s t r u c t u r e 6 a d d r l e n = l e n g t h o f empty s o c k e t a d d r e s s s t r u c t u r e accept a connection from a client; gets the next connection waiting in the queue if there are no pending connections, the process sleeps assuming this is a blocking socket (this is the default) on success returns a new socket descriptor on success, accept() fills in the address of the client

Example 28/33 See simple echo server on GitHub. GitHub

Parsing Messages

Example 30/33 Try simple echo client with slow echo server in the socket example code. GitHub

Parsing Messages 31/33 an application protocol reads and write messages, but TCP and the socket API use a byte stream when a client calls recv() there is no guarantee that it will get an entire message the application has to designate where a message starts and ends and then parse the byte stream looking for messages two options for reading a message 1 variable length: read until a sentinel (end-of-message marker) 2 known length: read a length field and then read the listed number of bytes you will need to write a read-until-sentinel() and read-fixed-length() methods for your socket programming labs

Sending and Receiving Properly 32/33 results of send() or recv() call 1 less than zero bytes, errno == EINTR the system call was interrupted try again 2 less than zero bytes, any other error fatal error try to recover gracefully 3 zero bytes the socket is closed try to recover gracefully 4 positive bytes the return value is the number of bytes sent or received use a send() or recv() loop!

Example 33/33 See echo server and client on GitHub. GitHub