240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative...

41
0-322 Cli/Serv.: sockets 3/9 Client/Server Distributed Syste Client/Server Distributed Syste ms ms Objectives Objectives describe iterative clients and describe iterative clients and servers using the UDP protocol servers using the UDP protocol 240-322, Semester 1, 2005-2006 9. Sockets (3)

Transcript of 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative...

Page 1: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 1

Client/Server Distributed SystemsClient/Server Distributed Systems

ObjectivesObjectives– describe iterative clients and servers using the describe iterative clients and servers using the

UDP protocolUDP protocol

240-322, Semester 1, 2005-2006

9. Sockets (3)

Page 2: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 2

OverviewOverview

1. Why use UDP?1. Why use UDP?

2.2. The UDP Protocol The UDP Protocol

3.3. A Connectionless Example A Connectionless Example

4.4. A Client for the UDP A Client for the UDP daytimedaytime ServiceService

Page 3: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 3

1. Why use UDP?1. Why use UDP?

The connectionless protocol, UDP, has The connectionless protocol, UDP, has two big drawbacks:two big drawbacks:– datagrams may arrive in any orderdatagrams may arrive in any order– datagrams may be lostdatagrams may be lost

TCP does not have these problems, so TCP does not have these problems, so why use UDP?why use UDP?

Page 4: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 4

UDP UDP is reliable is reliable over a LANover a LAN– it is very unlikely that datagrams will be lost or reordered unless it is very unlikely that datagrams will be lost or reordered unless

there is a hardware problemthere is a hardware problem

UDP is less resource-intensive than TCPUDP is less resource-intensive than TCP

Use UDP if the client-server communication is simple, Use UDP if the client-server communication is simple, such as:such as:– get get fingerfinger information from a server information from a server– get time/day informationget time/day information

– messages must be small and self-containedmessages must be small and self-contained

Page 5: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 5

2. The UDP Protocol2. The UDP Protocol

socket()

bind()

recvfrom()

blocks until datareceived from a client

process request

sendto()

socket()

bind()

recvfrom()

data (request)

data (reply)

sendto()

Server

Client

Page 6: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 6

NotesNotes

The system calls for reading and writing are The system calls for reading and writing are different from TCP.different from TCP.

The client does not need to connect with the The client does not need to connect with the server using server using connect()connect()(although it is (although it is possible).possible).

The server does not need to call The server does not need to call accept()accept()..

Page 7: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 7

Communication DiagramCommunication Diagram

server

client

client

client

Page 8: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 8

sendto()sendto() Send a message from a socket to an address:Send a message from a socket to an address:

#include <sys/types.h>#include <sys/socket.h>

int sendto(int sd, char *msg, int len, int flags, struct sockaddr *addr, int addrlen);

Return the length of the sent message if ok, Return the length of the sent message if ok, -1 on error.-1 on error.

Page 9: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 9

sendto() Diagramsendto() Diagram

sendto(sd, msg, len, flags, addr, addrlen);

socket descriptor

len

data tobe sent

special deliveryoptions; usually 0

addrlen

address ofdestination

Page 10: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 10

recvfrom()recvfrom()

Receive a message at a socket from an address:Receive a message at a socket from an address:

#include <sys/types.h>#include <sys/socket.h>

int recvfrom(int sd, char *msg, int len, int flags, struct sockaddr *addr, int *addrlen);

Return the length of the received message if ok, -Return the length of the received message if ok, -1 on error.1 on error.

Page 11: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 11

recvfrom() Diagramrecvfrom() Diagram

n = recvfrom(sd, msg, len, flags, addr, &addrlen);

socket descriptor

len

received dataplaced here

special options; usually 0

addrlen

address ofsender

n

Page 12: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 12

Other send/receive FunctionsOther send/receive Functions

#include <sys/types.h>#include <sys/socket.h>

int send(int sd, char *msg, int len, int flags);int recv(int sd, char *msg, int len, int flags);

int sendmsg(int sd, struct msghdr msg[], int flags);

int recvmsg(int sd, struct msghdr msg[], int flags);

Page 13: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 13

send()send() and and recv()recv() are used when the client has are used when the client has used used connect()connect() to specify its server to specify its server– may not be supported in the futuremay not be supported in the future

sendmsg()sendmsg() supports supports gather-writegather-write– collect data from different places in memory, and send collect data from different places in memory, and send

as a single datagramas a single datagram

recvmsg()recvmsg() supports supports scatter-readscatter-read– split received datagram into separate places in memorysplit received datagram into separate places in memory

Page 14: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 14

3. A Connectionless Example3. A Connectionless Example

3.1. A UDP 3.1. A UDP echoecho Server Server

3.2. A UDP 3.2. A UDP echoecho Client Client

3.3. Usage3.3. Usage

Page 15: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 15

3.1. A UDP echo Server3.1. A UDP echo Server

Receive a string (ending in Receive a string (ending in ‘\n’‘\n’) from a client, ) from a client, convert it to uppercase, and send it back.convert it to uppercase, and send it back.

A simple communication protocol.A simple communication protocol.

Messages are short and self-contained.Messages are short and self-contained.

Page 16: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 16

echo_userv.c echo_userv.c

#include <stdio.h>: /* usual includes */

#include <ctype.h> /* for toupper() */

#define PORT 3667#define BUFSIZE 128

void touppers(char buf[]);int udp_serv_sock(int port);

:

Page 17: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 17

int main(){ int sd, client_len, n; struct sockaddr_in client; char msg[BUFSIZE+1]; sd = udp_serv_sock(PORT);

while(1) { client_len = sizeof(client); n = recvfrom(sd, msg, BUFSIZE, 0,

(struct sockaddr *)&client, &client_len);

:

Page 18: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 18

if (n < 0) fprintf(stderr, "recvfrom failed\n"); else {/* msg[n] = '\0'; printf("n: %d, msg: \"%s\"\n", n, msg); */ touppers(msg); if (sendto(sd, msg, n, 0, (struct sockaddr *)&client,

client_len) != n) fprintf(stderr, "sendto error\n"); } } return 0;}

Page 19: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 19

int udp_serv_sock(int port)/* Bind a socket with the server's address using the UDP protocol.*/{ int sockfd; struct sockaddr_in serv_addr;

/* create a UDP socket */ if ((sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0) { fprintf(stderr, "Could not open a socket"); exit(1); }

:

Page 20: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 20

/* initialise socket address */ memset((char *)&serv_addr, 0,sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);

serv_addr.sin_port = htons(port);

/* bind socket to address */ if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { fprintf(stderr, "Could not bind socket

to address\n"); exit(1); } return sockfd;}

Page 21: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 21

NotesNotes main()main() reads the client information from the reads the client information from the recvfrom()recvfrom() call. Then it knows where to send the call. Then it knows where to send the reply with reply with sendto()sendto()..

udp_serv_sock()udp_serv_sock() is the same as is the same as tcp_serv_sock()tcp_serv_sock() (part 7, slide 10) except for the (part 7, slide 10) except for the socket()socket() type, type, and no and no listen()listen() call. call.

We cannot test this server with We cannot test this server with telnettelnet because because telnettelnet uses the TCP protocol. uses the TCP protocol.

Page 22: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 22

3.2. A UDP echo Client3.2. A UDP echo Client

Read server details from the command line:Read server details from the command line:$ ./echo_ucli takasila 3667$ ./echo_ucli takasila 3667

main()main() reads a line from reads a line from stdinstdin, sends it to the UDP , sends it to the UDP echoecho server, and then prints the reply to server, and then prints the reply to stdoutstdout..

The program continues until the user types The program continues until the user types EOFEOF ((control-Dcontrol-D).).

Page 23: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 23

echo_ucli.cecho_ucli.c

#include <stdio.h>: /* usual includes */

#include <unistd.h>

#define PORT 3667#define BUFSIZE 128

struct sockaddr_in get_addr(int argc, char *argv[]);

int open_udp_sock(void);:

Page 24: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 24

int main(int argc, char *argv[]){ struct sockaddr_in serv_addr; int sd, serv_len, n; char buf[BUFSIZE+1];

serv_addr = get_addr(argc, argv); serv_len = sizeof(serv_addr);

sd = open_udp_sock();:

Page 25: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 25

while (fgets(buf, BUFSIZE, stdin) != NULL) { n = strlen(buf); if (sendto(sd, buf, n, 0, (struct sockaddr *)&serv_addr,

serv_len) != n) fprintf(stderr, "sendto failed\n"); else { if ((n = recvfrom(sd, buf, BUFSIZE, 0, (struct sockaddr *)0, (int *)0)) < 0) fprintf(stderr, "recvfrom failed\n"); else { buf[n] = '\0'; fputs(buf, stdout); } } } close(sd); return 0;}

Page 26: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 26

int open_udp_sock(void)/* Bind to any local address */{ int sd; struct sockaddr_in addr;

if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { fprintf(stderr, "socket() error\n"); exit(1); }

:

Page 27: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 27

memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(0);

if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {

fprintf(stderr, "Can not bind local address\

n"); exit(1); } return sd;}

Page 28: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 28

NotesNotes

get_addr()get_addr() is taken from part 7, slide 77. is taken from part 7, slide 77.

open_udp_sock()open_udp_sock() attaches any free port attaches any free port (between 1024-5000) to the socket by using (between 1024-5000) to the socket by using 00 as the port value. as the port value.– this port is used by the server when it repliesthis port is used by the server when it replies

Page 29: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 29

3.3. Usage3.3. Usage

$ ./echo_userv & /* on takasila */[1] 1745$ netstat -a | grep 3667udp 0 0 *:3667 *:*

$ ./echo_ucli takasila /*on fivedots*/helloHELLOgood byeGOOD BYE$

control-D typed

F T

Page 30: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 30

4. A Client for the UDP daytime Service4. A Client for the UDP daytime Service

The The daytimedaytime service can be contacted by using service can be contacted by using TCP TCP oror UDP. Shown in UDP. Shown in /etc/services/etc/services::

:daytime 13/tcpdaytime 13/udp

It's also necessary to check It's also necessary to check /etc/inet.conf/etc/inet.conf to to see if the service is switched onsee if the service is switched on– usually most UDP services are disabled due to usually most UDP services are disabled due to

hacker attack weaknesseshacker attack weaknessescontinued

Page 31: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 31

The availability of the serviceThe availability of the service via TCP via TCP can can be checked with telnet:be checked with telnet:– telnet <host> 13telnet <host> 13

Execute the Execute the daytimedaytime UDP client (called UDP client (called day_cusday_cus) with the server’s hostname as a ) with the server’s hostname as a command line argument:command line argument:

$ ./day_cus calvin$ ./day_cus calvin

Page 32: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 32

day_cus.cday_cus.c

#include <stdio.h>: /* usual includes */

#include <unistd.h>

#define MSG ”\n" /* dummy 'wake-up' msg */#define BUFSIZE 128

struct sockaddr_in get_udp_serv_addr(char *host, char *service);

int open_udp_sock(void);:

Page 33: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 33

int main(int argc, char *argv[]){ struct sockaddr_in serv_addr; int sd, serv_len, n; char buf[BUFSIZE+1];

serv_addr = get_udp_serv_addr(argv[1], "daytime");

serv_len = sizeof(serv_addr);

sd = open_udp_sock(); n = strlen(MSG);

:

Page 34: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 34

if (sendto(sd, MSG, n, 0,(struct sockaddr *)&serv_addr, serv_len) != n)

fprintf(stderr, "sendto failed\n"); else { if ((n = recvfrom(sd, buf, BUFSIZE, 0, (struct sockaddr *)0,

(int *)0)) < 0) fprintf(stderr, "recvfrom failed\n"); else { buf[n] = '\0'; fputs(buf, stdout); } } close(sd); return 0;}

Page 35: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 35

struct sockaddr_in get_udp_serv_addr(char *host, char *service)

/* Build a socket address struct for a system service using the host name (given in dotted-decimal or dotted-name format) and the service name. */{ struct sockaddr_in addr; unsigned long lgaddr; struct hostent *hp; struct servent *sp; if ((sp = getservbyname(service, "udp"))

== NULL) { fprintf(stderr, "Unknown service\n"); exit(1); }

:

Page 36: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 36

memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = sp->s_port;

if ((lgaddr = inet_addr(host)) != -1) addr.sin_addr.s_addr = lgaddr; else if ((hp = gethostbyname(host)) != NULL) memcpy(&addr.sin_addr.s_addr,

hp->h_addr, hp->h_length); else { fprintf(stderr, "Unknown address\n"); exit(1); } return addr;}

Page 37: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 37

NotesNotes The client must ‘wake up’ the server by The client must ‘wake up’ the server by

sending it a dummy message (sending it a dummy message (MSGMSG). ).

The server uses the client address The server uses the client address information included in the information included in the sendto() sendto() call call to direct its answer.to direct its answer.

continued

Page 38: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 38

get_udp_serv_addr()get_udp_serv_addr() is the same as is the same as get_tcp_serv_addr()get_tcp_serv_addr() (part 7, slide 100) (part 7, slide 100) except that the service type in except that the service type in getservbyname()getservbyname() is is “udp”“udp”..

Page 39: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 39

UsageUsage

$ ./day_cus calvin /*on fivedots*/Wed May 4 09:42:33 2005

$ ./day_cus fivedotsWed May 4 09:42:39 2005

The usual response is to getnothing back; day_cus hangs,waiting forever

Page 40: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 40

Timeouts in UDPTimeouts in UDP Have the code wait for a message for a certain aHave the code wait for a message for a certain a

mount of time, then mount of time, then timeouttimeout..

Three main approaches:Three main approaches:– use the timeout feature of select();use the timeout feature of select();

– set an alarm which interrupts the waiting process aftset an alarm which interrupts the waiting process after a certain amount of time;er a certain amount of time;

– configure the socket to timeout using (non-standard) configure the socket to timeout using (non-standard) socket options.socket options.

continued

Page 41: 240-322 Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol 240-322,

240-322 Cli/Serv.: sockets 3/9 41

Details in:Details in:– UNIX Network ProgrammingUNIX Network Programming (Vol. 1: Networki (Vol. 1: Networki

ng APIs: Sockets and XTI)ng APIs: Sockets and XTI)W. Richard StevensW. Richard Stevens