The User Datagram Protocol

Similar documents
UDP CONNECT TO A SERVER

Client software design

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

NETWORKS (CONT D) RELATION TO OTHER PROTOCOLS

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

Socket Programming for TCP and UDP

CS4700/CS5700 Fundamentals of Computer Networking

STUDY OF SOCKET PROGRAMMING

Network Communication

EEC-484/584 Computer Networks

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

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

CS321: Computer Networks Introduction to Application Layer

Elementary TCP Sockets

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

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

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

Group-A Assignment No. 6

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

CS321: Computer Networks Socket Programming

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

Department of Computer Science

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

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Unix Network Programming

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

Introduction to Socket Programming

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

Tutorial on Socket Programming

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Application Programming Interfaces

Sockets Sockets Communication domains

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

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

Socket Programming(2/2)

EECS122 Communications Networks Socket Programming. Jörn Altmann

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

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

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

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

Sockets 15H2. Inshik Song

Lecture 5 Overview! Last Lecture! This Lecture! Next Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book!

Programming with TCP/IP. Ram Dantu

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

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Programming Internet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

(Refer Slide Time: 1:09)

NETWORK AND SYSTEM PROGRAMMING

CLIENT-SIDE PROGRAMMING

UNIX Sockets. COS 461 Precept 1

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

Introduction to Client-Server Model

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

CS307 Operating Systems Processes

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


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

ICT 6544 Distributed Systems Lecture 5

Outline. 1 Networking overview. 2 Systems issues. 3 OS networking facilities. 4 Implementing networking in the kernel

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2)

CS 3516: Computer Networks

A Client-Server Exchange

CSE/EE 461 Lecture 14. Connections. Last Time. This Time. We began on the Transport layer. Focus How do we send information reliably?

Chapter 2 Applications and

Interprocess Communication. Interprocess Communication

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

Network programming(i) Lenuta Alboaie

What s an API? Do we need standardization?

Linux Programming

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

Multiservice servers. Stefan D. Bruda. Winter 2018

UNIT IV- SOCKETS Part A

Interprocess Communication Mechanisms

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

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

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

UNIT 1 TCP/IP PROGRAMMING CONCEPTS

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

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

Socket Programming. What is a socket? Using sockets. Types (Protocols) Associated functions Styles

Socket Programming TCP UDP

Ά η η 1 (30%): Sockets. socket () bind () listen () accept () connect () read () write () close ()

CSE 124 Discussion Section Sockets Programming 10/10/17

Network programming(ii) Lenuta Alboaie

SOCKETS. COMP750 Distributed Systems

CS4514 B08 HELP Session 1

Programming Ethernet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

Interprocess Communication Mechanisms

Lab 0. Yvan Petillot. Networks - Lab 0 1

Interprocess Communication Mechanisms

Redesde Computadores(RCOMP)

Motivation of VPN! Overview! VPN addressing and routing! Two basic techniques for VPN! ! How to guarantee privacy of network traffic?!

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

System Programming. Sockets

Overview. Exercise 0: Implementing a Client. Setup and Preparation

The Fundamentals. Port Assignments. Common Protocols. Data Encapsulation. Protocol Communication. Tevfik Ko!ar

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

Transcription:

The User Datagram Protocol Stefan D. Bruda Winter 2018

UDP Very similar to the TCP in terms of API Dissimilar with TCP in terms of innards (and hence programming techniques) Many-to-many communication. Unlike TCP (point-to-point communication), UDP allows wide flexibility in the number of applications that can communicate with each other multicast and broadcast facility Unreliable delivery. A message can arrive in duplicate, or not arrive at all No flow control. When messages arrive faster than they can be consumed, they are dropped Message paradigm. Unlike TCP (stream paradigm) UDP communication is based on individual messages (datagrams) Less overhead. UDP algorithms are simpler and thus communication is faster Your (informed) choice: one cannot choose between the sharply different TCP and UDP without taking into consideration the requirements of the application protocol The User Datagram Protocol (S. D. Bruda) Winter 2018 1 / 16

UDP CLIENTS Algorithm similar with TCP: 1 Obtain the IP address and port number of the server (unchanged) 2 Allocate a socket 3 Choose a port for communication (arbitrary, unused) 4 Specify the server to which messages are to be sent 5 Communicate with the server (application protocol, send and receive messages) 6 Close the socket The User Datagram Protocol (S. D. Bruda) Winter 2018 2 / 16

UDP CLIENTS Algorithm similar with TCP: 1 Obtain the IP address and port number of the server (unchanged) 2 Allocate a socket 3 Choose a port for communication (arbitrary, unused) 4 Specify the server to which messages are to be sent 5 Communicate with the server (application protocol, send and receive messages) 6 Close the socket Socket allocation: Need to specify the protocol family and the socket type (UDP) #include <sys/types.h> #include <sys/socket.h> int sd = socket(pf_inet, SOCK_DGRAM, 0); We end up with a socket descriptor The User Datagram Protocol (S. D. Bruda) Winter 2018 2 / 16

CONNECT TO A SERVER int connectudp(const char* host, const unsigned short port) { struct hostent *hinfo; struct sockaddr_in sin; int sd; const int type = SOCK_DGRAM; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; hinfo = gethostbyname(host); if (hinfo == NULL) return err_host; memcpy(&sin.sin_addr, hinfo->h_addr, hinfo->h_length); sin.sin_port = (unsigned short)htons(port); sd = socket(pf_inet, type, 0); if ( sd < 0 ) return err_sock; int rc = connect(sd, (struct sockaddr *)&sin, sizeof(sin)); if (rc < 0) { close(sd); return err_connect; } return sd; } The User Datagram Protocol (S. D. Bruda) Winter 2018 3 / 16

CONNECT TO A SERVER int connectudp(const char* host, const unsigned short port) { struct hostent *hinfo; struct sockaddr_in sin; int sd; const int type = SOCK_DGRAM; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; hinfo = gethostbyname(host); if (hinfo == NULL) return err_host; memcpy(&sin.sin_addr, hinfo->h_addr, hinfo->h_length); sin.sin_port = (unsigned short)htons(port); sd = socket(pf_inet, type, 0); if ( sd < 0 ) return err_sock; int rc = connect(sd, (struct sockaddr *)&sin, sizeof(sin)); if (rc < 0) { close(sd); return err_connect; } return sd; } The User Datagram Protocol (S. D. Bruda) Winter 2018 4 / 16

CONNECTED AND UNCONNECTED UDP SOCKETS Client applications can use a UDP socket in connected and unconnected mode To enter connected mode, the client calls connect to specify the remote endpoint address To communicate using an unconnected socket we have to specify the remote endpoint address each time we send a message This is the only difference between connected and unconnected sockets a call to connect does not initiate any packet exchange it just stores the remote address for future use even if the call succeeds there is no guarantee that the address is valid, that the server is up, or that the server is reachable The User Datagram Protocol (S. D. Bruda) Winter 2018 5 / 16

COMMUNICATE WITH THE SERVER We assume hereby that we have a connected socket We then send data using send and receive responses using recv Each time we call send, UDP sends a single message to the server containing all the data to be sent There is no longer the case that we might receive the answer in pieces Each call to recv returns a complete message, we no longer need repeated calls If the receiving buffer is large enough, we end up with our original message If the message is too large for the buffer... The User Datagram Protocol (S. D. Bruda) Winter 2018 6 / 16

COMMUNICATE WITH THE SERVER We assume hereby that we have a connected socket We then send data using send and receive responses using recv Each time we call send, UDP sends a single message to the server containing all the data to be sent There is no longer the case that we might receive the answer in pieces Each call to recv returns a complete message, we no longer need repeated calls If the receiving buffer is large enough, we end up with our original message If the message is too large for the buffer... the bytes that cannot be stored are discarded without feedback The User Datagram Protocol (S. D. Bruda) Winter 2018 6 / 16

CLOSING THE CONNECTION close closes the connection and destroys the socket The machine on which the close occurs does not inform its peer about the event The peer should be aware of this and know how long should it keep the data structures for the interaction with the client We can think of using shutdown to partially close the socket Unfortunately, such a call is useless The User Datagram Protocol (S. D. Bruda) Winter 2018 7 / 16

CLOSING THE CONNECTION close closes the connection and destroys the socket The machine on which the close occurs does not inform its peer about the event The peer should be aware of this and know how long should it keep the data structures for the interaction with the client We can think of using shutdown to partially close the socket Unfortunately, such a call is useless The purpose of partial close was to inform the peer UDP does not do this, the peer does not receive any indication of what happened on the other end (no end-of-file, no SIGPIPE) The User Datagram Protocol (S. D. Bruda) Winter 2018 7 / 16

UDP SERVERS In principle, your UDP server is just your usual server We can have concurrent or iterative servers We can build our servers stateless or stateful In practice, many combination do not make a lot of sense It is hard to argue for a stateful UDP server Under UDP, it is often the case that process/thread creation is too expensive Process req. 2 Concurrent Create slave 1 Process req. 1 Create slave 2 Iterative Process req. 1 Process req. 2 0 p c 2p 2c 2c+p Few UDP servers have concurrent implementations in practice Time The User Datagram Protocol (S. D. Bruda) Winter 2018 8 / 16

A UDP CONCURRENT SERVER 1 create and bind the master socket 2 leave the master socket unconnected 3 repeat forever: 1 call recvfrom to receive the next request from a client no call to accept is involved no slave socket is created 2 fork/create thread? 3 (in child thread) do: 1 form a reply according to the application protocol 2 send the reply back to the client through the master socket using sendto 3 terminate 4 continue with the loop A concurrent implementation does not make a lot of sense The child terminates after serving one request About the only reason for concurrency is a time consuming step 1.3.1 The User Datagram Protocol (S. D. Bruda) Winter 2018 9 / 16

CREATE AND BIND A UDP SERVER SOCKET int passiveudp(const unsigned short port, const int backlog, const unsigned long int ip_addr) { struct sockaddr_in sin; int sd; const int type = SOCK_DGRAM; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = ip_addr; // usually INADDR_ANY sin.sin_port = (unsigned short)htons(port); sd = socket(pf_inet, type, 0); if ( sd < 0 ) return err_sock; if ( bind(sd, (struct sockaddr *)&sin, sizeof(sin)) < 0 ) return err_bind; // if ( listen(sd, backlog) < 0 ) // return err_listen; return sd; } The User Datagram Protocol (S. D. Bruda) Winter 2018 10 / 16

UNCONNECTED SOCKETS An unconnected socket does not store the coordinates of a peer So the servers must use this kind of socket (they have more than one peer usually) The clients may use unconnected sockets (especially when they communicate with more than one server) To send through an unconnected socket, we use int sendto(int socket, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) How do we obtain the address of the peer? The User Datagram Protocol (S. D. Bruda) Winter 2018 11 / 16

UNCONNECTED SOCKETS (CONT D) How do we obtain the address of the peer? We create it, as we did for the call to bind The User Datagram Protocol (S. D. Bruda) Winter 2018 12 / 16

UNCONNECTED SOCKETS (CONT D) How do we obtain the address of the peer? We create it, as we did for the call to bind This is how we implement broadcast: we create a sockaddr structure containing the IP address INADDR_BROADCAST The User Datagram Protocol (S. D. Bruda) Winter 2018 12 / 16

UNCONNECTED SOCKETS (CONT D) How do we obtain the address of the peer? We create it, as we did for the call to bind This is how we implement broadcast: we create a sockaddr structure containing the IP address INADDR_BROADCAST When we send a reply, recvfrom gives us the reply address In addition to the buffer that holds the received message, a second buffer is filled in with the address of the sender int recvfrom(int socket, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen); So we do something like this: // other data (sd, request, rsize, etc.) // declared and initialized as appropriate struct sockaddr_in peer; socklen_t psize; r = recvfrom(sd, request, rsize, 0, (struct sockaddr*)&peer, &psize); // check r, prepare the buffer reply sendto(sd, reply, strlen(reply), 0, (struct sockaddr*)&peer, psize); The User Datagram Protocol (S. D. Bruda) Winter 2018 12 / 16

ON UDP UNRELIABILITY Our client and server algorithms ignore one crucial aspect of UDP communication: unreliability UDP communication semantics: unreliable, or best effort delivery Clients and servers must implement reliable communication all by themselves We can use timeout and retransmission mechanisms But then this introduces the problem of those pesky duplicate packets, which must also be handled Adding reliability can be difficult, and is closely related to the semantics of the application protocol Reliability can be approached in two ways: Ignore the problem. Do nothing, and so accept the possibility of dropped messages Deal with the problem. Implement control algorithms using message sequencing, acknowledgments, timeouts and retransmissions We thus end up with another implementation of TCP (so we would be better off using TCP in the first place) The User Datagram Protocol (S. D. Bruda) Winter 2018 13 / 16

TO USE OR NOT TO USE One should in principle prefer TCP Useful features already implemented: reliability, point-to-point, flow control Less burden on the application programmer In fact most Internet services use TCP precisely for these reasons Major reasons for not using TCP: speed and bandwidth TCP introduces a significant communication overhead (in terms of both bandwidth and time) Some applications do not tolerate this overhead Typical examples: games, real-time video streaming, VOIP This kind of applications will typically use UDP They usually use the do not care approach to reliability! We do not care about a frame dropped now and then; it is more important that say, the video stream is delivered in real time If we start implementing reliable communication we introduce the same kind of overhead we had problems with in the first place The User Datagram Protocol (S. D. Bruda) Winter 2018 14 / 16

TO USE OR NOT TO USE (CONT D) Another reason for using UDP: broadcast/multicast capabilities Good example: the DHCP protocol A machine can obtain its IP address and other routing information automatically from a DHCP server However, the machine cannot contact the server directly It has no idea how to send packets to a precise destination at all! Indeed, it does not even know its IP address DHCP is thus a UDP application The client broadcasts blindly a discovery packet (impossible under TCP) The quickest DHCP server within reach responds with the IP address, routing information, etc... with a broadcast message of its own The User Datagram Protocol (S. D. Bruda) Winter 2018 15 / 16

ADDENDUM TO MULTISERVICE SERVERS The concept of multiservice servers extends of course to UDP servers The same idea as for TCP: multiple threads listen to multiple ports and serve different types of clients The difference is that there are no slave UDP threads In addition, it is often the case in practice that we have multiprotocol servers That is, servers that accept both TCP and UDP clients Typically, such a server serves the same kind of requests arriving on both TCP and UDP ports Child thread Child thread Child thread Parent thread Parent thread Parent thread UDP thread communicate connect requests connect requests connect requests TCP clients being served new clients (of different types) UDP UDP clients (no notion of incoming/existing clients here) The User Datagram Protocol (S. D. Bruda) Winter 2018 16 / 16