Distributed Real-Time Control Systems. Module 26 Sockets

Similar documents
Distributed Real-Time Control Systems

Distributed Real-Time Control Systems. Lecture 17 C++ Exceptions Synchronous I/O

Distributed Real-Time Control Systems. Chapter 22 Asynchronous I/O The Boost ASIO Library

Distributed Real-Time Control Systems. Lecture 24 Asynchronous Socket Communications

Introduction to Internetworking

IP Addressing and Subnetting

TCP/IP and the OSI Model

TCP /IP Fundamentals Mr. Cantu

Distributed Real-Time Control Systems. Lecture 19 Asynchronous Communications

Chapter 6. The Protocol TCP/IP. Introduction to Protocols

Internetwork Protocols

ET4254 Communications and Networking 1

Guide to Networking Essentials, 6 th Edition. Chapter 5: Network Protocols

On Distributed Communications, Rand Report RM-3420-PR, Paul Baran, August 1964

User Datagram Protocol

IP: Addressing, ARP, Routing

Internet Protocols (chapter 18)

IP Address Assignment

McGraw-Hill The McGraw-Hill Companies, Inc., 2000

Chapter 6. What happens at the Transport Layer? Services provided Transport protocols UDP TCP Flow control Congestion control

Chapter Motivation For Internetworking

Network Protocols - Revision

Transport Layer. Gursharan Singh Tatla. Upendra Sharma. 1

SEN366 (SEN374) (Introduction to) Computer Networks

Interconnecting Networks with TCP/IP

ECE4110 Internetwork Programming. Introduction and Overview

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

Layer 4: UDP, TCP, and others. based on Chapter 9 of CompTIA Network+ Exam Guide, 4th ed., Mike Meyers

Transport Layer (TCP/UDP)

THE OSI MODEL. Application Presentation Session Transport Network Data-Link Physical. OSI Model. Chapter 1 Review.

PART X. Internetworking Part 1. (Concept, IP Addressing, IP Routing, IP Datagrams, Address Resolution)

Interconnecting Networks with TCP/IP. 2000, Cisco Systems, Inc. 8-1

Chapter 2 - Part 1. The TCP/IP Protocol: The Language of the Internet

Interprocess Communication Mechanisms

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

A Client-Server Exchange

Vorlesung Kommunikationsnetze

Data and Computer Communications. Chapter 2 Protocol Architecture, TCP/IP, and Internet-Based Applications

Copyright 2011 Sakun Sharma

IP - The Internet Protocol. Based on the slides of Dr. Jorg Liebeherr, University of Virginia

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

Lecture 17 Overview. Last Lecture. Wide Area Networking (2) This Lecture. Internet Protocol (1) Source: chapters 2.2, 2.3,18.4, 19.1, 9.

EITF25 Internet Techniques and Applications L7: Internet. Stefan Höst

The TCP Protocol Stack

ETSF05/ETSF10 Internet Protocols Network Layer Protocols

TCP/IP Protocol Suite and IP Addressing

Prof. Shervin Shirmohammadi SITE, University of Ottawa. Internet Protocol (IP) Lecture 2: Prof. Shervin Shirmohammadi CEG

Computer Networks. Lecture 9 Network and transport layers, IP, TCP, UDP protocols

Networking Basics. EC512 Spring /15/2015 EC512 - Prof. Thomas Skinner 1

Network Programming. Introduction to Sockets. Dr. Thaier Hayajneh. Process Layer. Network Layer. Berkeley API

CHAPTER-2 IP CONCEPTS

RMIT University. Data Communication and Net-Centric Computing COSC 1111/2061. Lecture 2. Internetworking IPv4, IPv6

Introduction Layer 3. IP-Header: and RFC-760 Addressing schemes Subnetting Routing. Layer 3 Solution in Trains

Memory-Mapped Files. generic interface: vaddr mmap(file descriptor,fileoffset,length) munmap(vaddr,length)

Just enough TCP/IP. Protocol Overview. Connection Types in TCP/IP. Control Mechanisms. Borrowed from my ITS475/575 class the ITL

Defining Networks with the OSI Model. Module 2

CCNA 1 Chapter 7 v5.0 Exam Answers 2013

06/02/ Local & Metropolitan Area Networks 0. INTRODUCTION. 1. History and Future of TCP/IP ACOE322

What is a Network? TCP / IP. The ISO OSI Model. Protocols. The TCP/IP Protocol Suite. The TCP/IP Protocol Suite. Computer network.

Network Layer/IP Protocols

Chapter 6 Addressing the Network- IPv4

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


This talk will cover the basics of IP addressing and subnetting. Topics covered will include:

Concept Questions Demonstrate your knowledge of these concepts by answering the following questions in the space provided.

Data & Computer Communication

COSC 6377 Mid-Term #2 Fall 2000

Data Communication Prof. A. Pal Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 34 TCP/ IP I

EEC-484/584 Computer Networks

QUIZ: Longest Matching Prefix

Unit C - Network Addressing Objectives Purpose of an IP Address and Subnet Mask Purpose of an IP Address and Subnet Mask

ETSF10 Internet Protocols Network Layer Protocols

Networking for Data Acquisition Systems. Fabrice Le Goff - 14/02/ ISOTDAQ

Chapter 7. Local Area Network Communications Protocols

Introduction. IP Datagrams. Internet Service Paradigm. Routers and Routing Tables. Datagram Forwarding. Example Internet and Conceptual Routing Table

OSI Network Layer. Chapter 5

Network+ Guide to Networks 6 th Edition. Chapter 4 Introduction to TCP/IP Protocols

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

Networks. an overview. dr. C. P. J. Koymans. Informatics Institute University of Amsterdam. February 4, 2008

Internet Protocol (IP)

ECE 4450:427/527 - Computer Networks Spring 2017

The Internet Protocol (IP)

Lecture 8. Network Layer (cont d) Network Layer 1-1

ECE 650 Systems Programming & Engineering. Spring 2018

Chapter 5 TCP/IP SUITE

1. Which OSI layers offers reliable, connection-oriented data communication services?

The Internet. The Internet is an interconnected collection of netw orks.

C18: Network Fundamentals and Reliable Sockets

Introduction to TCP/IP networking

CPEG514 Advanced Computer Networks. Atef Abu Salim University of Nizwa Spring 2013/2014

Chapter 6: Network Communications and Protocols

Lab - Using Wireshark to Examine a UDP DNS Capture

Lecture 3. The Network Layer (cont d) Network Layer 1-1

Objectives. Chapter 10. Upon completion you will be able to:

Internetworking Terms. Internet Structure. Internet Structure. Chapter 15&16 Internetworking. Internetwork Structure & Terms

Concept Questions Demonstrate your knowledge of these concepts by answering the following questions in the space that is provided.

Lab - Using Wireshark to Examine a UDP DNS Capture

CIS-331 Final Exam Fall 2015 Total of 120 Points. Version 1

IT 341: Introduction to System

UDP CONNECT TO A SERVER

Transcription:

Distributed Real-Time Control Systems Module 26 Sockets 1

Network Programming with Sockets Sockets are probably the most widely used objects in programming networked communications. What is a socket? To the kernel, a socket is an endpoint of communication: IP address, port number, and protocol (TCP, UDP, etc). To an application, a socket is a file descriptor that lets the application read/write from/to the network Clients and servers communicate with each by reading from and writing to socket. 2

The Ethernet Frame-based Technology Ethernet stations communicate by sending each other data packets. Each Ethernet station is given a single 48-bit MAC address, which is used both to specify the destination and the source of each data packet. 3

The OSI stack model APDU Application Application Protocol Application 7 PPDU Presentation Presentation Protocol Presentation 6 SPDU Session Session Protocol Session 5 TPDU Transport Trasport Protocol Host-Router communication boundary Transport 4 packet Network Network Network Network 3 Frame Data Link Data Link Data Link Data Link 2 Bit Physical Physical Physical Physical 1 Host-Router internal protocol 4

IP Addressing There are two versions of an IP address : IPv4 and IPv6. Because of its prevalence, "IP address" typically refers to those defined by IPv4. Using reserved values for the first octet, network addresses are broken into classes: Class A very large networks (up to 2 24 hosts) Class B large networks (up to 2 16 hosts) Class C small networks (up to 255 hosts) Class D multi-cast messages to multiple hosts Class E addresses not allocated and reserved. 5

IP Classes 7 24 Class A: 0 Network ID Host ID 14 16 Class B: 1 0 Network ID Host ID 21 8 Class C: 1 1 0 Network ID Host ID 28 Class D (multicast): 1 1 1 0 Multicast address Class E (reserv ed): 1 1 1 1 0 unused Class Range within 1st octet Network ID Host ID 27 Possible number of networks Possible number of hosts A 1-126 a b.c.d. 126 16,777,214 B 128-191 a.b c.d 16,384 65,534 C 192-223 a.b.c d 2,097,151 254 6

Special Addresses Adapter Loopback 127.0.0.1 Private addresses (will not be routed) 10.0.0.0-10.255.255.255 172.16.0.0-172.31.255.255 192.168.0.0-192.168.255.255 7

Subnet Masking Subnetwork or Subnet is a range of logical addresses within the address space that is assigned to an organization. Subnetworking simplifies routing. Dot-decimal Address Full Network Address 192.168.5.10 Subnet Mask 255.255.255.0 Network Portion 192.168.5.0 Host Portion 0.0.0.10 Binary 11000000.10101000.000001 01.00001010 11111111.11111111.111111 11.00000000 11000000.10101000.000001 01.00000000 00000000.00000000.000000 00.00001010 8

Address Resolution The Address Resolution Protocol (ARP) is a method for finding a host s hardware address when only its network layer address is known. E.g. IP addresses to MAC addresses. ARP is used in four cases of two hosts communicating: 1. When two hosts are on the same network and one desires to send a packet to the other 2. When two hosts are on different networks and must use a gateway/router to reach the other host 3. When a router needs to forward a packet for one host through another router 4. When a router needs to forward a packet from one host to the destination host on the same network 9

Sockets A socket is an endpoint of communication: IP address, port number, and protocol (TCP, UDP, etc). TCP (or Stream) Sockets Connection oriented, require handshake Client contacts Server to establish connection. Server provides a private connection to client. UDP (or Datagram) Sockets Connectionless, low overhead. Data is sent directly to destination. No handshake: data may be lost. 10

Connection-Oriented Model The connection-oriented model is implemented with Stream Sockets using the TCP protocol. TCP provides a variety or error correction and performance features for transmission of byte streams. Implements a client-server architecture. 11

Connectionless-Oriented Model The connectionless model uses UDP protocol with no guarantee of delivery. UDP packets may arrive out of order, multiple times, or not at all. UDP has considerably less overhead than TCP Implements a peer-to-peer architecture. 12

Connectionless-Oriented Model (UDP) Server Process socket() bind() Client Process socket() bind() read_from() write_to() request response write_to() read_from() close() close() 13

Datagram (UDP) Socket Primitives socket() creates a new socket, and allocates system resources to it. bind() is the process to associate a socket with a specified local port number and IP address. send_to()/write_to, recv_from()/read_from(), are used for sending and receiving data to/from a remote socket. 14

Boost Datagram Sockets ip::udp::endpoint - identifies an address, port and protocol for communication. ip::udp::resolver (cliente side) retrieves a suitable communication endpoint from a server. ip::udp::socket creates a socket associated to an endpoint socket::receive_from() waits for some data from any client socket::send_to() sends data to a specific client 15

Boost Synchronous UDP Example #include <iostream> //SYNC_UDP_SERVER #include <boost/asio.hpp> using namespace boost::asio; using ip::udp; int main() { io_service io; auto ep = udp::endpoint(udp::v4(),10000); std::cout << Receiving at: << ep << std::endl; udp::socket s(io,ep)); char buf[1] = {0}; while (buf[0]!= q ) { udp::endpoint client; //client endpoint retrieved on receive_from s.receive_from(buffer(buf, 1), client); if( buf[0]!= q ) s.send_to(buffer( Hello World ), client); else s.send_to(buffer( q ), client); } } #include <iostream> //SYNC_UDP_CLIENT #include <boost/asio.hpp> using namespace boost::asio; using ip::udp; using query = udp::resolver::query; int main() { io_service io; udp::resolver r(io); query q(udp::v4(),"127.0.0.1","10000"); udp::endpoint ep = *r.resolve(q); udp::socket s(io); s.open(udp::v4()); char buf[128] = {0}; do { std::cin.getline(buf,128); s.send_to(buffer(buf,1), ep); //send 1st char size_t n = s.receive_from(buffer(buf, 128),ep); std::cout.write(buf, n-1); } while( buf[0]!= q ) } 16

Notes on Previous Code The example client opens a socket to the server machine and loops on reading data from cin, sending a char to make contact with server, wait server reply and prints the reply. When the reply start with character q, it exits. The example server waits for messages of arbitrary clients and sends back the message Hello World, unless the first character of the message is q. In that case it also sends back q and exits. This example does not check for errors. Functions send_to and receive_from have overloads to check for errors: socket.send_to(buffer, end_point, flags, error_code) socket.receive_to(buffer, end_point, flags, error_code) 17

Connection Oriented Model Server Listening socket Welcomes some initial contact from a client Connection socket It is created at initial contact of client New socket that is dedicated to the particular client Client Client socket Specify the address of the server process, namely, the IP address of the server and the port number of the process 18

Connection-Oriented Model Server Process socket() Client Process socket() bind() listen() accept() get a blocked client read() write() read() 3-way handshake client request server response close notification connect() write() read() close() close() 19

Socket Primitives socket() creates a new socket and allocates system resources to it. bind() is typically used on the server side, and associates a socket with a socket address structure, i.e. a specified local port number and IP address. listen() is used on the server side, and causes a bound TCP socket to enter listening state. connect() is used on the client side. Attempts to establish a new TCP connection. accept() is used on the server side. It accepts a received incoming attempt to create a new TCP connection from the remote client, and creates a new socket associated with the socket address pair of this connection. write() and read() are used for sending and receiving data to/from a remote socket. 20

Boost Stream Sockets Important classes ip::tcp::acceptor - binds a listening socket to accept cliente requests ip::tcp::endpoint sets address and port pair for accept cliente requests ip::tcp::resolver retrieves a address and port pair for communication ip::tcp::socket - a socket associated to an endpoint Important functions acceptor::accept() accepts connections from a client ip::tcp::connect() tries to connect to a server. ip::tcp::read() waits until some data is received ip::tcp::write() write to socket 21

Boost Synchronous TCP Example #include <iostream> //SYNC_TCP_SERVER.CPP #include <boost/asio.hpp> using namespace boost::asio; using ip::tcp; int main() { io_service io; boost::system::error_code ec; char buf[128]; auto ep = tcp::endpoint(tcp::v4(),10000); std::cout << Listening at: << ep << std::endl; tcp::acceptor a(io, ep); for (;;) { tcp::socket s(io); //create new listening socket a.accept(s); //wait client to connect for(;;) { //got a client size_t n = s.read_some(buffer(buf,128), ec); if(ec) break; write(s, buffer(buf,n), ec); if(ec) break; } //kills connection } } #include <iostream> //SYNC_TCP_CLIENT.CPP #include <boost/asio.hpp> using namespace boost::asio; using ip::tcp; int main() { io_service io; boost::system::error_code err; char buf[128]; tcp::resolver r(io); //find endpoints from address tcp::resolver::query q( 127.0.0.1, 10000 ); tcp::resolver::iterator server = r.resolve(q); tcp::socket s(io); s.connect(*server, err); //connect and wait for (;;) { std::cin.getline(buf,128); write(s,buffer(buf,strlen(buf)+1), err); if(ec) break; size_t n = s.read_some(buffer(buf,128), err); if (ec) break; std::cout.write(buf, n-1); } } 22

Notes on Previous Code The example client opens a socket to the server machine and loops on reading data from cin, sending that data to the server, wait server reply and prints the reply. If exits if the server does not respond. The example server creates a listening socket and waits for a client to connect. When it gets a connection, it waits for a message from the client and send that exact same message back to the client (echo). If the connection is broken, it closes the connection and creates a new listening socket. 23