Programming with network Sockets Computer Science Department, University of Crete. Manolis Surligas October 16, 2017

Similar documents
How do we use TCP (or UDP)

M U LT I C A S T C O M M U N I C AT I O N S. Tarik Cicic

Grundlagen der Rechnernetze. Introduction

DRG-Series. Digital Radio Gateway. Kenwood NXDN Donor Radio (Tier-2) Interfacing Omnitronics DRG with Kenwood NXDN Donor Digital Radios (Tier-2)

DRG-Series. Digital Radio Gateway. Hytera DMR USB Donor (Tier-2) Digital Radio Supplement

DRG-Series. Digital Radio Gateway. Icom IDAS Conventional Wireline IP (Tier-2) (IC-FR5000/IC-FR6000 IDAS VHF/UHF Repeaters) Digital Radio Supplement

DRG-Series. Digital Radio Gateway. Motorola MotoTRBO DMR. Interfacing Omnitronics DRG with Motorola MotoTRBO DMR Digital Radios

Interfacing of. MAGIC AE1 DAB+ Go. and ODR DAB Multiplexer. High quality Audio encoding for the Open Source DAB Multiplexer

DRG-Series. Digital Radio Gateway. Tait P25 CCDI Tier-2 (TM9400 Series Mobile Radio) Digital Radio Supplement

Operating Systems and Networks. Networks Part 2: Physical Layer. Adrian Perrig Network Security Group ETH Zürich

This is by far the most ideal method, but poses some logistical problems:

Frequently Asked Questions ConnexRF Products

BlinkRC User Manual. 21 December Hardware Version 1.1. Manual Version 2.0. Copyright 2010, Blink Gear LLC. All rights reserved.

DRG-Series. Digital Radio Gateway. Icom IDAS MultiTrunk IP (Tier-3) Digital Radio Supplement

Configuring OSPF. Information About OSPF CHAPTER

M7 Series Modems for SCADA Applications

Hytera DMR Conventional Series

TI2863 Complete Documentation. Internet Transceiver Controller. 1. Device purpose. 2. Device configuration. TI2863 Internet Transceiver Controller

Version 9.2. SmartPTT PLUS. Capacity Max Configuration Guide

CL4790 USER GUIDE VERSION 3.0. Americas: Europe: Hong Kong:

TIBCO FTL Part of the TIBCO Messaging Suite. Quick Start Guide

6 System architecture

CANopen Programmer s Manual Part Number Version 1.0 October All rights reserved

IX Series 2. Description. IX Series 2 System Features

2320 cousteau court

IOT Question Bank. Unit 1. Chapter 1

FAQ and Solutions. 02 May TM and copyright Imagicle spa

Contents. IEEE family of standards Protocol layering TDD frame structure MAC PDU structure

745 Transformer Protection System Communications Guide

"Terminal RG-1000" Customer Programming Software. User Guide. August 2016 R4.3

RC-WIFI CONTROLLER USER MANUAL

Modular Metering System ModbusTCP Communications Manual

Version 9.1. Installation & Configuration Guide

Experiment 3. Direct Sequence Spread Spectrum. Prelab

LTE Review. EPS Architecture Protocol Architecture Air Interface DL Scheduling EMM, ECM, RRC States QoS, QCIs & EPS Bearers

AT-XTR-7020A-4. Multi-Channel Micro Embedded Transceiver Module. Features. Typical Applications

MADEinUSA OPERATOR S MANUAL. RS232 Interface Rev. A

F8101ALE User s Guide

QUIZ : oversubscription

INSTRUCTION MANUAL IP REMOTE CONTROL SOFTWARE RS-BA1

Cisco IOS IP Routing: OSPF Command Reference

Live Agent for Administrators

LincView OPC USER GUIDE. Enhanced Diagnostics Utility INDUSTRIAL DATA COMMUNICATIONS

Version 8.8 Linked Capacity Plus. Configuration Guide

ReVRSR: Remote Virtual Reality for Service Robots

Monitoring Cable Technologies

Enabling ECN in Multi-Service Multi-Queue Data Centers

SMARTALPHA RF TRANSCEIVER

Catalog

Genesis Channel Manager

Live Agent for Administrators

glideinwms Training HTCondor Overview by Igor Sfiligoi, UC San Diego Aug 2014 HTCondor Overview 1

Live Agent for Administrators

PaperCut PaperCut Payment Gateway Module - CASHNet emarket Checkout - Quick Start Guide

TRBOnet Mobile. User Guide. for ios. Version 1.8. Internet. US Office Neocom Software Jog Road, Suite 202 Delray Beach, FL 33446, USA

Catalog

Canon EF/EF-S Lens Controller (LC-2) Aplication Program Interface

ANSYS v14.5. Manager Installation Guide CAE Associates

Distributed Gaming using XML

Installation and Operation Manual EVTM Stand-alone Encoder/Decoder

Interface Genius Modem Instruction Manual v1.2.4

Understanding PMC Interactions and Supported Features

Taking your game online: Fundamentals of coding online games

Christopher Stephenson Morse Code Decoder Project 2 nd Nov 2007

Brian Hanna Meteor IP 2007 Microcontroller

C Mono Camera Module with UART Interface. User Manual

UCP-Config Program Version: 3.28 HG A

Introductory Module Object Oriented Programming. Assignment Dr M. Spann

CS601 Data Communication Solved Objective For Midterm Exam Preparation

TRBOnet Mobile. User Guide. for Android. Version 2.0. Internet. US Office Neocom Software Jog Road, Suite 202 Delray Beach, FL 33446, USA

Sirindhorn International Institute of Technology Thammasat University

CANopen Programmer s Manual

Spectrum Detector for Cognitive Radios. Andrew Tolboe

CANopen Programmer s Manual

Debugging a Boundary-Scan I 2 C Script Test with the BusPro - I and I2C Exerciser Software: A Case Study

2.11 Over The Air Battery Management

TLE1 REFLECTIVE LINE SENSOR WITH ETHERNET INTERFACE

BandMaster V Manual. Installation

ProLink Radio. 900 MHz SDI-12 Data Radio Scienterra Limited. Version A-0x0C-1-AC 20 October 2009

WEC200 Wiegand to Ethernet

I hope you have completed Part 2 of the Experiment and is ready for Part 3.

Catalogue

A Simple Smart Shopping Application Using Android Based Bluetooth Beacons (IoT)

Hamid Aghvami, King s College Markku Kojo, University of Helsinki Mika Liljeberg, N o k i a R e s e a r c h C e n t e r

Article Number: 457 Rating: Unrated Last Updated: Wed, Sep 2, 2009 at 3:46 PM

Lab Topology R16 R12 R15. Lo R /32 R /32 R /32 R /32 R / /

Real-time Quality Monitoring and Control of Voice over IP

Customer Programming Software RG-1000e (CPS RG-1000e) User Guide. October 2017 R2.0

Outline / Wireless Networks and Applications Lecture 2: Networking Overview and Wireless Challenges. Protocol and Service Levels

Emergency Information Broadcasting Distribution System

SV-MESH Mesh network series Catalogue

VisorTrac A Tracking System for Mining

Appendix S2. Technical description of EDAPHOLOG LOGGER - Communication unit of the EDAPHOLOG System

TRBOnet Enterprise. IP Site Connect. Deployment Guide. Internet. US Office Neocom Software Jog Road, Suite 202 Delray Beach, FL 33446, USA

User's Manual. VCTS Communications Software

EE 314 Spring 2003 Microprocessor Systems

USB Port Medium Power Wireless Module SV653

Chapter 5 Diagnostics

Kodiak Corporate Administration Tool

Active RFID System with Wireless Sensor Network for Power

Transcription:

Programming with network Sockets Computer Science Department, University of Crete Manolis Surligas surligas@csd.uoc.gr October 16, 2017 Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 1 / 30

Goal of this lab Learn to create programs that communicate over a network Create TCP and UDP sockets using the POSIX Socket API Handle properly data Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 2 / 30

The POSIX Socket API What is POSIX? Portable Operating System Interface, is a family of standards specified by the IEEE for maintaining compatibility between operating systems. There are several Sockets implementations (e.g Berkeley, BSD) POSIX Socket API, provides a cross-platform and reliable way for network and inter-process communication Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 3 / 30

What is a Socket? Socket is an endpoint of communication between two processes Two basic types of sockets: - UNIX sockets - Network sockets Processes read and write data to the sockets in order to communicate Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 4 / 30

What is a Socket? Socket Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 5 / 30

Transport Layer Transport layer is responsible for providing end-to-end data transfer between two hosts Two main protocols are used: - TCP - UDP Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 6 / 30

Transport Layer: TCP Connection-oriented communication Reliable, in-order and error free data delivery Flow-control, congestion avoidance Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 7 / 30

Transport Layer: UDP Connection-less communication Packets may be lost Packets may arrive in wrong order Packets may contain wrong data There is no guaranty that packets sent will reach their destination Used when low latency is critical (e.g VoIP, streaming, e.t.c.) Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 8 / 30

Creating a Socket Prototype #i n c l u d e <s y s / t y p e s. h> #i n c l u d e <s y s / s o c k e t. h> 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 ) ; socket() creates a socket of a certain domain, type and protocol specified by the parameters Possible domains: - AF INET for IPv4 internet protocols - AF INET6 for IPv6 internet protocols Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 9 / 30

Creating a Socket Prototype #i n c l u d e <s y s / t y p e s. h> #i n c l u d e <s y s / s o c k e t. h> 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 ) ; socket() creates a socket of a certain domain, type and protocol specified by the parameters Possible types: - SOCK STREAM provides reliable two way connection-oriented byte streams (TCP) - SOCK DGRAM provides connection-less, unreliable messages of fixed size (UDP) protocol depends on the domain and type parameters. In most cases 0 can be passed Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 10 / 30

Creating a Socket SOCK STREAM Sockets of this type are full-dublex data streams that do not rely on a known data length. Before sending or receiving the socket must be in a connected state. To send and receive data, send() and recv() system calls may be used. By default, socket of this type are blocking, meaning that a call of recv() may block until data arrive from the other side. At the end, close() should be used to properly indicate the end of the communication session. SOCK DGRAM This kind of sockets allowing to send messages of a specific size without the guarantee that they will be received from the other side. To send and receive messages sendto() and recvfrom() calls may be used. Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 11 / 30

TCP: Creating the socket Lets try to create our first TCP socket! i n t sock ; i f ( ( sock = s o c k e t ( AF INET, SOCK STREAM, IPPROTO TCP ) ) == 1){ p e r r o r ( o p e n i n g TCP l i s t e n i n g s o c k e t ) ; e x i t ( EXIT FAILURE ) ; } Always check for errors! Using perror() printing a useful and meaningful message is very easy! Opening a TCP socket is exactly the same for both server and client side Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 12 / 30

Bind a Socket Prototype #i n c l u d e <s y s / s o c k e t. h> i n t b i n d ( i n t s o c k e t, c o n s t s t r u c t s o c k a d d r a d d r e s s, s o c k l e n t a d d r e s s l e n ) ; bind() assigns an open socket to a specific network interface and port bind() is very common in TCP servers because they should waiting for client connections at specific ports Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 13 / 30

TCP: Bind the socket s t r u c t s o c k a d d r i n s i n ; memset(& s i n, 0, s i z e o f ( s t r u c t s o c k a d d r i n ) ) ; s i n. s i n f a m i l y = AF INET ; s i n. s i n p o r t = h t o n s ( l i s t e n i n g p o r t ) ; s i n. s i n a d d r. s a d d r = h t o n l (INADDR ANY ) ; i f ( b i n d ( sock, ( s t r u c t s o c k a d d r )& s i n, s i z e o f ( s t r u c t s o c k a d d r i n ) ) == 1){ p e r r o r ( TCP b i n d ) ; e x i t ( EXIT FAILURE ) ; } Always reset the struct sockaddr in before use Addresses and ports must be assigned in Network Byte Order INADDR ANY tells the OS to bind the socket at all the available network interfaces Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 14 / 30

Listening for incoming connections Prototype i n t l i s t e n ( i n t s o c k e t, i n t b a c k l o g ) ; After binding to a specific port a TCP server can listen at this port for incoming connections backlog parameter specifies the maximum possible outstanding connections Clients can connect using the connect() call Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 15 / 30

Listening for incoming connections Prototype i n t l i s t e n ( i n t s o c k e t, i n t b a c k l o g ) ; After binding to a specific port a TCP server can listen at this port for incoming connections backlog parameter specifies the maximum possible outstanding connections Clients can connect using the connect() call Hint! For debugging you can use the ss utility! Try: bash$ n e t s t a t l t p n Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 15 / 30

Trivia Think! Which of the calls of the previous slides cause data to be transmitted or received over the network? Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 16 / 30

Trivia Think! Which of the calls of the previous slides cause data to be transmitted or received over the network? NONE! Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 17 / 30

TCP: Accepting connections Prototype #i n c l u d e <s y s / s o c k e t. h> i n t a c c e p t ( i n t s o c k e t, s t r u c t s o c k a d d r r e s t r i c t a d d r e s s, s o c k l e n t r e s t r i c t a d d r e s s l e n ) ; accept() is by default a blocking call It blocks until a connection arrives to the listening socket On success a new socket descriptor is returned, allowing the listening socket to handle the next available incoming connection The returned socket is used for sending and receiving data If address is not NULL, several information about the remote client are returned address len before the call should contain the size of the address struct. After the call should contain the size of the returned structure Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 18 / 30

TCP: Connecting Prototype #i n c l u d e <s y s / s o c k e t. h> i n t c o n n e c t ( i n t s o c k e t, c o n s t s t r u c t s o c k a d d r a d d r e s s, s o c k l e n t a d d r e s s l e n ) ; Connects a socket with a remote host Like bind(), zero the contains of address before use and assign remote address and port in Network Byte Order If bind() was not used, the OS assigns the socket to all the available interfaces and to a random available port Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 19 / 30

TCP: Sending Data Prototype #i n c l u d e <s y s / s o c k e t. h> s s i z e t send ( i n t s o c k e t, c o n s t v o i d b u f f e r, s i z e t l e n g t h, i n t f l a g s ) ; send() is used to send data using a connection oriented protocol like TCP Returns the actual number of bytes sent Always check the return value for possible errors or to handle situations where the requested buffer did not sent completely Question! Does this call block? Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 20 / 30

TCP: Sending Data Prototype #i n c l u d e <s y s / s o c k e t. h> s s i z e t send ( i n t s o c k e t, c o n s t v o i d b u f f e r, s i z e t l e n g t h, i n t f l a g s ) ; send() is used to send data using a connection oriented protocol like TCP Returns the actual number of bytes sent Always check the return value for possible errors or to handle situations where the requested buffer did not sent completely Question! Does this call block? YES! Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 20 / 30

TCP: Receiving Data Prototype #i n c l u d e <s y s / s o c k e t. h> s s i z e t r e c v ( i n t s o c k e t, v o i d b u f f e r, s i z e t l e n g t h, i n t f l a g s ) ; recv() is by default a blocking call that receives data from a connection-oriented opened socket length specifies the size of the buffer and the maximum allowed received data chunk Returns the number of bytes received from the network recv() may read less bytes than length parameter specified, so use only the return value for your logic If you do not want to block if no data are available, use non-blocking sockets (hard!) or poll() Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 21 / 30

TCP Overview Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 22 / 30

UDP: Creating the socket Creating a UDP socket is quite the same as with TCP i n t sock ; i f ( ( sock = s o c k e t ( AF INET, SOCK DGRAM, IPPROTO UDP ) ) == 1){ p e r r o r ( o p e n i n g UDP s o c k e t ) ; e x i t ( EXIT FAILURE ) ; } Only type and protocol parameters are different bind() is also exactly the same for UDP too Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 23 / 30

UDP: Connection-less UDP is connection-less!!! No need to call accept() or connect()!!! Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 24 / 30

UDP: Receiving data Prototype #i n c l u d e <s y s / s o c k e t. h> s s i z e t r e c v f r o m ( i n t s o c k e t, v o i d r e s t r i c t b u f f e r, s i z e t l e n g t h, i n t f l a g s, s t r u c t s o c k a d d r r e s t r i c t a d d r e s s, s o c k l e n t r e s t r i c t a d d r e s s l e n ) ; length specifies the length of the buffer in bytes address if not NULL, after the call should contain information about the remote host address len is the size of the struct address Returns the number of bytes actually read. May be less that length Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 25 / 30

UDP: Problems at receiving Have in mind that recvfrom() is a blocking call How you can probe if data are available for receiving? Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 26 / 30

UDP: Problems at receiving Have in mind that recvfrom() is a blocking call How you can probe if data are available for receiving? - Use poll() Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 26 / 30

UDP: Problems at receiving Have in mind that recvfrom() is a blocking call How you can probe if data are available for receiving? - Use poll() What if the message sent is greater that your buffer? Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 26 / 30

UDP: Problems at receiving Have in mind that recvfrom() is a blocking call How you can probe if data are available for receiving? - Use poll() What if the message sent is greater that your buffer? - Use recvfrom() in a loop with poll() Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 26 / 30

UDP: Sending data Prototype #i n c l u d e <s y s / s o c k e t. h> s s i z e t s e n d t o ( i n t s o c k e t, c o n s t v o i d message, s i z e t l e n g t h, i n t f l a g s, c o n s t s t r u c t s o c k a d d r d e s t a d d r, s o c k l e n t d e s t l e n ) ; length is the number of the bytes that are going to be sent from buffer message dest addr contains the address and port of the remote host Returns the number of bytes sent. May be less that length so the programmer should take care of it Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 27 / 30

UDP: Sending data Prototype #i n c l u d e <s y s / s o c k e t. h> s s i z e t s e n d t o ( i n t s o c k e t, c o n s t v o i d message, s i z e t l e n g t h, i n t f l a g s, c o n s t s t r u c t s o c k a d d r d e s t a d d r, s o c k l e n t d e s t l e n ) ; length is the number of the bytes that are going to be sent from buffer message dest addr contains the address and port of the remote host Returns the number of bytes sent. May be less that length so the programmer should take care of it Trivia! Does sendto() block? Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 27 / 30

UDP: Sending data Prototype #i n c l u d e <s y s / s o c k e t. h> s s i z e t s e n d t o ( i n t s o c k e t, c o n s t v o i d message, s i z e t l e n g t h, i n t f l a g s, c o n s t s t r u c t s o c k a d d r d e s t a d d r, s o c k l e n t d e s t l e n ) ; length is the number of the bytes that are going to be sent from buffer message dest addr contains the address and port of the remote host Returns the number of bytes sent. May be less that length so the programmer should take care of it Trivia! Does sendto() block? NO! Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 27 / 30

Endianness Networks are heterogeneous with many different OS s, architectures, etc Endianess is a serious problem when sending data to other hosts When sending entities that are greater that a byte, always convert them in Network Byte Order By default Network Byte Order is Big-Endian Use htons(), ntohs(), htonl(), ntohl() Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 28 / 30

Endianness Networks are heterogeneous with many different OS s, architectures, etc Endianess is a serious problem when sending data to other hosts When sending entities that are greater that a byte, always convert them in Network Byte Order By default Network Byte Order is Big-Endian Use htons(), ntohs(), htonl(), ntohl() Trivia! When sending large strings do we have to convert in Network Byte Order? Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 28 / 30

Endianness Networks are heterogeneous with many different OS s, architectures, etc Endianess is a serious problem when sending data to other hosts When sending entities that are greater that a byte, always convert them in Network Byte Order By default Network Byte Order is Big-Endian Use htons(), ntohs(), htonl(), ntohl() Trivia! When sending large strings do we have to convert in Network Byte Order? NO! Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 28 / 30

Useful man pages socket(7) ip(7) setsockopt(3p) tcp(7) udp(7) Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 29 / 30

Questions?? Manolis Surligas (CSD, UoC) Programming with network Sockets October 16, 2017 30 / 30