



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















5
docsity.com
perror(“socket”);exit(1);}} • socket returns an integer (
socket descriptor
)
-^ fd^ < 0 indicates that an error occurred• AF_INET: associates a socket with the Internet protocol family• SOCK DGRAM:
selects the UDP protocol
8
-^ SOCK_DGRAM: selects the UDP protocol
docsity.com
int^ fd;^
/* socket^ descriptor
int^ fd;^
/^ socket^ descriptor
struct^ sockaddr_in
srv;^ /* used
by^ bind()^ / / create^ the
socket^ / / bind:^ use
the^ Internet
address^ family
srv.sin_family
/* bind:^ socket
‘fd’^ to^ port
/^ bind:^ socket
fd^ to^ port
srv.sin_port^
=^ htons(^80 ); /*^ bind:^ a^ client
may^ connect^
to^ any^ of^ my^
addresses^ */
srv.sin addr.s
addr^ =^ htonl(
srv.sin_addr.s_addr
htonl( INADDR_ANY
if( bind (fd,^
(struct^ sockaddr*)
&srv,^ sizeof(srv))
perror("bind");
exit(1); }
9
} • Now the UDP server is ready to accept packets…
docsity.com
UDP Server^ socket()bind() UDP Cli^ t^
UDP^ Client socket()
blocks until datagramreceived from a client
received from a client data request
data reply
Lecture 3: 9-4-
14
from UNIX Network Programming Volume 1, figure 8.
15
17
18
20
21