Docsity

Elementary UDP Sockets-Network Programming-Lecture Slides, Slides of Network Programming

Partial preview of the text

Download Elementary UDP Sockets-Network Programming-Lecture Slides and more Slides Network Programming in PDF only on Docsity!

Network^ Programming(Elementary^

UDP^ Sockets)

UDP

-^ UDP^ uses^ a^ simple

transmission^ model

without^ implicit hand‐shaking^ dialogues

for^ providing^ reliability, ordering,^ or^ data^ integrity. • UDP^ assumes^ that

error^ checking^ and

correction^ is either^ not^ necessary

or^ performed^ in^ the

application, avoiding^ the^ overhead

of^ such^ processing.

-^ useful^ for^ servers

answering^ small^ queries

from^ huge numbers^ of^ clients.

Unlike^ TCP,^ UDP^ is

compatible^ with packet^ broadcast^ (sending

to^ all^ on^ local^ network)

and multicasting^ (send

to^ all^ subscribers).

UDP

-^ UDP^ is^ a^ connectionless,

unreliable,^ datagram

protocol,^ quite^

unlike^ the^ connection

oriented,^ reliable

byte^ stream^ provided

by

TCP. • Some^ popular^ applications

are^ built^ using

UDP:^ DNS,^ NFS,

and^ SNMP.

Continued..

-^ The^ client^ does

not^ establish^ a^ connection

with the^ server.^ Instead,

the^ client^ just^ sends

a datagram^ to^ the

server^ using^ the

sendto function

-^ requires^ the^ address

of^ the^ destination

(the server)^ as^ a^ parameter. • Similarly,^ the^ server

does^ not^ accept

a^ connection from^ a^ client. • Instead,^ the^ server

just^ calls^ the^ recvfrom function,^ which^ waits

until^ data^ arrives

from some^ client. • recvfrom returns

the^ protocol^ address

of^ the client,^ along^ with

the^ datagram,^ so

the^ server^ can send^ a^ response

to^ the^ correct^ client

recvfrom and

sendto Functions

#include^ <sys/socket.h>ssize_t^ recvfrom(int^ sockfd,

void^ *buff,^ size_t^ nbytes,^ int

flags,^ struct^ sockaddr^ *from,

socklen_t

*addrlen);ssize_t sendto(int sockfd,^ const

void^ *buff,^ size_t nbytes,^ int flags,

const^ struct sockaddr *to, socklen_t addrlen);

Both^ return:^ number^ of^ bytes

read^ or^ written^ if^ OK,^ –1^ on

error docsity.com

recvfrom and

sendto Functions

-^ The^ first^ three

arguments,^ sockfd,

buff,^ and

nbytes,^ are^ identical

to^ the^ first^ three

arguments^ for^ read

and^ write:^ descriptor,

pointer^ to^ buffer

to^ read^ into^ or^

write^ from,

and^ number^ of^

bytes^ to^ read^ or

write.

-^ Flag^ is^ 0.

recvfrom and

sendto Functions

-^ The^ final^ two^ arguments

to^ recvfrom are

similar to^ the^ final^ two^ arguments

to^ accept:^ The contents^ of^ the^ socket

address^ structure

upon return^ tell^ us^ who

sent^ the^ datagram

(in^ the^ case of^ UDP)^ or^ who^ initiated

the^ connection^

(in^ the case^ of^ TCP). • The^ final^ two^ arguments

to^ sendto are^ similar

to the^ final^ two^ arguments

to^ connect:^ We^

fill^ in^ the socket^ address^ structure

with^ the^ protocol address^ of^ where

to^ send^ the^ datagram

(in^ the case^ of^ UDP)^ or^ with

whom^ to^ establish

a connection^ (in^ the

case^ of^ TCP)

recvfrom and

sendto Functions

-^ Both^ functions

return^ the^ length

of^ the^ data

that^ was^ read^ or

written^ as^ the^ value

of^ the

function. • In^ the^ typical^ use

of^ recvfrom,^ with

a

datagram^ protocol,

the^ return^ value

is^ the

amount^ of^ user

data^ in^ the^ datagram

received. docsity.com