D$EFHGHEIJ3JLKHK MON
Autunno 2003
2
PRATICA (Lez. 1)
#include <stdio.h>
int main(int argc, char **argv) {
union {
short s;
char c[sizeof(short)];
} un;
un.s = 0x0102;
printf("CPU = %s - byte ordering: ",getenv("CPU"));
!
#"$%& '
(
if (sizeof(short) == 2) {
if ( un.c[0] == 1 && un.c[1] == 2 )
printf ("big-endian\n");
else if ( un.c[0] == 2 && un.c[1] == 1 )
printf ("little-endian\n");
else
printf("unknown\n");
}
else
printf("size of short: %d.\n",sizeof(short));
PRATICA - Lezione 1
)*+ ,-./+ 0132-4$5 +6/ 072+62+895 -.*:
;97.-93-3<+ =5 :>?93+ *A@*B:.>?90+ C9
N#FHJQP$K R ISHK FHM$TQTQUVIWTXK R T
exit(0);
}
UIWTXK R T
Autunno 2003
3
Autunno 2003
4
PRATICA (Lez. 1)
Y[Z\]^`_ a?bdcHebgfihjgkH^`lnm?o \ j#e^
PRATICA (Lez. 1)
#Makefile
ALL = lib-errori lib-corso-reti \
daytimesrv daytimecli daytimesrv-ric \
echosrv echocli echosrv-sigh
Ypfq^`lgm?o \ jq_rbis%ctm%_ ^?a`_ujqlvlwjgkH^`cH]?o ]ebxcHbq\
yHz|{ EI}~XFHE~JLIET3K RH}FHEG$TM { T3NK MO€FH$K €TLFG$G$T {u{ F
‚ z„ƒ M$K ET { IR TL€FH$K €TLFG$G$T {r{ Fw€FHMLR TLR K …$ETEK TQ† R K M$W‡
all: $(ALL)
.c:
Ypa?kHk|ˆ~‰‹ŠŒŽffq^`lgm?o \ bq_
G$€€R }’H“ €
G$€€•”%FwR }’wR }’“ €
G$€€•”%–J`—R K …$P$I {˜ ”?R J`—R K …”?™ J`—K M$€P$I {˜ ”%š›”?œœŸž$ 6¡%¢
“““
‘
‘
‘
lib-errori: lib-errori.c
gcc -g -O0 -Werror -c lib-errori.c
lib-corso-reti: lib-corso-reti.c
gcc -g -O0 -Werror -c lib-corso-reti.c
> clean:
Y[£Vj¤b#¥o \ b
‘
lib-errori.o lib-corso-reti.o
@echo compiling $< with rule 1
gcc $< -g -O0 –Werror -o $@ lib-errori.o lib-corso-reti.o
rm -f $(ALL)
rm -f *~
rm -f *.o
JQIWTL“ “ “XI {ƒ{u{ F
ž$}TJQP$K F
–K …$ETEK TLTLK M$€R ƒ $TdXK R T
Autunno 2003
5
Autunno 2003
PRATICA (Lez. 1)
robdep@zircone:~/Corsi/Reti/C> gmake
gcc -g -O0 -Werror -c lib-errori.c
gcc -g -O0 -Werror -c lib-corso-reti.c
compiling daytimesrv.c with rule 1
gcc daytimesrv.c -g -O0 -Werror -o daytimesrv lib-errori.o
lib-corso-reti.o
compiling daytimecli.c with rule 1
gcc daytimecli.c -g -O0 -Werror -o daytimecli lib-errori.o
lib-corso-reti.o
compiling daytimesrv-ric.c with rule 1
gcc daytimesrv-ric.c -g -O0 -Werror -o daytimesrv-ric liberrori.o lib-corso-reti.o
compiling echosrv.c with rule 1
gcc echosrv.c -g -O0 -Werror -o echosrv lib-errori.o libcorso-reti.o
……
6
PRATICA (Lez. 1)
Y[¦Qo cHk%\ sH§?bi¨LcH^`lwbd©~ª•«
N#TE€ILK RXK R TQ$IQK M$€R ƒ $TETQM$TR R TL$K ET€ { FE —¬$KEK €TE€I
‘
} { IM$$IEw$TRH€FJQP$K R I { FET
‘®­ ƒ }E ­ K M$€R ƒ $T¯ ­ ƒ }E ­ R K … ­ K M$€R ƒ $T¯°“ “ “
Y[¦±o cHk%\ sH§?bg²cH^`lwbd©~ª%³
N#TE€ILK RXK R TQ$IQK M$€R ƒ $TETQM$TR R IL€~´w
‘
YpµA\?lO^`lwbqc$e^|§`ob#]b#a`s%o _ubio \?\ o c%¤¶o \kH^`lgm?o \ j#e^`_rbgkHbd_rkj
o \kH^?§`o kHbicHb#kHb]]jd_o ^·cHbd\ \ bi\ o ¸?_ bd_o b
R K …$ETEK TQ}P$T€K XK €I { TL€FHM`”%RHM$TR€FHJLIM$HFw$KH€FHJQP$K R ISHK FHM$T
R ILEK €TE€IL$K { IR KHR K …$ETEK TQ¹XI {~{ IQK MwP$FH} { KH} { IM$$IE
‘
† ­ ƒ }E ­ R K …$¯ ­ ƒ }E ­ R FH€IR ­ R K …$¯°“ “ “ ‡qTLM$TR R T3$K ET€ { FHE —¬}P$T€K XK €I { T
‘
‘
€FHM”%–
R K …$ETEK IQ$K$TXI ƒ R { † €FM { K TM$T3P$EK M { X‡
I}K €“ ˜
Autunno 2003
7
PRATICA (Lez. 1)
Y
#ifndef __BASIC__
#define __BASIC__
#include
<sys/types.h>
#include
<sys/socket.h>
#include
<sys/time.h>
#include
<time.h>
#include
<netinet/in.h>
#include
<arpa/inet.h>
#include
<errno.h>
/* basic system data types */
/* basic socket definitions */
/* timeval{} for select() */
/* timespec{} for pselect() */
/* sockaddr_in{} and other Internet defns */
/* inet(3) functions */
Y
‘
/* for Unix domain sockets */
Y
‘
#endif
! "$# %'&(&(%)%* +-, +.%/0, %1/3204-, %
"$57 698 :;%%* +, +.<%/=, %>/?204', %
"$#%'&(&(%>@(/7+A8 :&(+
?
`sHb]e ji\ o ¸?_ bd_o jim$bq_VsH]j_ b
R € ƒ M$T { TE~JLK M$IM$FwR T}T€ ƒ S$K FHM$TL$TRP$E~FHG$EIJQJQI
#o \ bi\ o ¸ rbd__ ^`_o © k
‘
–K …$ETEK IL€FHE}FLET { K
8
PRATICA (Lez. 1)
$^`cH^v¥
s%c °o ^`c?ok%ªHbg]e jqlnmjqcH^„s%ctlwb#]]j#a?a`o ^|§`o
bd__ ^_ b
#define MAXLINE 256
#define PORT 12345
#define BACKLOG 5
#define MAX(a, b) ((a) > (b) ? (a) : (b))
Y
j#kk%o jqlw^„o \?\ o c%¤QkH^`c
e jd\ o¥
s%c °o ^`c%o
Y
<unistd.h>
<sys/wait.h>
<sys/un.h>
Autunno 2003
Ÿb#¥
o c%o ]kHbghjd_o bg¥s•c #o ^`c%o?mbq_V\ jga%b#]eo ^`c$bg§?b°a`\ o
bd__ ^_o
……
#include
#include
#include
–K …$ETEK ILG$T} { K FHM$KTEEFEK
} { I JQP$IL}FR FwR TE~EFHET
€ ˜ K IJQIM$FwT K {
TEE %JQ}G
ƒ K { ¯°TEE %}~—}
TEE
Autunno 2003
9
PRATICA (Lez. 1)
Ÿb#¥o %
c o ]kHbghjd_o bg¥
s%c °o ^`c%o?mbd_V\ ji\ b#ees_ jnb
]k?_o e e%
s _ jg§%bdo]^?k?¤
be
Ypfq^`cHhb #o ^`cHbg]?s%\?cH^`lwb
ET { K
‘
& '(
!#"$%
H]bqlnm?o§`o¥s%c #o ^`c•o§?bq\ \ ji\ o ¸?_ b_o j
ET { K %ETI$M
‘
‘
ET { K H´LEK { TM
‘
ET { K %ETI$R K M$T
PRATICA - Lezione 2
Ÿb#eej#a`\ o?c$bq\¥
o \ bi\ o ¸ rk^`_ ]^ _ b#e
o © k
C
D E <
F HG AFI F
B
)#*+ ,-./+ 01Q2-4H5 +6/ 072+62+6895 -.*:
;97.-93-Q<6+ =5 :>?93+ *A@*B:.>?90+ C9
F€WT {
Autunno 2003
11
PRATICA (Lez. 2)
Y
b#ee bd_rjq\ lObdcHe bg]°o a`c%o ¥
o kHjg²m%_rb]j³3ˆ~§`ok^`__ bqc$ebq
Y
t\ j#]e_uj °o ^`cHbg§`o?s•ckHjdcHjd\ bg§`okH^`lgs%c•o kHj °o ^`cHbn¥_ j
§`sHbgkH^`lgm?s$ebq_kH^`c%cHb#]]?o§?jis%c$ji_ b#eb
Y
<
bd_ Ÿf
HZ |s%c¬]^?k%¤b#eLo §?bqcHeo ¥o kj·o§`sHbim?s%c$e
o
‘
‘
§?bd\ \ jnk^`c•cHb]]?o ^`cHb
¡%MOK M$$K EK SSHFL™ L
D T
¡%MOK M$$K EK SSHFL™ L
D T
ƒ MS$K FHM$KHP$TEŸKH}FH€WT {
ƒ M$IQP$FE { I3} O
ƒ ƒ M ˜ FH} {
ƒ M$IQP$FE { 3
I } ƒ R R IR { E F ˜ FH} {
Autunno 2003
12
PRATICA (Lez. 2)
socket()
bind()
SKT L RM N O L P-UTQ R PN
V
O
P
W
U
X
L
'
N
Q
R
P
R
N
L
U
R
N
S
S
T
T
YP O U Z [ US \ UN L O P ]
YP O U Z [ UL M T L O P ]
^ T O U_ S P ` U T R Na U _ U R N S T b Q R US P ` UT R N
listen()
socket()
H^`cH^|§?b#¥o c%o eo?mbd_•hjq_o?m?_ ^%e ^?kH^`\ \ o
Y
J
c : d(:6e+>: /=, %8 +'fg:h/=%>: /i@(/0+6jh//7%*7*=:h/=% c<k9l
CLIENT
Y
#
connect()
write()
read()
close()
accept()
read()
write()
read()
close()
SERVER
Y
B
struct in_addr {
in_addr_t
}
struct sockaddr_in {
uint8_t sin_len;
sa_family_t
in_port_t
struct in_addr
char
}
struct sockaddr {
uint8_t sin_len;
sa_family_t
char
}
FH€WI$$E %K M
– ƒ M$G ˜ TSS$TQ} { E ƒ{~{ƒ ETL}FH€WT {
Autunno 2003
13
Autunno 2003
14
PRATICA (Lez. 2)
s_addr;
/* 32-bit, network byte ordered */
sin_family;
sin_port;
sin_addr;
sin_zero[8];
/* tipo di protocollo, AF_INET */
/* 16-bit, network byte ordered */
/* struttura indirizzo IP */
PRATICA (Lez. 2)
" *=: / f7% 8 h
!#" $&%'( " *=: / *+/)%!/& !, ! 01 !& ! &&-! . 32
sin_family;
sa_data[14];
/* tipo di protocollo: AF_XXX */
/* indirizzo specifico del protocollo */
J
I
ƒ MS$K FM$TL}F€WT {
15
M
?416798 : ;
PRATICA (Lez. 2)
>
FN O]bnbq__r^`_rb
QPSR
B
UT
B
B"$8 %-+Z=[\i8 :, %3[j6 h* %1F* Wh/=h %1* Y, %*=* %U]=@(/7f(: h/(:Z(X%W :(]=: % W
)
)
)
ƒ MS$K FM$TL€FHM$M$T€ {
*V
Autunno 2003
: /7,: /76j6 h@ /((/7% %'6j, *4: /=*, '* h6 * %-,e,8 @06e,* h-6 + 8 * h6 %/ ,
+ h8 % :8 :, h8 /7h 3* % %8 h8 % * %
F
E 'F
G 1F
E L• 3F• B
—–˜–š™ J B
*› B • –š™ C›
17
PRATICA (Lez. 2)
bd_lOb#ee bnj#§·s%c¬k%\ o bqc$e`§`ojdm%_o _rbxs%cHjnk^`c•cHb]]#o ^`cHb
kH^`cto \]bd_ hbd_
Y[Z\?¤
bq_cbq\]kHba`\ o bis•cHjim^`_ ejgb#¥ ¥o lwbd_rjiˆ~bi\ o cH§`o _o ^
Ÿf
: /7,: /062(: @/ (% : /0<, * 4 * '** h-,=68 @76j%,, * h6 + 8 * h6 %/ ,
+ h8 % :-8 :, h8 /0h 3* % %8 h8 % * %
F
G AF E W JŸ
‘
‘
ž Ÿ™ UVžHœšd¡
ž$Nš
°ž ¡
ž š q¡
°ž
ž$œ
N
ƒ MS$K FHM$TQ…$K MH
Autunno 2003
18
~ ) Z œ  q 1i lu‚
Z=j bƒ1„u…
rq ZuZ (†žŠt„L„uˆ([ q ) CŠ„u„uˆ ‹ ‡
ŒnŽ
 ) Z
=J‘ ’ “
3[” œh v
Y
PRATICA (Lez. 2)
bq_l‹bee bvj§„s%c¬]bd_rh6bd_§`oj#]]b#a cHjd_ bis%cto c$§`o _o ^
mbq_Vo \]bd_rhb_jq\]^?k%¤b#e
YpfŸ^`c
‘
‘
Ÿf v\ o cH§`o _o ^„m?s vb#]]bd_ b
K M$$K EK SS$FL™DO† $T #T3
T }}TET ƒ M$IL$TR R TLK M { TE XI€€T‡
P$FHE { I
TM { EIJQ…$K
M$T}} ƒ M$F
T
E -FT J¥
‘
ho bqcHbn¥j#ee ^
‘
\ ª$jqcH§?]?ªHjd¤o c$a do c¬kHj]^v§`obd__ ^`_ubx_o e ^`_cHj
‘
¯ { EIM$M$TL€ ˜ TLP$TE
J
Z ?
Y[Šbq\kHj#]^|§`o?s%cHjnk^`c?c$b]]°o ^cHb
ƒ IR T`XIJLK G$R K IL$KPHEF { FH€FHR R KH}K
Y[o cHe
‘
~ ) (Z €  rq Hi lu‚
Hj C(ƒ „u…
q tZuZ †ƒ‡
ˆ‰ Št„L„uˆ([ rq ) bŠ„u„uˆ ‹ ‡
Œn
Ž
 ) Z
=‘J’ “
J[” €h v
Y
3^
c f g
c Hi kj
rq ) ) )
¡%MOK M { TEFL€ ˜ TQ$K €TLK R { K P$FL$KH}FH€WT {
‘
T ƒ MOK M { TEFQ¹ ƒ }I { Fw€FJLTdXK R TL$T}€EK P { FHE•M$FHMOP ƒ
T}}TET ƒ }I { Fw€FJLTQ}FH€WT { $T}€EK P { FET #K €T TE}I
FH€WT { T`XK R TQ}FHM$F #K } { KHP$K OFQJLTM$FQIR R Fw} { T}}FwJQFH$F
J
16
PRATICA (Lez. 2)
Y[o cHe
H^?k%¤b#e`§%b#]k?_o me ^`_ gkH^`lwbis%c¬¥
o \ bn§?b]k%_o mHe ^`_
FHM$FLPHET}KH$IR R Fw} { T}}FwK M$}K TJLT
‘
‘
Autunno 2003
"`"`_b_baa **cc d=d=ee ccCg ll ;;
"`"`_b_baa *o# hhbkp _c # e d(57h8 h-6 , %-,eh-*6j+ d7%% 8 86 h:@=%,e/=: /7,& %>* %8 ;%8*7@ ) )hA* , %*7* hml7h* ,n
" + ),=8 t: s
?> ;=@DA
"" u55uhh kkvv *5y*c z*oo e__bw w d7d7%%88LZ(@(+'/7,h1+'& * ,e8 +8 %. +.x. :tZ j
:p Z(y +-l,e:tn j c<k l n
" u5 h k v *o _{ d7%8+d(d ) :6e+'fg:h/(: Z : 8 %, %>*=@ c l
D@FEG>1GIJG:
}|
u5 h k v Jo _{
‘
s%c]^?k%¤b#e`§?b#]k?_o me^`_]b
‘
¡%MOK M { TEFL€ ˜ TQ}P$T€K XK €I
K M { TM$$T ƒ }IET
54(6798 : ;=<Ao c$e?> ;=@#ACBdo cHeD@FEG>HGIJG:#LK
jq\ ^`_ bg§`o?_o e ^`_cH^
ƒ MSHK FHM$TQ}FH€WT {
Y[o cHe
¦Lo cHk%\ s$§?bi¨] ] ]^?k%¤b#e
©ª%«
o c$e`]^?k?¤
beˆo cHe
J
Autunno 2003
¡ ? cHbgho bqcb
`sHbd\ \ ^£¢go \ §?kjd_r§„ˆ™1–
œœ˜™
–=¤¯ |
Hbi\ jxm^`_ ejicH^`c g]?mbk%o ¥o kj#e jiˆh6jd\ ^`_ b
]kHbd\ e jis%cHjnb¥¥o lObd_rj
Y
Hbi\ o cH§`o _o ^·Z
ho bdcHbisH]j#e^ `s$bd\ \ ^v§?b#]?o a`cHj#e v
^ kH^`lwbiZ
§?b]eo cHj #o ^`cHbicHbd\ VŠŽ§?bd\k?\ o d
b cHe
-F Y

I
3<
J
2
ƒ MS$K FM$TLR K } { TM
6I€WR FHG
Autunno 2003
19
6I=: GdLK
M
Ao cHe
>
)G 1F
FN O]bgbd__ ^`_ubBF ‹]b9PSR
Y[Œ•]j#e jg]^`\ ^|§?jis%c¬]bd_ hbq_ Ÿf #
B ]Hbd_rhbnj
fq^`cHhbd_reo _ bio \]^?k%¤b#e`§%jgj°e eo h^|jxmj#]]°o h^FdB mb_•¥j_
]?k%ªHbio \?¤bd_cHbd\j#kkHb#eeok^`c•cHb]]?o ^`c%o]?s?\]^?k%¤b#e
D$TE•$TXI ƒ R { ƒ Mw}F€WT { ¹L€ETI { FwI {u{ K W FH¯°TQK RHWTEM$TRH}K
‘
I}P$T {u{ IL€ ˜ T3}K ILK RH}FH€WT { $K ƒ MO€R K TM {
–•TRH$K IG$EIJLJQIQIQ} { I { KŸ• N6DXIQJ ƒ FtW TETL$I h euy I
‘
c eud
# 5c
jq\ ^`_ bg§`o?_o e^`_cH^
Server
k# 5
ƒ MSHK FHM$TLI€€TP {
: /7,: /7+-6 6e@6e(%% d=, : */74, * '* h6 * ,e%-8 @7, 6j,* h-6 + 8 * h(6 %/ ,
+ h8 % :-8 :, h8 /7h 3* % %8 h8 % * h6 % (%* 698 : d7, h8* %€h v
F
B
IJ: 8 6#$E T
jg]^`lglwjv§?b#a`\ o bq\ bqlObqce
o?o c¬bqcHe_ jdln¸bx\ bgkH^?§%bic$^`c
m?s v]?s%mbd_ j_ bio \
W &%
22
PRATICA (Lez. 2)
daytimesrv.c
if( (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr))) < 0)
err_sys("bind error");
if( listen(listenfd, BACKLOG) < 0 )
err_sys("listen error");
œI— { K JLTQ}TE TE† H‡
Autunno 2003
if( (sp = getservbyname("daytime", "tcp")) == NULL ) {
fprintf(stderr, "getservbyname error for daytime, tcp.");
exit(-1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family
= AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port
= sp->s_port;
is%ctmj_ jlwb°e
_r^vhjq\ ^`_ b _o ]°s%\ e je ^
™ MO€ ˜ K IJQI { IQ€FHM { K TM$TQK RHR K } { TM$K M$GO}FH€WT {
RHEK { FHEM$FL€FM { K TM$TLK RH}FH€WT { €FHM$M$T}}FLIRHP$IE { K €FR IET
€R K TM {
‘
W
#include
"basic.h"
#include
<time.h>
int main(int argc, char **argv) {
pid_t
pid;
int
listenfd, connfd;
struct sockaddr_in
servaddr;
char
buff[MAXLINE];
time_t
ticks;
struct servent
*sp;
if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err_sys("socket error");
TQM$FHMO€T3M$TL}FM$FL}K…$R FH€€I
‘
6I=: G
œŸI— { K JQTL}TE TEŸ† ’‡
21
bd_lOb#ee bnj#§·s%c¬]bq_uhb_§`o?m?_rbdcH§%bd_ bi\ j·m?_o l‹j
Y
*Ÿ
PRATICA (Lez. 2)
kH^`c%cHb#]]°o ^`cHbnk^`lnm?\ b#ej#ejg§%jd\ \ jgkH^?§?j
‘
connect dal client
Autunno 2003
~ ) (Z €  rq Hi lu‚
Hj bƒ(u„ …
q tZuZ (†! ‹ " Št„u„Lˆ([ q ) bŠt„L„uˆ ‹ ‡Œn
Ž
 ) Z
=‘J’ “
3[ rq Z—Z
Y
7Y S KT b S TM RX N O NP LO N U T R U
Z
L
PS T R M S NT [ bO Q M[ PX N O P O P O P O T V K V Y]
7Y U RK S Tb R M X N OL N U T R U
Z
V S T ^9R PN ML N [UTO QR [N P L O P O T V ^ 0Y]
accept
C
jk%¤\ ^?av]?mb#k?o ¥o kHj `sHjdcHebgkH^`c%cHb#]]?o ^`c?oj#kHkHbe e jq_ubgb
lwb#ee b_ bio c¬j#ee b]Hjimb_b]]bq_ubg]b_ hŸo eb
J
20
PRATICA (Lez. 2)
¦Lo cHk%\ sH§?bi¨`] ] ]^?k%¤b#e©~ª%«
o cHeL\ o ]ebdc%ˆo cHe
Autunno 2003
PRATICA (Lez. 2)
/* backlog = 5 */
œI— { K JLTQ€R K TM { † ’H‡
Autunno 2003
23
Autunno 2003
24
PRATICA (Lez. 2)
for ( ; ; ) {
if( (connfd = accept(listenfd, (struct sockaddr *) NULL, NULL)) < 0)
err_sys("accept error");
ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
write(connfd, buff, strlen(buff));
close(connfd);
}
B
('
Hbd_ hbq_Vo ebd_rj#eo h^
TE TLK€R K TM {ƒ M$FLIR R I FHR { I
‘
ƒ IM$$F ƒ MO€R K TM { ¹3€FHM$M$T}}FwK RH}TG ƒ TM { TL€R K TM { $T #T
‘
I}P$T {~{ IET
€€T {~{ I…$K R TLP$TEŸ}TE TEŸ}TJQP$R K €K€FHJLT3K RH$I— { K JLT
‘
W
W
XW
#include
"basic.h"
daytimecli.c
int main(int argc, char **argv) {
int
sockfd, n;
char
recvline[MAXLINE + 1];
struct sockaddr_in
servaddr;
struct in_addr
**pptr;
struct hostent
*hp;
struct servent
*sp;
}
Y
PRATICA (Lez. 2)
W
if (argc != 3)
err_quit("usage: daytimecli <hostname> <service>");
if ( (hp = gethostbyname(argv[1])) == NULL)
err_quit("hostname error for %s: %s", argv[1], hstrerror(h_errno));
if ( (sp = getservbyname(argv[2], "tcp")) == NULL)
err_quit("getservbyname error for %s", argv[2]);
pptr = (struct in_addr **) hp->h_addr_list;
&%
œŸI— { K JQTL€R K TM { † H‡
B
W
W
TE TE•EK €FHE}K K
Autunno 2003
25
PRATICA (Lez. 2)
Autunno 2003
26
PRATICA (Lez. 2)
Y[Œc¬]bd_ hb_V_o k^`_r]°o h^·s$]jis•cHjgkH^`m?o jg§`o]bg]e b]]^
for ( ; *pptr != NULL; pptr++) {
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
err_sys("socket error");
mbd_]b_ hŸo _rbxs%cHji_o k%ª%o b#]e j
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = sp->s_port;
memcpy(&servaddr.sin_addr, *pptr, sizeof(struct in_addr));
pid_t
int
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) == 0)
break;
/* success */
err_ret("connect error");
close(sockfd);
/* riempi la struttura sockaddr_in (es. numero di porta) */
pid;
listenfd, connfd;
listenfd = socket(….);
bind(listenfd,….)
listen(listenfd, LISTENQ)
}
if (*pptr == NULL)
err_quit("unable to connect");
for ( ; ; ) {
connfd = accept(listenfd,…);
if ( (pid = fork()) == 0) {
close(listenfd);
/* figlio chiude il socket di ascolto */
DOIT(connfd);
/* serve la richiesta */
close(connfd);
/* chiude il socket */
exit(0);
/* il figlio termina */
}
close(connfd);
/* il padre chiude il socket della connessione */
}
while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
recvline[n] = 0;
/* null terminate */
fputs(recvline, stdout);
}
exit(0);
}
B
W
W
TE #TEEK €FHE}K K
B
Autunno 2003
27
PRATICA (Lez. 2)
Client
Server
W
Autunno 2003
28
PRATICA (Lez. 2)
Client
listensd
connect()
W
TE TE•EK €FHE}K K
Server
listensd
Richiesta di connessione
Connessione stabilita
connect()
connsd
W
‘
™ RH}TE TE€ ˜ K IJLIQI€€TP { † ‡
‘
K TM$TL€ETI { F ƒ MOM ƒ F Fw}FH€WT { $
 T}€EK P { FHEM$TRH}TE TE•P$TE
ƒ R IL€FHM$M$T}}K FHM$TQ€FHM
T } { FLP$IE { K €FR IE~TL€R K TM {
tW
Server
W
listensd
connsd
Client
W
Server
‘
™RH}TE TE•€ ˜ K IJLI`XFEW† ‡
‘
D$I$ET3T`XK G$R K FwM$TR}TE TE€FHMH$K K $FM$FLK RH}FH€WT {
listensd
connect()
Connessione stabilita
B
connsd
W
W
TE #TEEK €FHE}K K
Client
29
PRATICA (Lez. 2)
Server
connect()
connsd
‘
‘
" l @ +'6e6j%', +8 %>/(@7h(;%)6jh/(/7%*=*7:h/(:
" l @ &%* ,e: 8 % )+6jh//7%*7*=:h/7% 6=h/i: ) 6 ) : %/7,
™ RP$IHETL€ ˜ K ƒ  TLK RH}FH€WT { $TR R IL€FHM$M$T}}K FM$T
‘
tW
Autunno 2003
"" ™ )# - :- /(: /(Z Z : 8: 8:f=:f=f=f7h h) hZ(6=% + ) ) )-%+ ) ,=+8 h*7* h-)+-69, :h+', Z(h3% + ) )+) * 6jh-h6/(q %/7,%*e*=:h/=%)+*=* h69:+, + + ) * h-6q%-,
B W p/6 :%/=,/6. l0%1/=h/6rl(: +.<+12: /(Z /=h/3* +10@7+ %Ad7h8 , +12A* , +-, +1@(* +', +
"}
"}
p/6 )) : %/=,/=h/3* + ) - : /Z : 8 : fef7h c l @(* +', h1* % 69:-* h) /=h1d(: 33: /=, %8 ] +'6e69: %
"}
"" p57y ,h/7%+)d7*7h1*6+l(@g6j: +/7h+ .<* +A%+-=', +d7%'%6 +>8 *=)2(:-- : :(/d(/(Z Z3@ A: 86e:f=h8 fe: /i*h + d7) c:h8l % 8 , j ++'5 +7& 469) ”:8): +(/8;Z*=:9* :%-8 6t:f7& +7fg/0<:+AZ(n % @()/7)+)+>6ed7hh/g8 ,/0+ %%*7*=] ]=: h: . /7% %8 +
+7>/(& ! !@?!& .1
°K { FHEM$IM$F
‘
listensd
™ R XK G$R K Fw€ ˜ K ƒ $TLK R}FH€WT { P$TEŸR I€€T {u{ ISK FHM$TQ$KHM ƒ F T
€FM$M$T}}K FHM$K
W
30
PRATICA (Lez. 2)
padre
figlio
figlio
!31'( ! (
 r!( u!1 !  r!1 " #$% & u !1 ' ! ! !)(H* "3H ! u !1 &
( !+,
Server
connsd
W
¢T { }FH€WM$IJQTLT3G$T { P$TTEM$IJLT
Autunno 2003
listensd
Connessione stabilita
padre
TE TQP$TE€ ˜
W
ž$€ ˜ Fw}TE TEŸ† ’H‡
W &%
ž$€ ˜ Fw}TE TE† H‡
Autunno 2003
31
Autunno 2003
32
PRATICA (Lez. 2)
#include
#include
"basic.h"
"echo.h"
PRATICA (Lez. 2)
for ( ; ; ) {
cliaddr_len = sizeof(cliaddr);
if( (connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &cliaddr_len)) < 0)
err_sys("accept error");
echosrv.c
int main(int argc, char **argv) {
pid_t
childpid;
int
listenfd, connfd;
struct sockaddr_in
servaddr, cliaddr;
socklen_t
cliaddr_len;
if( (childpid = fork()) == 0 ) {
close(listenfd);
str_echo(connfd);
exit(0);
}
close(connfd);
if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err_sys("socket error");
}
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family
= AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port
= htons(PORT); /* daytime server */
void str_echo(int sockfd) {
ssize_t
n;
char
line[MAXLINE];
for ( ; ; ) {
if ( (n = read(sockfd, line, MAXLINE)) == 0)
return;
/* connection closed by other end */
write(sockfd, line, n);
}
}
if( (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr))) < 0)
err_sys("bind error");
if( listen(listenfd, LISTENQ) < 0 )
err_sys("listen error");
ž$€ ˜ FL€R K TM { †’H‡
&%
ž$€ ˜ Fw€R K TM { † H‡
Autunno 2003
33
Autunno 2003
34
PRATICA (Lez. 2)
#include
"basic.h"
#include
"echo.h"
int main(int argc, char **argv) {
int
sockfd, n;
struct sockaddr_in
servaddr;
echocli.c
PRATICA (Lez. 2)
void str_cli(FILE *fp, int sockfd) {
char sendline[MAXLINE], recvline[MAXLINE];
while (fgets(sendline, MAXLINE, fp) != NULL) {
reti_writen(sockfd, sendline, strlen(sendline));
if (reti_readline(sockfd, recvline, MAXLINE) == 0)
err_quit("str_cli: server terminated prematurely");
fputs(recvline, stdout);
}
}
if (argc != 2)
err_quit("usage: echotcpcli <IPaddress>");
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err_sys("socket error");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
/* echo server */
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
err_quit("inet_pton error for %s", argv[1]);
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
err_sys("connect error");
str_cli(stdin, sockfd); /* svolge tutto il lavoro del client */
exit(0);
}
W
ž$€ ˜ Fw}TE TE
" l %8* %. d ) :69:, ˜] +'6j69:+. h & : 8 +8 %>* %8 ;%8(%6 ) :%/=, *=@ ) ) +>* , %*e* +1.+'6e6l(: /7+
W
ž$€ ˜ FL}TE TE
Autunno 2003
35
Autunno 2003
36
PRATICA (Lez. 2)
PRATICA (Lez. 2)
prompt > echoserver &
[1] 21130
prompt > echoclient 127.0.0.1
Ciao server
Ciao server
Arrivederci
Arrivederci
^D
prompt >
prompt > netstat –a
Proto Recv-Q Send-Q Local address
Tcp
0
0 *.9877
Foreign address
*.*
(state)
LISTEN
" c /3@(/ -+ ),=8 +U]=: /0%* ,=8 +
‘
‘
Foreign address
localhost.1052
localhost.9877
*.*
" _ 0@7%* , hAd@(/=, h )+6jh/(/7%*=*=:h/=% 2A* , +2: ) : , +
(state)
ESTABLISHED
ESTABLISHED
LISTEN
Risposta del server
Risposta del server
Digitata al terminale
prompt > netstat –a | grep 9877
Tcp
0
0 localhost.1052
Tcp
0
0 *.9877
prompt > echoclient 127.0.0.1
prompt > netstat –a
Proto Recv-Q Send-Q Local address
Tcp
0
0 localhost.9877
Tcp
0
0 localhost.1052
Tcp
0
0 *.9877
Digitata al terminale
Digitata al terminale
localhost.9877
*.*
TIME_WAIT
LISTEN
" 6ec %)t)8-+ , ,h1h3d76%l08 % :ht6Z(hml(: @(j Z(L% w5()+#6jn hd7/(%/78 c%c*=w *=:he /={F%>_8 : .<c c +/=%>: /$0 @0%* , h * , +, hAd7%8 @(/
! !(
!& !@6 ,˜01 H ! !
W
™ RH}TE TE ˜ IL€ ˜ K ƒ } FwK RH}FH€WT {
™ RH€R K TM { ¹L$
M TR R Fw} { I { FL$K
- ! (0( ( !H(5+ H H !!
!& !& 0((!H !
W
ž$€ ˜ Fw}TE TE
"" yc ) :&q%:, +8 /7/(% Z) h6lg: @(y Z([ % : ) ,=6 @7) ,:%e:-/0,:(]e, :%)%U8 .Z(%: /7* +698 6r: d7l(, :h+8.<[+0 /(@(Z(: /(h3Z %9: =+:,/76rl7%1:(* h(6q %,Z(%* 698 : d=, h8
"
" <# +@6: (/ 1l(Z: @(: *7: )(@(*8h-+1 6! :q!. %,3dZ) :H6=% + ) 6 65 9+X) :,š%/=, ;5 : %+01/7% 6l(: @(* h
"" _ /7# +1% 0) )d(h@78%: *.<* , , +-hA+>, hkd7d+@(a 8/=c, ,%UdLh Zg{#: %) _* %+ c c8 ;6r %l(8: @21*=@(/78 %+U) )Zh :* @(, +-/0+, h 6ehk #/h/=%5 *ee1*7 :h{F/7_% c cc k9.<l %2/=,=6e8h%A/=6: ) 6@()* :%+ /7,2
))
)
"
" 0; <c :]e@(%@0::+)/7U%(/ %AZ(/ ZgZ: *7%hA6 d7*l(%t69: ):Z+8 * ::.<,%d7h , +8 h;+ %%8) =;8 6 %:8,) ::%/06e&g%'/=;-h, %>/7h3: ) 6 a lc :d @(*721: [/=0 % @() ):+A(/ Z 8 %': ++ Z/=r6 ) : l=/=%1%r6: ) l7* %Ah-6 8 q:, %h,8 /7% +Z e @(hC—/ a a % c d
" _;+10/7@7% %) *)hA, hA* ,d+@(, h/=, hc c )+w e16jh{F/7%_ *7c*=c :h./=% %2/=,=6e8 %Ah.: ) d * %)%-8, ;+%.<C8 %l0/7+, %6 l(, %: @(8 *. h : /=)+ +-,6=+h%/(Z/=%: *=) *76 : h) :%/0/0% ,
" 6 y l(h: d7(@ h1Z(% @g/)+ 6e6=%h8 /(, h /=%d7*=%*78:h :Lh /0Z(% mh j w+ = : . @. 57%-&.<%/=, #:] %,=: . J% n : ) 6 ) :%/7,
B
Autunno 2003
37
PRATICA (Lez. 2)
Y
‘
$TX ƒ M$€ {
k%ªHbge bq_lgo cHjx\ j#]k%o jxs%cH^
K M$$K €I ƒ M$F`S$FJQ…$K T
robdep@zaffiro:~/Corsi/Reti/C> echocli 127.0.0.1
ciao
ciao
^D
robdep@zaffiro:~/Corsi/Reti/C> echocli 127.0.0.1
pippo
pippo
^D
robdep@zaffiro:~/Corsi/Reti/C> ps
PID TTY
TIME
CMD
1077 pts/0
00:00:00 cat
22084 pts/2
00:00:00 bash
27162 pts/3
00:00:00 ssh
30007 pts/6
00:00:00 bash
30331 pts/11
00:00:00 bash
30761 pts/11
00:00:00 echosrv
30765 pts/11
00:00:00 echosrv <defunct>
30767 pts/11
00:00:00 echosrv <defunct>
30768 pts/6
00:00:00 ps
e bd_lgo cHj
W
€FHM { EFR R IETLK R #IR FHETL$K ƒ }€K { IL$TRP$EFH€T}}FLT
$TR R TLEK }FHE}T3$TRXK G$R K Fw† JQTJLFEK I¯°N6D$¡?¯°T { €“ ‡
PQhho jql‹bqcHebxcH^`c¬h^?a`\ o jqlw^„\ j#]k?o jq_ b
Y
PQkHk^`__ bg]k?_o hbd_ bis%c¬]?o a`cHjd\?ªHjdcH§
Y
¢wjdo e
B
39
ƒ{ K R K SS$F
^`lv¸?o b
\ bq_k%ªHbgk%ª%o jqlwj
K G$M$IR ˜ IM$$R TE
Autunno 2003
40
PRATICA (Lez. 2)
^`lg¸?o b
void sig_child(int signo) {
pid_t pid;
int stat;
while ( pid = waitpid(-1,&stat,WNOHANG)) > 0) {
printf(“Child %d terminated\n”,pid);
}
Il client viene ucciso
}
YiŒ%eo \ o jdcH§?^·o \a?b]e ^ _ bg§`o]b°a`cHjd\ o]?obho e jqcH^„o
m?_ ^?kHb]]°o ^`lg¸?o b
Il client viene ucciso
‘
FW
P$P$TM$IQK RXK G$R K F`XK M$K }€T K TM$TQ€ ˜ K IJLI { I´wIK { P$K 
Prompt > echoserver &
[2] 19287
prompt > echoclient 127.0.0.1
Ciao server
Ciao server
^D
Child 19293 terminated
accept error: interrupted system call
2003
€IR Autunno
R
¡•M$IQP$FH}}K …$K R TQ}FHR ƒ S$K FHM$T
41
Autunno 2003
42
PRATICA (Lez. 2)
PRATICA (Lez. 2)
for ( ; ; ) {
clilen = sizeof(cliaddr);
if ( (connfd = accept(listenfd, &cliaddr, &clilen)) < 0) {
if (errno = EINTR)
continue;
else {
perror(“accept error”);
exit(1);
}
}
}
Y[Z\a?b#]e^`_ubg§%bd\]b#a`cHjd\ bvho bdcHbgb#]Hb#a`s•o e ^
1–=•U™
QT
Y
^`o k%ª
g]e j#e jio cHebd__r^?eejx\ jg¥
s%c ?o ^`cHbnjkHkHbqme
_o e^`_cHjgkH^`cto \kH^?§`o kHbg§`obd__ ^`_rbiž$™
Y
^`o k%ª x\ jga?b#]eo ^`cHbg§`oe jq\ bnbd__ ^ _rbxcH^`c
o
\ ]bd_ hbd_Ÿe b_lno cHji\ b]b#k?s #o ^`c$b
‘
PRATICA (Lez. 2)
E 9T
Y
C
•Zr‰Vf
jd\?mj°§`_rbgbio \¥o a`\ o ^v§`o hbqc$ej ^`lg¸?o b
FHJL…$K TL}FM$FL$TKP$EFH€T}}K { TEJLK M$I { KHPHTEK ƒ IR K
WTM$G$FHM$FwJQIM { TM ƒ{ K$TK$I { KHM$TRH}K } { TJLIQFHP$TEI { K WF
‘ FHJL…$K TL}FM$FLM$T€T}}IEKP$TE•P$TE~JLT {u{ TETQIRHP$I$ET3$K
‘
Y[Z\]b#a`cHjq\ b g]eje ^|kHj#e es%_uje ^|§%jq\?mj#§ _ bv§`s%_ jdc$e b
\ b#]bk%s °o ^`cHbg§`oj#kHkHbqmHe
38
PRATICA (Lez. 2)
mbd_Ÿa?b#]eo _ubi\ jnk^`c•cHb]]?o ^`cHb
Autunno 2003
™ M { TE~E ƒ SHK FHM$T3$TR R TL}~—} { TJ
F
F
Autunno 2003
Y[Z\]°o ]e bdlwjg^`mb_ j°eo h^„lOjdcH§?jis•c›]b#a`cHjd\ bg§`o
PQa`c%\ ok?\ o bqcHe
C›
™¢N Ÿ–œ
`s$jqcH§?^„\ jnk^`c•cHb]]?o ^`cHbghŸo bdcHbgk%ª%o s$]jio \¥o a`\ o ^
Y
FB
B
Y[Zcts%c¬]bd_ hb_V_o kH^`_ ]?o h6^ do \]bq_ h6bq_Ÿk%_ b#jis%c¬¥
o a`\ o ^
S$FHJQ…$K T
TG$M$IR T
E P3kHkH^`__ bnebdcHb_Vm?_rb#]bdcHeb
Tim?_ b#ho ]ej
`s$b]He ^·m?_r^`¸?\ blwj
™ MOIR € ƒ M$KH}K } { TJLKHR TQ}~—} { TJ €IR RH}FHM$F
I ƒ{ FJLI { K €IJLTM { TQEK € ˜ K IJLI { TQK MwIR { EKM$F
B E
Y
$bi\ jgk%ª%o jqlOjejgj#§tjkHkHbdmHeL_o e ^`_c$j
I€€TP { K TM$TQEK € ˜ K IJLI { I
‘
Y
‘
W
$bi\ bd__ ^`_rbQTg§`o h6bd_ ]^|§?j
! )G
! )G
•ZŠ
'
ZŠ
KHG$T} { K }€TQR TEEFHET3† M$TR R T}TJQP$K Fw}KH€ ˜ K IJLIQT K { ‡
™°T}T {
€FHM$M$T}}K FHM$TLTQI€€TP {
•ŸTEJQK M$ISHK FHM$T3$TRH}TE WTE
Autunno 2003
43
FT
W
N#FH}IL} ƒ €€T$T3}TQK RH}TE TE { TE~JLK M$IQPHE~TJLI {ƒ EIJLTM { T
‘
_ b#]b°e §?bq\ \ jgkH^`c%cHb#]]°o ^`cHbim?_o lwjg§%bd\ \ jgk%ª%o jdlwj#ej
j#§vj#kHkbme
B B• B›
W
–IL€FHM$M$T}}K FHM$TQ$K TM { L
I ž
™ R€R K TM { }P$T$K }€T ƒ M
™ R}TE TE•€ ˜ K IJQILI€€TP {
‘
x™ •
W
‘
–™
Prompt > echoclient 127.0.0.1
Ciao
Ciao
Il server viene ucciso
Arrivederci
Il server non risponde (dipende dal codice)
žHœ
}
" p/—a c d ; :%/=%>*=d7%Z :, h3+ W ) 6 *) W:%/=, W
" 3d7%8 .<%*=* hAd0%8 6rl 1: ) 6 ) :%/=,/=h/—l7+ 6l(: @(* h13: J) * h6q %-,
" %d 8hh/38 %*7:7%+*7, d7%%8 ,. ˜+ : /7Z+ :8 :6j%';%8 % e hba 0 @(: (/ Z :* , +. d7+>: ) .<%*7* +'&(& :hZ :
RHWK R RKH}FH€WT { $T}€EK P { FHE TM$G$FHM$Fw€ ˜ K ƒ }K
‘
YpµqkHkbdmeL_o e ^`_cHjis•ckH^?§`o kbn§`obq__ ^`_ b
—–˜–
ž$Nš
‘
™•Ÿž$œ
6š
™ RH€R K TM { }P$T$K }€T
‘
JŸ
Y[Z\]bd_ h6bd_Vm?s ·_o k%ª%o jdlwj_ bvjkHkHbqmeOmbd_\ jxm?_ ^?]]?o lwj
B
W
" @7%11*, h1 d((!@6j+d(:,+8%A* %>!%: 01) 6ehLZ:6j%œ l=+˜$Z : @7%˜\i8 : , %6eh/g* %'69@=,=:;%
" :c& ) /7d(h8 h-8 +-6j,%h *7* h3, %8 . : /7+>* %1: ) * %'& /7+ ) %>B /=h/; : %/7% 6=+-, e@(8 +, h3h
B
B
B
W
c &/7h6L8 +8 k% j5 (c zu*c !z*d !n (: ) !* %' &/= + ) ˜%Z :(5 c z l c l e 1( H & % k h/0(,e 8 h !+ 8 % %8 h8 U% Z : l l *=@ % \i8 : , %)%A/7% 6e+* —h Z : %8 h8 %
, %8 . : /0+) ) 8 €% j/7) -h/i* 6g8 :;%8 e%1d(c : 3ue n ) ) )
™ ¢•DH™ D$ž
45
PRATICA (Lez. 2)
‘
K TM$TLG$TM$TEI { F
‘
™¢•DH™ DHž
C
Y
W
TE #TEŸ} ˜$ƒ{ $F´wMLIM$wET…$FHF {
Y
(
47
Y
Y[Z\k%\ o bdcHe`]?mb§`o ]kHbic•sH^?ho§?j#e
ojd\]bq_uhbq_Ÿ§?^`m^„o \
_ bd¸^?^%e
™ R}TE TE•M$FHM ˜ IQP$K OK RH}F€WT { IP$TE { F
‘
ŸN#DwEK }P$FMH$TLIK$I { KH€FHM ƒ M
‘
W
•
Ypf
‘
#T
Y
B
?™ •
—–š–˜™ B L• 3 G
\ o bqcHe io ct_ b#j°§`\ o cHb `sHjdcH§?^„_o kHb°hb
°TI$R K M$TLEK { FEM$I3ž$Nš
#ž ž
™
*›
Autunno 2003
J H–
W
#T
W
E kR
B • J–˜• ™ C›
¶¥
o cH^|j#§
jg¥s•c #o ^`cHbg§`o?\ b#ees%_ v
j §?jq\]^?k%¤b°eL_o e^`_cHjis%c¬b__ ^`_ub
ž q™UVžHšd¡
ž Ÿš Ÿ¡
#ž N Ÿ¯°ž •ž Ÿ¡
°ž N
‘
‘
Y[Z\]bd_ h6bd_h6jga`o bn¥jxo \?_ bd¸^?^?e`]bc jgk%ªHbxo \k%\ o bqcHe
]bicHbgj#kHk^`_ra?j
•FHMO€ ¹L€FHJ ƒ M$K €IS$K FHM$T3 ƒ E~IM { TLR FL} ˜Hƒ{ $F´LMO† }TE TE
‘
}€FHR R TG$I { Fw$IR R ILET { T3IR { EK JLTM { KH}PHT$K }€T ™ •‡
–
Ÿf v_o e_ j#]°l‹bee b_ i
o§%j#eo?mHbd__o kHb°hb_ bi\ µqf
s•ckHbd_ e ^veo lwb^`s$e
PRATICA (Lez. 2)
jgkH^`c%c$b]]°o ^`cHbgho bqc$bg]e jd¸?o \ o e j
J H– ™ B •
W
*V
G AF
C L• Y
H“
B
46
PRATICA (Lez. 2)
9T
W
Y[Z\k%\ o bdcHe i¸?\ ^?kHkHje ^„o ct_ b#j§`\ o cHb
ƒ IM$FwM$FMO}KHHT TETIG$K ET
FHR ƒ SHK FHM$TQ}TJQP$R K €T¯
IR R TEEFET
’H“
‘
W
W
T
™¢•D$™D$žO¹LK G$M$FEI { FQR FP$TEIS$K FM$TQHK´wEK { TLGHTM$TEI
R TEEFHET3$KHž$D$™D$ž
‘
ž$š
Y[Œc d
j \ e_rjxm^?]]?o ¸?o \ bgkHjdsH]jn§`obq__r^`_rb g]bi\ jilOj#kHk?ª•o cHj
]bd_ h6d
b _VcH^`ct_o ]?m^`cH§?bim%_r^`m?_o ^
ƒ TRH€I}F
œŸK TE}FL$I ƒ €€K $TETQK RP$EFH€T}}FL}TE TEŸ† K M
‘
TM$G$FHM$Fw}P$T$K { K ™ %¯
q‡
D ƒ
$K P$TM$$TETQ$IR R I3ET { T
‘
šŽ$IR R IQJQI€€ ˜ K M$IL}TE TE
‘
ƒ Mw}TG$M$IR TQ$K
EEK T$TE€K dIR}TE TE
2003
UVI€€ ˜ K M$IQ}TE TEM$FHMwEIGHG$K ƒ M$G$K …$Autunno
KRT
E
Autunno 2003
ETI$R K M$TLT
™ RH€R K TM { € ˜ K IJLI3ETI$R K M$TQ€ ˜ TQEK { FHEM$I
‘
kH^`c%cHb#]]°o ^`cHb
N#F}IL} ƒ €€T$TQ}TLK RH€R K TM { K G$M$FEI3R TEEFETQ} ƒ
}€EK TLM$TRH}F€WT {
44
PRATICA (Lez. 2)
Y[Œcjd\ e_ ^|bd__ ^`_ubgeo m?o kH^|§?jga%b#]eo _ bnkH^`c¬jkHkHbdme io \
‘
Autunno 2003
PRATICA (Lez. 2)
*– t• š–š™ ›
B
W
TE TE}FJQJQI
Autunno 2003
48
PRATICA (Lez. 2)
$^`\ ^„\ jg¥
s%c °o ^`cHjgk?ªHbga?b#]e
o ]kHbio \k?\ o bdcHe
void server_somma(int sockfd) {
int
i, arg1, arg2;
ssize_t
n;
char
sendline[MAXLINE], rcvline[MAXLINE];
char c;
for ( ; ; ) {
if ( (n = reti_readline(sockfd, rcvline, MAXLINE)) == 0)
return;
/* connection closed by other end */
/* legge dalla stringa passata dal client i due interi
da sommare */
if( sscanf(rcvline, "%d %d", &arg1, &arg2) == 2 )
/* converte il risultato in stringa e lo scrive nel
buffer */
sprintf(sendline, "%d\n", arg1 + arg2);
else
sprintf(sendline, "input error\n");
n = strlen(sendline);
reti_writen(sockfd, sendline, n);
}
}
sommasrv.c
N#R K TM { }FHJQJLI
D$EFH…$R TJQI
Autunno 2003
49
PRATICA (Lez. 2)
9Tis•c|m3Ÿ„m?o Y
YgfA\ o bdcHe`bn]bd_ h6bd_ #]e b]]^veo m^|§`o?lOj#kHk%ª%o cHj
sunos5 > sommacli 206.62.226.33
11 22
33
-11 -44
-55
Ÿb#hbna?b#]eo _rbg§`sHbxo c%m?sHe
™q$I { KK MOIEEK Fw$IRH}FH€WT {
™q$I { K$K G$K { I { K$IR R ƒ{ TM { TLIR R I { I} { K TEI
‘
W
‘
B B
YgfA\ o bdcHe`bn]bd_ h6bd_ dlOjkHk%ª%o c$bg§`oeo mH^|§`o hbd_ ]^
P$IE€R IR { EIL™ M { TR
‘ ¡%M$I
›s$b]e ^·m?_ ^`¸?\ bql‹jgh6bd__ n
j#¥¥
_ ^`cHej°e ^·o c¬]b#a`s%o e ^
™ š|J ƒ R { K P$R T K M$G
‘
TR T€ {
‘
Y
B
QTg§`o ]?m^`c%o ¸?o \ bg]?s%\ \ jimj#a`o cHj
Y[Z\kH^?§`o kb
‘
}FHJLJQI€R K “ €
50
PRATICA (Lez. 2)
B
Y[Z\kH^?§`o kbn§?bq\k%\ o bqc$e`]^`lglwj
kH^`lgm?\ b#]]^
Autunno 2003
bsdi > sommacli 206.62.226.33
11 22
33
-11 -44
-16542537
bq¸
Y
?
N
&B
3
N
%mjq_rk V¸?o a rbdcH§`o jdc ZcHe bq\ \ o e e\ b rbcH§`o jqc
G A•F
1FB
Y
¡•œD
Autunno 2003
52
PRATICA (Lez. 3)
Ÿf
qEI}P$FE { FwFEK TM { I { FwIR R IL€FM$M$T}}K FHM$T¯°IXrXK $I…$K R T
‘
Y[Œ
‘
!
#"$%& '
(
Ypf
TMSHIL€FHM$M$T}}K FHM$T¯#K M$IXrXK $I…$K R T
Y
3T
1F
$]bdlgm?o
œ
‘
PRATICA - Lezione 3
o]^`cH^n]?o esHj #o ^`cHbio ck%s?o g]bdcH]j#e ^·sH]jd_rbiŒ
! U– B
–)J B
B–
‘
UD
‘
)*+ ,-./+ 0132-4$5 +6/ 072+62+895 -.*:
;97.-93-3<+ =5 :>?93+ *A@*B:.>?90+ C9
J
ƒ MSHK FHM$KHP$TEŸK}FH€WT {
Autunno 2003
53
PRATICA (Lez. 3)
•qK P$K €ILK M { TEIS$K FHM$T3P$TE•K RHP$EF { FH€FR R Fw¡%œŸD
socket()
K a L P M O NP O P[ bQ R
recvfrom()
YP O U Z [ U S \ U N L O P ]
YP O U Z [ U L M T L O P ]
K a L P M O NP O P[ Qb R
sendto()
SERVER
CLIENT
sendto()
recvfrom()
W 7 W 0 J^ "!
" * h 6q %,Z(%* 69 8 : d7, h8 [ :(Z(+',j:Z(+1* 6g8 :;%8 %h : 2(@1] ] %8: /69@(: %'&(&(%8 %
"} % jc$;) %t)+ # Z )8 @(%| /7.<&=hl7%'+ f=f=6+Ul7%AZ(%* %:(Z(8 ;-+'%),e:'6j2(h@(/ ] ] %%8 ]e@/7fg:h/(:Z : ) h?+-;+/7fe+', h*n )
" = ;5(%d7%'8%69+:]e/(:6e/0
!+h /7 !*h 698 )+>:, =*:(,=Z(8 +@= ,) =)H+@( ]=8 +)@(/76f( l=:h%U)/7Z(% %* 6g 8 : ;%1: ) * h!c 6&q % ,1![ :)'& ˆ(  %X
‹ ‡
Œ
W
IR FET3$KHEK { FHEM$F
close()
¯#P$TE•FHEI
‘
‘
·F
" 5(: . : ) :0+'& ) :@ ),e!: . &:01Z @7 %1d=+!&8 +.< % ,= 8 :Z :7+'6e6e%d=,
54
PRATICA (Lez. 3)
’w}TLTEEFET¯#…— { TLR T u{ { H
K Fw}€EK {u{ KH}T3š
T
‘
close()
W
2003
P$T$K ETQTQEK €T TET3$I { IG$EIJLJQAutunno
K
`K M$€R ƒ $
 T
}u—#} ­ }F€WT { “ ˜
K M { ET€ X EFHJL†K M {
FHK 
¯°K M {
¯°K M {
¯°} { E ƒ € {
}FH€WI$HE
A¯°}FH€WR TM { HR TM$‡
K M { }TM$ { FH†K M {
€FHM$} { FHK 
¯°K M {
¯#K M {
¯
€FHM$} { } { E ƒ € { }FH
€ WIH$E
¯#}F€WR TM { R TM$‡
bind()
socket()
B
B
W
TE TEŸT€ ˜ FL€FHMO¡?œD
N#R K TM { T€ ˜ Fw€FHMO¡?œDw† ’H‡
Autunno 2003
55
Autunno 2003
56
PRATICA (Lez. 3)
int main(int argc, char **argv) {
int
sockfd;
struct sockaddr_in
servaddr, cliaddr;
if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
err_sys("socket error");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family
= AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port
= htons(PORT);
echoudpsrv.c
PRATICA (Lez. 3)
#include
#include
"basic.h"
"echo.h"
echoudpcli.c
int main(int argc, char **argv) {
int
sockfd;
struct sockaddr_in
if( bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 )
err_sys("bind error");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
server_echo_udp(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr));
}
void server_echo_udp(int sockfd, struct sockaddr *p_cliaddr, socklen_t clilen) {
int
n;
socklen_t
len;
char
mesg[MAXLINE];
for ( ; ; ) {
len = clilen;
if( (n = recvfrom(sockfd, mesg, MAXLINE, 0, p_cliaddr, &len)) < 0)
err_sys("recvfrom error");
if( sendto(sockfd, mesg, n, 0, p_cliaddr, len) != n )
err_sys("sendto error");
}
}
&%
NR K TM { T€ ˜ Fw€FMO¡?œŸDw† ‡
servaddr;
if (argc != 2)
err_quit("usage: udpclient <IPaddress>");
if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
err_sys("socket error");
client_echo_udp(stdin, sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
exit(0);
}
NFHM { EFHR R Fw} ƒ RJLK {u{ TM { T
Autunno 2003
57
Autunno 2003
58
PRATICA (Lez. 3)
PRATICA (Lez. 3)
Y[Z\k%\ o bdcHe k^`c$e
_r^`\ \ jnk°ªHbxo \§?j#ej#a _ jl
void client_echo_udp(FILE *fp, int sockfd, const struct sockaddr *p_servaddr,
socklen_t servlen) {
int
n;
char
sendline[MAXLINE], recvline[MAXLINE + 1];
char
buff[MAXLINE];
socklen_t
len;
struct sockaddr *p_replyaddr;
h6bqcHa•jg§%jd\]bd_ h6bd_
Y[ZcH¥jeeo?m^?e_ b¸?¸Hbi_o kHbhbq_ubis%c
§?j#ej#a _ jl
p_replyaddr = malloc(servlen);
•qIR TL$I { IG$EIJ
`sHjd\ ]?o j#]°ojd\ e_ ^
}IET…$…$T3K M { TEPHEI { I { FQ€FHJLTQR I
EK }P$FH} { IQ$TRH}TE #TE
‘
while (fgets(sendline, MAXLINE, fp) != NULL) {
sendto(sockfd, sendline, strlen(sendline), 0, p_servaddr, servlen);
len = servlen;
§`o?_o ]°m^?]e j
W
! tW
" B l %8 .<%',(
%UZ:*=d=%Z : 8 %1@/Z+-, +-& 8 +. ; %8 * hA@(/=+1d7h8 , +kp y l
if( (n = recvfrom(sockfd, recvline, MAXLINE, 0, p_replyaddr, &len)) < 0 )
err_sys("recvfrom error");
Y
$]bd_ k%o °o ^
D$EF IET3IL€ETIET ƒ M$IL}K {ƒ IS$K FM$TQ$TRG$TM$TET
ƒ RH}K { FL€ ¹QK RH€FH$K €TL$KH}P$T$K }€T %$G$“ €
‘
if( (len != servlen) || memcmp(p_servaddr, p_replyaddr, len) != 0 ) {
struct sockaddr_in *sin = (struct sockaddr_in *) p_replyaddr;
err_msg("risposta da %s ignorata\n",
inet_ntop(AF_INET, &sin->sin_addr, buff, sizeof(buff)));
continue;
}
‘
recvline[n] = 0;
fputs(recvline, stdout);
}
}
œŸI { IGHEIJQJQKHP$TE ƒ{ K
Autunno 2003
59
PRATICA (Lez. 3)
F Autunno 2003
60
PRATICA (Lez. 3)
Y
Ypf
Y[Š^`c¬]?ok%_ b#jis%c$jgkH^`c%cHb#]]?o ^`cHbxˆªjdcH§?]?ªHjq¤b
ª%o?\ ^v]e jgj#]?mb#e ejdcH§?^„ˆu]bq_uhb_^vk%\ o bqc$e
3_o lwjdcHb
F
Y
bd_•b°ho ejq_ub
‘
™ O
M IR € ƒ M$KH€I}KM$FHMw…$I} { I
•FHMO}IPHP$K IJLFw}TQK RJLT}}IG$G$K FL$TRH€R K TM { M$FHMO¹QJQIK
IEEK #I { FwIRH}TE #TEŸFP$P ƒ ETQ}TQR IQEK }P$F} { IL$TRH}TE TE
M$FHMO¹QIEEK I { IQIRH€R K TM {
™ MOIR € ƒ M$KH€I}K† T}“ { EIM$}ISHK FHM$KH…$IM$€IEK T‡XILJQFHR { I
–
‘
‘
*Ÿ
W
$K XrXTETMS$I
Y
W
W
W
G 1F
E 'F
Ÿf
?
$bqlgm?\ o kHbdlwbqce bio \?¤b_c$bq\?lwblw^`_o ji\ o cH§`o _o ^„Z
`sHb#]e ^„m%_ ^`¸?\ bdlOjg]°o?m?s ·sH]jd_ bis%c
eo lwb#^`sHe
]°o jg]bdc jgkH^`c%cHb#]]?o ^`cHb
k%ª%o jqlOjq_ bx\ jg¥
s%c °o ^`cHbnk^`c•cHbkHe ]#sts%c¬]^°k%¤b#eLŒ
¸?\ ^?kHkHj#e^„o c¬j#e eb#]j
1F
QTim^?]]?o ¸?o \ b
Ypfq^?]jn]°sHkHkHb#§?bg]bxs%c¬§?je j°a`_rjdl
]?o?mbd_ §%b
D$TEŸT}TJQP$K F ƒ MwEF ƒ{ TE•R Fw… ƒ{u{ I #K I
‘
—W
$bq¸?¸HbdcHbiŒ
NFHM$M$T€ { T3¡%œŸD
bx\ jim^`_uejnk^`c¬k%s%o]°ohsH^`\ bnk^`l
B
B
Y
¶s%o c$§`o§?^`¸?¸?o jdlw^v§`o ]eo cHa`s%o _rbge_ j
FH€WT { ¡%œŸDL€FHM$M$T}}F
FH€WT { ¡%œŸDLM$FHMO€FHM$M$T}}F
‘
‘
s•c%o kHjd_ b
B
JŸ
FH€WT { ¡%œŸDL€FHM$M$T}}F
FNšT
Autunno 2003
D
F *Ÿ
61
PRATICA (Lez. 3)
Y[Š^`c¬]°o?m?s v]°mb#k%o ¥
o kHjd_ bio \§?b#]e
o cHj#ejd_o ^
`s$bd\ \ ^
]?mbk%o ¥
o kHj#e ^·o ckH^`c?cHb#kHe
•FHMO}K ƒ }IL}TMH { FQJLI`´QEK { TLFL}TM$
‘
™qP$I€€ ˜ T {r{ K #TEEIM$FLI ƒ{ FJLI { K €IJQTM { T3}P$T$K { K
‘
IR R K M$$K EK SS$Fw}P$T€K XK €I { FwM$TR R IQ€ ˜ K IJQI { ILIQ€FHM$M$T€ {
–
E –
W
!
Y
‘
W
Y
62
PRATICA (Lez. 3)
tW
J' &– B
" ld(h-8 h,e82 %)%2(.20%1+ 8 :, h8 /=+8 % ) -%8 h8 % e _ba d h 5up ll h o c [ .<+1/7h/$21@(/
?s vb#]]bd_ bisH]je ^„mHbd_
‘
‘
LW
W
Autunno 2003
tm^?]]°o ¸?o \ ok%ª%o jqlOjq_ bnk^`c•cHbkHewm?o ¬
§`o?s%cHjgh6^`\ e j
Y
Y[ZŸ§?j°e j#a`_ujql
\ b#e eo]^`cH^ `s$bq\ \ ok?ªHbgjd__o hjqc$^
§?jd\ \ o cH§`o _o ^|²kH^`c%c$b]]^`³
•FHMO}K ƒ }3
I ET€ XEFHJL¯JLI3}K ƒ }IQETILFwETI
‘
NK R K JQK { I ƒ MO}TE #TEŸ¡?œDOIQ€FJ ƒ M$K €IETQ€FHM ƒ MO}FHR F
‘
€R K TM {
V
€FHM$M$T€ {
€IJQ…$K IETLR K M$$K EK SSHFL€FHMO€ ƒ KH}K ƒ FHR€FJ ƒ M$K €IET
œŸK }€FHM$M$T {~{ TET3K RH}F€WT { † }P$T€K XK €IM$$F
%¡
D$ž$N
€FHJQT`XIJQK G$R K IL$KHPHEF { FH€FHR R KHM$TR€IJQPHFw}K M HXIJQK R —‡
JV
%__r^`_oj]?o c$k?_r^`c%o?m^?]]^`cH^vb]]bd_ bvk^`c$e
_r^`\ \ j#eo
¡%MO}F€WT { ¡%œŸDwM$FHMO€FM$M$T}}FwM$FMwP ƒ
€FM { E FHR R IET
TEEFEKHI}K M$€EFHM$K
¡•œDO€R K TM { ”
WTE}K FHM$T3€FHM$M$T€ {
Autunno 2003
63
PRATICA (Lez. 3)
Y[Œ
™ M$IXrXK $I…$K R K {
1F
Autunno 2003
64
PRATICA (Lez. 3)
|cH^`c¤ g
jq\ k%s%cHjga?jd_ jdc #o jg]?s%\ \ jgkH^`cH]b#a`cHjg§?bdo
§?j#ej#a _ jl
void client_echo_udp_conn(FILE *fp, int sockfd, const struct sockaddr *p_servaddr,
socklen_t servlen) {
$KH¡?œD
<
1FW W
" l* %-@& /0%+ *7)% * %68 %1l7%1: /=* ,%%. 8 dh-,) :6eh3%.<6eh%/ /=, ke%>c * o , +#’. k d7[j+ 6 -0% -@7+@(//7,e&g:%Z(+-* , , h+'&8 %U8 +Z . : * h/=h
* , +-,e:-8 :6j%'; @=,e:
Ypfq^`cH]?o §?bd_o jqlw^„\ jn]b#a`s$bdcHe bnjdm%m?\ o kHj °o ^`cHbgk%\ o bdcHe
int n;
char sendline[MAXLINE], recvline[MAXLINE + 1];
]bd_ h6bd_VŒ
if( connect(sockfd, (struct sockaddr *) p_servaddr, servlen) < 0 )
err_sys("connect error");
™ RH}TE TEEK €T TQ$I { IG$EIJ
‘
while (fgets(sendline, MAXLINE, fp) != NULL) {
write(sockfd, sendline, strlen(sendline));
‘
n = read(sockfd, recvline, MAXLINE);
recvline[n] = 0;
/* null terminate */
fputs(recvline, stdout);
TQ}TJQP$R K €TJLTM { TQR KH€FM { I
™ H
R €R K TM { } P$T$K }€T ƒ Mw}TEK TQ$KHP$I€€ ˜ T ~{ { K ¯°}TMS$I
I}P$T {u{ IE3
T IR € ƒ M$ILEK }P$FH} { I
}
}
echoudpcli-connect.c
W
¡•œDOT€ ˜ FQ}TE TE•€F ƒ M { † ’‡
W
65
PRATICA (Lez. 3)
#include
#include
"basic.h"
"echo.h"
echoudpcli-count.c
void server_echo_udp_count(int sockfd, struct sockaddr *p_cliaddr, socklen_t
clilen);
static void gestisci_interrupt(int signo);
void server_echo_udp_count(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen)
{
int
n;
socklen_t
len;
char
mesg[MAXLINE];
n = 240 * 1024;
setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
int main(int argc, char **argv) {
int
sockfd;
struct sockaddr_in
servaddr, cliaddr;
for ( ; ; ) {
len = clilen;
recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
count++;
sleep(1); /* rallentiamo il server */
}
if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
err_sys("socket error");
if( bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 )
err_sys("bind error");
signal(SIGINT, gestisci_interrupt);
server_echo_udp_count(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr));
}
Autunno 2003
66
PRATICA (Lez. 3)
int count = 0;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family
= AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port
= htons(PORT);
&%
¡%œŸDOT€ ˜ FL}TE #TE€F ƒ M { † H‡
Autunno 2003
}
static void gestisci_interrupt(int signo) {
printf("\nDatagrams ricevuti: %d\n", count);
exit(0);
}
¡%œŸDOT€ ˜ FL€R K TM { €F ƒ M {
ž$EEFHEKHM$FMO}TG$M$IR I { K
Autunno 2003
67
Autunno 2003
68
PRATICA (Lez. 3)
#define NDG
2000 /* #datagrams to send */
#define DGLEN 1400 /* length of each datagram */
robdep@zircone:~/Corsi/Reti/C> netstat -s | grep -C4 "Udp" | tail -5;
Udp:
6686 packets received
2012 packets to unknown port received.
9674 packet receive errors
18634 packets sent
echoudpcli-count.c
void client_echo_udp_count(FILE *fp, int sockfd, const struct sockaddr *pservaddr,
socklen_t servlen) {
int i;
char sendline[MAXLINE];
for (i = 0; i < NDG; i++) {
sendto(sockfd, sendline, DGLEN, 0, pservaddr, servlen);
}
robdep@zircone:~/Corsi/Reti/C> echoudpsrv-count
Client in un’altra shell; dopo un pòCTRL-C
Datagrams ricevuti: 11
}
FT
robdep@zircone:~/Corsi/Reti/C> netstat -s | grep -C4 "Udp" | tail -5;
Udp:
7206 packets received
2012 packets to unknown port received.
11154 packet receive errors
20634 packets sent
Y[Z\k%\ o bdcHe gkH^`lwbna`\ ojd\ e_o
NIJL…$K IL}FR FLR IX ƒ MS$K FM$TL€ ˜ T3}P$T$K }€TLKH$I { IGHEIJ
‘
Ypfq^?]jn]°sHkHkHb#§?bg]bxsH]°o jqlO^
PRATICA (Lez. 3)
9
`sHb#]e^|k%\ o bqcHe u]bd_ hbd_
robdep@zircone:~/Corsi/Reti/C> echoudpcli-count 127.0.0.1
robdep@zircone:~/Corsi/Reti/C>
D$EFH…$R TJQI
Autunno 2003
70
PRATICA (Lez. 4)
B
Y[Œctm?_ ^?a`_rjdlgl‹jg§%b#h6bga%b#]e
o _ bn§`sbio c•m?sHe
]?o lns%\ e jdcHbjdlObdcHeb
{ IM$$IE~OK M$P ƒ{ † R TG$G$TETL$I { I} { K TEI‡
‘
¡%MO}FH€WT { †R TG$G$TET3$IRH}FH€WT { ‡
‘
Ypµ
–
Y
)*+ ,-./+ 0132-4$5 +6/ 072+62+895 -.*:
;97.-93-3<+ =5 :>?93+ *A@*B:.>?90+ C9
UVFH$TR R KH$KH™ ­ š
) h6q: /7&
“
%“
H“
B
h /(2. h@ 6 ,e: d: /7%& : /7&
@(:(+', h +:-* %-&/7+ ) :
_<*=: /7698 h/=h
d h ) q =
c Z —) Z )
z
J^
%
6R FH€WK M$GO™ ­ š
71
Autunno 2003
72
PRATICA (Lez. 4)
PRATICA (Lez. 4)
applicazione
kernel
sincroni
System call
Recvfrom
=
Non ci sono
datagram pronti
FASE 1:
attesa
9_ k ,h%d:*+˜+1Z(d7%%:8Z(+:(Z(,=:+'Z(,j+:Z() +1q %d08 +/7%8 , %) + Z() % d() 8 hq%6=%8 /7*e% * h ) 6l7%˜Z(%;%1@* +8 ) :
™ M ƒ M$I3FHP$TEISHK FHM$TQ$KR T ~{ {ƒ EIQ$I
P FH}}K IJLFw$K } { K M$G ƒ TETQ ƒ TXI}K
$
’H“
H“
‘
tW
Autunno 2003
K M$€EFHM$F •K RHP$EFH€T}}FQ}KH…$R F€€IL† ƒ IMH$Fw€ ˜ K IJQI
R H
F P$TEIS$K FHM$TL$KHR T {u{ƒ EI‡XK M$FwIR R IL€FHM$€R ƒ }K FHM$T
$TR R FHP$TEIS$K FHM$T
‘
]?o?¸?\ ^°kHkjn]b
$bq_ hbis%ctlw^?§?^„mb_m^?ebd_Ÿ²j]?mHb°e e d
j _rbd³§%jim?o
kHjdcHjd\ o§`o?o c•m?sHe
™ RHP$EK JLFL€ ˜ TQP$EF ƒ €T3$I { K K TM$TLR T {u{ F
‘
BLOCCATA
ILP
IEKHJ3FH$TR R KH$KH™MHP ƒ{ ­ š ƒ{ P ƒ{
’H“
H“
%
W
Y[Š^`_lOjq\ lObdcHe bis%cHjg¥
s%c #o ^`c$bg§`o?Z
c$^`ck%o]^`c^v§?j#eo§?ji\ b°a?a%bd_rb
PRATICA - Lezione 4
‘
J (–
¸?¸?o jqlw^|ho ]e ^„s%c¬b]bqlvm?o ^„o c¬k?s%o?o \k%\ o bdcHe bq_ j
¸?\ ^?kHkj#e ^vjx\ b#a?a%bq_ubg§%jg]e jqc$§%jq_u§ o c%m?sHe
FMOP$F { T ILR TG$G$TE~TLK R ™ v} ƒ RH}FH€WT {
‘
!
#"$%& '
(
datagram pronto
Copia datagram
ƒ Mw€IM$IR TQ$KH™ ­ š
FASE 2:
copia
Processo continua
(elabora il datagram)
Ritorna OK
Copia completata
–•FHM$…$R FH€WK M$GO™ ­ š
™ ­ štJ
Autunno 2003
73
applicazione
kernel
FASE 1:
attesa
EWOULDBLOCK
System call
Recvfrom
Recvfrom
Copia completata
™­ t
š G ƒ K $I { FQ$IKH}TG$M$IR K
datagram pronto
System call
Copia datagram
BLOCCATA
BLOCCATA
FASE 2:
copia
Ritorna OK
FASE 1:
attesa
Ritorna “pronto”
datagram pronto
Copia datagram
Processo continua
(elabora il datagram)
Non ci sono
datagram pronti
BLOCCATA
System call
74
kernel
System call
select
EWOULDBLOCK
Recvfrom
Autunno 2003
PRATICA (Lez. 4)
applicazione
Non ci sono
datagram pronti
System call
Recvfrom
'
ƒ R { K P$R T K M$G
PRATICA (Lez. 4)
Processo continua
(elabora il datagram)
FASE 2:
copia
Ritorna OK
Copia completata
™­ t
š I}K M$€EFHM$F
Autunno 2003
75
Autunno 2003
76
PRATICA (Lez. 4)
applicazione
PRATICA (Lez. 4)
kernel
applicazione
Non ci sono
datagram pronti
System call
signal
kernel
System call
aio_read
Non ci sono
datagram pronti
FASE 1:
attesa
SEGNALE
GESTORE SEGNALE
Recvfrom
datagram pronto
datagram pronto
Copia datagram
Copia datagram
BLOCCATA
System call
FASE 2:
copia
Ritorna OK
Processo continua
(elabora il datagram)
J
ƒ MS$K FHM$TL}TR T€ {
FASE 2:
copia
GESTORE SEGNALE
Processo continua
(elabora il datagram)
Copia completata
77
PRATICA (Lez. 4)
•
FT
* ,e8 @06j)h,/7,j& : .<%';+, ; ) * %6Ž
) h/7& , ; @* %-6rŽ
Autunno 2003
(l57%,=@(8 */7@769, , +'8=:,,@(h8h+8 %A8 %6eh/(2A@/i)d()h*@(8 h/‘+/7,=, :*7hJ.<d7n%-%',h+>@=, * /=%h/=/f=+ fe%,=:8 .h=%‘'h+@0*7,&d=%-j ,]e: +—/7h3]=:+)/0h 6l=+ %1) ,e@(: .</ %h@7,1[
d7h:-8 :, h8 /7+ +/76l7%A* %A/7h/6g:* h/7hZ(%* 698 :, h8 :gd(8 h/0,e:
6t ! !!! !  " &H ( ! !!
! 1 6t !& 1 .!! !&! ! ! !7 01 57: .,=8 .<@7,%t=Z@(:8+'+, +6eh.</i%/=@(, /%œ,=j: d7.<h %') h) : /7@=&=, n d7+8 :7k+ ”t‘/7h/ +*=d7%-, +8 3% [ 8 : , h8 /0+
%'+ Z * %-, ‘1d(8 h/=,e:-d=%8 + %, e@8 +
{ o 8 : , %* %-,H‘d(8 h/=,e:-d7%8 ) +A) * ) 6g8 : , e@8 +
= e 6e%6td7 ,e 01: h!&/(* %1, ‘6eh / !Z : f(%: h/(:d7+8 ,=: 6jh&) +!8 : (
qK JQTF ƒ{
’H“
‘
%H“ Z
§?b#]k%_o me^_•]?o jdcH^„m?_ ^`cHeo?mb_b]]b_ bx\ b#ee
o
Y[Z\eo lwb^`s$e n§?j°e ^|§?jq\ \ jg]e_sHe es•_ j
Copia completata
78
PRATICA (Lez. 4)
bd_lOb#ee bn§`oj#]°mb°e ejq_ubgk%ªHbis%c$^v^„m?o ¬
¥
o \ b
Y
SEGNALE
D$IEIJQT { EKH}TR T€ {
Autunno 2003
: /76 @ (% * 4* '* % %'6jH, i lu‚
: /7,: /76* % @ %-(6j% , : /=* ,=] 4Z *  ,eŠ* %: .<,˜&) „u%3‡ …Li lL] !Z‚ ‡ * %'" ,C((Œuˆƒu‡
‡Š„u[ƒ6=‡h/g[]* Z, * ,=* 8%'@7, 6j,,=ˆ ": . ‡%-ƒu;'‡ + [ ) † " m‡( n
Ž
+ h8 % :8 :, h8 /7*h ‘J’ “3* %%8 h8 3% [” * % ,e: .<%'h@=1, [/(@.<%8 h Z : Z%* 6g8 :, h8 :(d(8 h/7,e:
F
~ ) (Z €
~ ) (Z €
) 1j
 ) Z
FASE 1:
attesa
‘
"
"" J
“
K R TQ$T}€EK P { FEŸ$I3€FHM { EFR R IET
5 ! !! ! !&!&
! D$IEIJLT { EKH}TR T€ {
W
Autunno 2003
" ;h:Zm6La yr =euc o hb j] Z * %-, &&„u% ƒr‡ nŽ
" ;h:Zmayr 5kXe "Jj: /=%, r& „[ H] Z 01 !&* %-H, & „uƒu‡ n Ž ! " ;h:Zmayr # &o c j: /=%, r& „[ H] Z 01 !&* %H, & „uƒr‡ n Ž ! " : /7,aLyL!= c 5(!5 e j: /7, %& „[] Z 0(* !š%-, & „L ƒr‡ nŽ ( ! b,X"
J B L• B |%
W*^
"}
" ;pe + *(* +-i)%, * 8hA%Ak% d7*=“t%:+”—8(.<jt% :h ] ]=]e: ::69)%/0:,%%Z(/0%8 fe%*U+ 69*7* Z(8 +': +d7,j, :0) h+q 8%:(Z(:8 /(/7%:%f9* :6g)+8 /7: ,—h hZ(8 œ+ :u“L”L[ fJn [ t[ 3.<+ =] Z—Z(%';'%
D$TE•$T}€EK #TEKHG$R KHK M$}K TJLK}K ƒ } ILR IQ} { E ƒ{~{ƒ EI`X %}T { € ˜ T3¹
ƒ MOK M$}K TJQTL$K…$K {
‘
Y
‘
‘
‘
œT}€EK {u{ FHEKP$EFM { K
Y
™ R}FH€WT { ¹Q} { I { FQ€ ˜ K ƒ }FLK MO}€EK {u{ƒ EI
‘
N
¹
HkHkHb °o ^`c%o?mbd_]^?k%¤b#e
EEK #FQ$KH$I { KX ƒ FEKH…$IM$$I
WTE}K FM$TL}TR T€ { † H% ‡
Autunno 2003
83
if (n == 0)
err_quit(“%s [%d]: server disconnesso“,__FILE__,__LINE__);
fputs(recvline, stdout);
}
if( FD_ISSET(fileno(fp), &rset) ) {
if( fgets(sendline, MAXLINE, fp) == NULL)
return;
if( (reti_writen(sockfd, sendline, strlen(sendline))) < 0)
err_sys("write error");
}
B " 6 : %/7W , hA8 :6e%';-%)% —, %W 8 . : /7+ +6jh/(/7%*=*e:h/7% 3J
" c57)%/0) f=+A* % ) )%-6e,: ) 6 ) :%/0,* %A/0%A* )+8 %2(27% +'6e6jh8 , h—Z(hd7h
}
echocli-slct.c
T€ ˜ FH€R K TM { TQ}TR T€ {
‘
Autunno 2003
84
PRATICA (Lez. 4)
Ypfq^`cH§`o #o ^`c%oa?b]eo e bg§%jn]bd\ b#kHeLo ct\ be es•_ jn]°s¬]e§`o c
b#§„s%c¬]^?k%¤b#e
Client
Data o EOF
stdin
socket
™ H
R €R K TM { EK T}€TLI3G$T} { K ETL}K ILR K M$P ƒ{ $I { I} { K TEIQ€ ˜ TQR K M$P ƒ{
$IR}FH€WT {
TLK RH}TE TE { T EJQK M$I #K TM$TL}P$T$K { F
82
PRATICA (Lez. 4)
if( FD_ISSET(sockfd, &rset) ) {
if ( (n = reti_readline(sockfd, recvline, MAXLINE)) < 0) {
if( errno == EPIPE ) {
err_msg(“%s [%d]: server disconnesso“, __FILE__,__LINE__);
break;
}
else
err_sys("readline error");
}
PRATICA (Lez. 4)
‘
2003
† ’HAutunno
‡
FD_ZERO(&rset);
for( ; ; ) {
FD_SET(fileno(fp), &rset);
FD_SET(sockfd, &rset);
maxfdl = MAX(fileno(fp), sockfd) + 1;
if( select(maxfdl, &rset, NULL, NULL, NULL) < 0 )
err_sys("select error");
ƒ MOTEEFET
T€ ˜ FH€R K TM { ”
}
WTE}K FHM$TQ}TR T€ {
void client_echo_select(FILE *fp, int sockfd) {
int maxfdl;
fd_set rset;
char sendline[MAXLINE], recvline[MAXLINE];
int n;
% | ‘
‘
T€ ˜ FH€R K TM { ”
81
PRATICA (Lez. 4)
"$"$## +>-hd7* h-%& 8 +-) f(:+1: h*7/0:(%d(Z@ :*6j69+8 .:, =2(@(:+8 +18 %8 :6j, hh8 / /7+1) %: h) /(d7fg@(:.<h%/(:8 hZ(%Z :g:(* 20h4'6r, q% %-,
t% ] ] %', e: ;'+.<%/=, %1d7+*7* +-,e:7+ )L) : ;% ) ) h Z :7,=8 +*=d7h8 , h
" <
p/ -hd7%8 +'f9:h/7% Z:* 6g8 : , e@8 +&(%/=%8 +15 c z l c l e
"$#) -%-h8 d7h%88 %+-f(: h/0%Z :* 698 :, =@(8 +18 : , h8 /7%8 œ’ “% %8 /7h *=d0%-69:]=:6l0%8
! < W
Y
™ H
R }FH€WT { ¹ ƒ M R K } { TM$K M$G d
}FH€WT { TL€K}FHM$Fw$TR R T
€FHM$M$T}}K FHM$KH€FHJQP$R T { I { T
N ¹ ƒ MOTEEFET
Autunno 2003
H^?k%¤b#eLo c]k%_o e es%_ j
™ RM ƒ JLTEFL$K…— { 3
T $KH}P$IS$K FQ$K }P$FM$K …$K R TQM$TRH… ƒ XrXTE$TR
WTEM$TRH¹QJLIG$G$K FHETQ$K
‘
ƒ MO…— { T3$ILR TG$G$TET
™ RH}FH€WT { ¹L} { I { FL€ ˜ K ƒ }FwK MOR T {u{ƒ EI
‘
JLI X ¹3K RHM ƒ Q
J TE~FwJLI}}K JLFL$K$T}€EK {~{ FHEK
TXrXT { K IJLTM { T ƒ }I { K
" ' # +1* h& ) (: +>
*=:(d@ 6j+. 2(:+8 %)6jh/ )%hd0fg: h/(:Z%:g* h6q %,
"" e# -(*hid72>%8* +', fg+,:hh1/7%8 :6=Z%:; )%@7,, h1e@(: 8) +>a c8 d: , h8 /=+ e hCa
" #) -%-h8 d7h%88 %+'fg:h/7% Z: )%, e@(8 +>8 : , h8 /=%8 €’
“<% %8 /7h *=d7%-6g:]=:r6 l7%8
$^?k?¤
bewo ct\ b#e e
s•_rj
ƒ IM$$FQ€ ¹QIR JQTM$F
‘
–IL€F} { I M { T œ
ž
™ žL† }TR T€ { “ ˜ ‡Ÿ¹QK RM ƒ JLTEFL$K
$T}€EK {~{ H
F EKHK MX ?}T { † }H
F R K { IJLTM { TQ’
?‡
‘
80
PRATICA (Lez. 4)
UT
b#]]bd_ bisH]j°e ^
Y
Autunno 2003
¶sHjdcH§?^·s•c]^°k%¤be §%b#]k%_o me^`_ xm?_ ^`cHe ^·m$bq_
œŸT}€EK {u{ FHEKP$EFHM { K
79
PRATICA (Lez. 4)
TCP
ƒ Mwž$š Q} ƒ RH}F€WT {
RST
data
FIN
B ?
¢
{ FHP IM$
B
´LIK {
Autunno 2003
N
Y[Z\k%\ o bdcHe`^`mbd_ jio c|lO^?§%jq\ o e g]e ^`m rjdcH§ wjdo e
P$T$K }€T ƒ M$ILR K M$TI3$KHK M$P ƒ{ TL}KH…$R FH€€ILK MOI {u{ T}I
‘
$TR R ILEK }P$F} { IQ$TRH}TE TE•T€ ˜ F
W
Tempo 0
C
Y
K M$P ƒ{
Autunno 2003
86
PRATICA (Lez. 4)
•o]°mb#§`o ]kH^`cH^x\ bi_o k%ª%o b]e bgkH^`cH]b6k%sHeo hjdlwbdc$e b
B
]bdc jgj#]%mb°eejd_rbx\ bi_o ]°m^?]e b #k%ªHbvjq__o hbq_ujqc%c$^
§?^`m^
Tempo 4
Tempo 0
dati
Tempo 4
d1
C
d5
C
d4
d3
d2
r1
S
S
d5
d4
r1
d3
r2
S
S
d6
r1
d5
r2
d4
r3
S
S
d7
r2
d6
r3
d5
r4
S
S
S
echo
Tempo 1
6I { € ˜
85
PRATICA (Lez. 4)
Tempo 1
Tempo 5
dati
Tempo 5
d2
C
d1
d6
C
echo
Tempo 2
Tempo 2
Tempo 6
dati
Tempo 6
d3
C
d2
d1
d3
d2
d7
C
echo
Tempo 3
Tempo 7
dati
C
B
Y
Tempo 3
˜$ƒ{ $F´LMw$TR R IL€FHM$M$T}}K FHM$T
xW#TE}K FHM$TL} ˜$ƒ{ $F´QM
87
void client_echo_shutdown(FILE *fp, int sockfd) {
int
maxfdl, stdineof;
fd_set
rset;
char
sendline[MAXLINE], recvline[MAXLINE];
int
n;
˜ F´ { F
Ÿ¡
°œ¯
Ÿ¡
°¯
T€ ˜ FH€R K TM { ”
¡
|
’w}TLTEEFET¯
°œ
™
if( FD_ISSET(sockfd, &rset) ) {
if ( (n = reti_readline(sockfd, recvline, MAXLINE)) < 0) {
if( errno == EPIPE ) {
err_msg("%s [%d]: server disconnesso",__FILE__,__LINE__);
break;
}
else
err_sys("readline error");
}
}TQš
WTE}K FHM$TQ} ˜$ƒ{ $F´QM
89
PRATICA (Lez. 4)
}
if( FD_ISSET(fileno(fp), &rset) ) {
if( fgets(sendline, MAXLINE, fp) == NULL) {
stdineof = 1;
shutdown(sockfd, SHUT_WR);
FD_CLR(fileno(fp), &rset);
continue;
}
if( (reti_writen(sockfd, sendline, strlen(sendline))) < 0)
err_sys("write error");
}
}
B
F
Autunno 2003
if (n == 0) {
if( stdineof == 1 )
return;
else {
err_msg("%s [%d]: server disconnesso",__FILE__,__LINE__);
exit(-1);
}
}
fputs(recvline, stdout);
echocli-shtd.c
FD_ZERO(&rset);
for( ; ; ) {
FD_SET(fileno(fp), &rset);
FD_SET(sockfd, &rset);
maxfdl = MAX(fileno(fp), sockfd) + 1;
if( select(maxfdl, &rset, NULL, NULL, NULL) < 0 )
err_sys("select error");
"!
‡
IR FET3$KHEK { FHEM$F
88
PRATICA (Lez. 4)
%o§?b#hbgk%ª%o sH§?bq_ubio \]^?k%¤b#e`]^`\ ^„o c¬]k%_o ees%_rjgb
\ j#]k?o jq_\ ^|jqmbd_re ^„o ct\ b#e e
s•_rj
P$T$K ETQK R ™ g}FHR FLK M ƒ M$IQ$K ETSHK FHM$T
‘
K M {
Autunno 2003
PRATICA (Lez. 4)
B
J H–
9 J^ # B › 3• &™ B › 3• £™ B › 3•< ™
d8
r1
C
C
S
T€ ˜ F€R K TM { ”
Autunno 2003
JŸ
W
K M$€R ƒ $T
}~—} ­ }FH€WT { “ ˜
K M { } ˜$ƒ{ $F´LM$† K M {
d1
echo
›s$jqcH§?^„o \k?\ o bqc$e`¥
o c%o ]kHbg§`o]?mb§`o _ bicH^`ctm?s
k%ª%o s$§?bq_ bxo \]^?k?¤
be
NKHP$FH}}FHM$FwT}}TEŸIM$€FEIQ$I { KHK MOIEEK #F
‘
Y
Tempo 7
d4
C
S
Y
W
TR T€ { P$TEK R}TE TE
Autunno 2003
90
PRATICA (Lez. 4)
^?]]?o jdlw^·s$]jd_ bg]bq\ bkHe jqcHk%ªHbxcHbq\]bd_ h6bd_
Ypµ
B
\?m^?]e ^v§`ok%_ b#jq_rbis%c¬¥o a`\ o ^·mbd_Ÿ^?a`c%o
kH^`c%c$b]]#o ^`cHb
TR T€ { P ƒ
R TGHG$TETQ$I {ƒ{~{ KHK€R K TM { €FM$M$T}}K
‘
Y
*V
$e
_sHe e s_ bn§?j°eo?sHeo \ o j°e b
EEI—E}T { ¯€FHM { K TM$T`XK R TQ$T}€EK P { FEHTKH}F€WT {
ƒ{ K R K SS$I { KH$IRH}TE TE† }K ILR K } { TM$K M$GO€ ˜ T €FM$M$T}}K ‡
EEI—€R K TM { ¯°€FHM { K TM$TQK M { TEK€ ˜ TQK M$$K €IM$F`X
‘
W
‘
client
1
-1
2
-1
3
-1
FD_SETSIZE-1
-1
}
rset
fd0
fd1
fd2
fd3
fd4
fd5
0
0
0
0
0
0
…
B
W
{ E ƒ{u{ƒ ETQ$I { KHP$TEK R}TE TE
B
91
PRATICA (Lez. 4)
B
" 5(@(d(d0h/(:+. h 6l=%1: ) ] Z 8 :, h8 /7+, h 2f
client
rset
fd0
fd1
fd2
fd3
fd4
fd5
0
0
0
1
0
0
92
PRATICA (Lez. 4)
ƒ IM$$F ƒ MO€R K TM { } { I…$K R K }€T ƒ M$IL€FHM$MT}}K FHM$T #TEE
€ETI { F ƒ Mw
} FH€WT { P$
T EŸR IQ€FHM$M$T}}K FM$T3€FHMOR I`X ƒ MH
S K FHM$T
I€€TP {
‘
\ o ]e bdc
ƒ $
P P$FHM$K IJLFQ€ ˜ T3G$R KH} { IM$$IE~XK R TL}K IM$FwIP$TE { KHTQ€ ˜ T
‘
K RXOEK { FHEM$I { FwHIQR K } { TMO}K I
ŸIR TQK MXFHEJLIS$K FHM$T TE~E
JQTJQFHEK SSHI { ILK MOE}T {
‘
XW
Autunno 2003
XW
'
Y[Z\]bd_ h6bd_k?_rb#jxo \?\ o ]e bdc%o c$av]^°k?¤
bek%ª%o jdlwjdcH§?^
•
W
{ E ƒ{~{ƒ ETL$I { KHP$TE•K RH}TE TE
Autunno 2003
…
1
4
2
-1
3
-1
rset
fd0
fd1
fd2
fd3
fd4
fd5
0
0
0
1
1
0
…
Y[Z\]bd_ h6bd_k?ª•o jdlwjg]bq\ bk$ewmbd_V\ ba%a•bq_ bv§?jnesHe eo?o
' (
]^?k?¤
bewˆu¥
o \ bg§?b]k%_o m$e^`_qjmbd_re
o
R R K M$K S$K FL€ ¹Q}FHR FLK RHK RHR K } { TM$K M$GO}FH€WT { ¯?} ƒ X
ƒ K M$$KK RHP$IE~IJQT { EFLJLI Xw$KH}TR T€ {  T L
T T}}TET
‘
‘
B
B
W
W
{ E ƒ{u{ƒ ETQ$I { KHP$TEK R}TE TE
tW
W
"}
"h<<8@+: /(69Z:-:* :h) /7d7hU+8 Z+@=.<%1%,=* 8h-h 6q.<%+,1=[ ] *7Z—@Z ] Z:-* —j% )%-)6e: * ,3, %Zg/(%';: /7% &=%ng%*=* %] Z—8 %fk j 6eh//=%*7*=: h/7%3n
B
B
93
PRATICA (Lez. 4)
ƒ P$P$FHM$K IJLFQ€ ˜ T ƒ MwIR { EFw€R K TM { }KH€FM$M$T {u{ T¯ TE~E
€ETI { F ƒ MLM ƒ F w
F }FH€WT {
‘
tW
1
5
2
5
3
-1
3
-1
fd0
fd1
fd2
fd3
fd4
fd5
0
0
0
1
1
1
‘
tW
rset
-1
W
"}Z h @08 +%6g6eh:*/(h/=/7%h3*7*=,e:h8 %A/g:* h-6q%-,H[ *=@] Z—j ) : * , %/(: /0&=n
[H%] Z—f g %˜] Z d7%8 ) %
" @(: (/ Z :: ) d0+8 +.<%-,e8 h . + =] Z—Z :* % )%6e, Zg%-;% %*=* %8 %
œŸKM ƒ F FwK RH}TE TE€ ˜ K IJLIQ}TR T€ { P$TER TG$G$TETQ$I {ƒ{u{ KK
}F€WT { † XK R TQ$T}€EK P { FHE ‡ŸIP$TE { K
W =W
2003
ž$€ ˜ Fw}TE TE TE}K FHM$T3}TR T€ { † ’Autunno
‡
‘
echosrv-slct.c
if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err_sys("socket error");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family
= AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port
= htons(PORT);
if( (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr))) < 0)
err_sys("bind error");
if( listen(listenfd, BACKLOG) < 0 )
err_sys("listen error");
ƒ TR R IQ€ ˜ T
fd0
fd1
fd2
fd3
fd4
fd5
0
0
0
1
0
1
…
95
-1
W
"}
"hj 6e8@(h+:/(/69Z/7:-%:**=:h*=/0:d7hh+/78 %3Z+n@=.<%1%,=* h8 h 6q.<%+,1=[ ] *=Z—@Z] Z:* —j% %) 6e: * ,, %Zg/(%-;: /7% &=%n
[H*=*%U%]8 %Zg
™ H
R }TE TE€ ˜ K IJLIQ}TR T€ { P$TER TG$G$TET3HI {ƒ{u{ KHKH}F€WT { † XK R T
$T}€EK P { FHE‡ŸIP$TE { K
)
PRATICA (Lez. 4)
int main(int argc, char **argv)
{
int
listenfd, connfd, sockfd;
int
i, maxi, maxfd;
int
ready, client[FD_SETSIZE];
char
buff[MAXLINE];
fd_set
rset, allset;
ssize_t
n;
struct sockaddr_in
servaddr, cliaddr;
socklen_t
cliaddr_len;
94
PRATICA (Lez. 4)
…
FD_SETSIZE-1
FD_SETSIZE-1
Autunno 2003
-1
2
rset
e
bW
4
1
W
{ E ƒ{~{ƒ ETL$I { KHP$TE•K RH}TE TE
ƒ P$P$FHM$K IJLFLFEI3€ ˜ TLR I3P$EK JLI3€FHM$M$T}}K FHM$TL†
ƒ }IX %‡ TMHG$IQ€ ˜ K ƒ } I
‘
client
client
-1
œŸKHM ƒ F FwK RH}TE TE•€ ˜ K IJLIL}TR T€ { P$TE•R TG$G$TET3$I {ƒ{u{ KHK
}FH€WT { † XK R TL$T}€EK P { FHE ‡IP$TE { K
‘
Autunno 2003
W
" 5(@d(d7h/:+.<h 6l=%>: ) ] Z 8 : , h8 /7+-, h 2
FD_SETSIZE-1
)
W CW
&%
2003
ž$€ ˜ FL}TE TE TE}K FHM$TQ}TR T€ { † HAutunno
‡
96
PRATICA (Lez. 4)
maxfd = listenfd; /* inzializza il numero di descrittori */
maxi = -1;
for ( i = 0; i < FD_SETSIZE; i++) client[i] = -1; /* inizializza l'array client a -1 */
FD_ZERO(&allset); /* inizializza a zero tutti i descrittori */
FD_SET(listenfd, &allset); /* setta il descrittore di ascolto */
for ( ; ; ) {
rset = allset; /* insieme descrittori da controllare per la lettura */
if( (ready = select(maxfd+1, &rset, NULL, NULL, NULL)) < 0 )
err_sys("select error");
if( FD_ISSET(listenfd, &rset) ) { /* richiesta ricevuta dal listening socket */
cliaddr_len = sizeof(cliaddr);
if( (connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &cliaddr_len)) < 0)
err_sys("accept error");
for(i = 0; i < FD_SETSIZE; i++)
if( client[i] < 0 ) { /* cerca il primo posto libero per il nuovo il descrittore */
client[i] = connfd;
break;
}
if( i == FD_SETSIZE ) err_quit("troppi client");
FD_SET(connfd, &allset); /* setta connfd per la select */
if( connfd > maxfd ) maxfd = connfd; /* aggiorna maxfd */
if( i > maxi ) maxi = i;
/* aggiorna maxi */
if( --ready <= 0 )
continue; /* se non ci sono altri socket pronti riprendi da select */
}
W =W
ž$€ ˜ Fw}TE TE TE}K FHM$T3}TR T€ { †
Autunno
2003
‡
BW œTM$K IRHFX
97
TE K €T
PRATICA (Lez. 4)
{u{ I€W
Autunno 2003
98
PRATICA (Lez. 4)
JŸ
Ypfq^`cH]?o §?bd_o jqlw^„\ jn]b#a`s$bdcHe bg]?o esHj #o ^`cHbgk%ªHbim?s
j#kHkHj§?b_ bgkH^`cto \]bd_ hbd_Ÿho ]e ^·m^?kH^|¥j
¡%MO€R K TM { }KH€FHM$M$T {u{ T¯°}PT$K }€T ƒ MO}FHR Fw…— { TL†€ ˜ T
‘
M$FHMO}K I ƒ MOM$T´wR K M$T‡qTQM$FHMXILP$K wM ƒ R R I
™ RH}TE TE€ ˜ K IJLI3ETI$R K M$TL€ ˜ TQR TG$G$TE
K RH}K M$G$FHR Fw…— { T
‘
JLIQ}KH…$R F€€ ˜ TE
M$TR R IQPHEFH}}K JQIL€ ˜ K IJQI { ILI3ETIwK M
I {u{ T}IL$K ƒ MwM$T´wR K M$T
for( i = 0; i <= maxi; i++ ) {
/* controlla tutti i socket attivi per controllare se sono leggibili */
if( (sockfd = client[i]) < 0 )
continue;
Y
W
if ( FD_ISSET(sockfd, &rset) ) {
/* se sockfd è leggibile invoca la readline */
if ( (n = reti_readline(sockfd, buff, MAXLINE)) == 0) {
/* connessione chiusa dall'altro endpoint */
close(sockfd);
/* rimuovi sockfd dalla lista di socket
che la select deve controllare */
FD_CLR(sockfd, &allset);
client[i] = -1; /* cancella sockfd da client */
}
else
reti_writen(sockfd, buff, n);
if ( --ready <= 0 )
break;
}
W
Y
W
W
™ H
R }TE TE¹Q…$R FH€€I { FLTLM$T}} ƒ MwIR { EFw€R K TM { EK €T TE
}TE K S$K F
‘
Ÿbdc%o jq\^?¥
KR
$bq_ hŸo kHbgµqe ejk%¤
¡%MO€R K TM { EK T}€TLIXIEKJQFH$FL€ ˜ TQK RH}TE #TEŸM$FHM
EK }P$FHM$$I3P$K OIwIR { EKH€R K TM {
‘
W
Y
}
}
}
œŸTM$K IRHFX
BW TE #K €T
{~{ I€W
Autunno 2003
Y[Z\?m?_r^`¸?\ blwjg§%bd_o hjg§%jd\¥j#e e ^|k?ª$bxo \]bd_ hbd_•]?o
lwbdcHe_ g
b k%o]^`cH^|jqcHk%ªHbnjq\ e
_ok?\ o bdce
D$F { E
T …H…$TQIMH$IET3…$TM$T3}TLK RH}TE TE•$T TQG$T} { K ET
}FHR Fw€R K TM {
F
Y
W
W
]bd_ h6bd_]^`lgl‹j
ƒ M
100
PRATICA (Lez. 4)
E Y[£A^?§`o ¥
o kHjq_rbnb#k?ª$^?]°_ h r]?\ kHeLo cvlw^?§?^|k%ªHbg]e jqlgm?o
\ o cH§`o _o ^|§%bd\k?\ o bdcHe `s$jqcH§?^
K RH€R K TM { }K€FHM$M$T {u{ T
™ RH€R K TM { }K$K }€FM$M$T {u{ T
‘
‘
^?]]°o ¸?o \ o]^\ s °o ^`c%o
¡%}IET ƒ MO}K M$G$FHR FLP$EF€T}}FLP$TEŸFG$M$KH€R K TM {
¡ { K R K SS$IET ƒ M { K JQTF ƒ{ } ƒ R R TQFHP$TEISHK FHM$KH$KH™ ­ š
‘
¡%}IETQ™ ­ štM$FM …$R FH€WK M$G
‘
‘
Autunno 2003
Y[£A^?§`o ¥
o kHjq_rbnb#k?ª$^?]°_ h r]?\ kHeLo cvlw^?§?^|k%ªHbg]?o jis•c
¸?\ ^?kHkHjio c|s?c$jg¥s•c #o ^`cHbi_ bd\ j#eo hjvj§„s%c¬k%\ o bqcHe
‘
ž$}TE€K S$K
? 99
PRATICA (Lez. 4)
B
FH€WT { FHP { K FHM$}
Autunno 2003
102
PQa`c%o]^#k%¤b#e`jmb_ e ^„ªHjn§?bd\ \ bim%_ ^`m?_o b°enk?ª$bicHb
Y
§?b#ebd_lgo cHjqc$^vjd\ k%s%c%okH^`lgm^`_rejdlObdcHeo
F
C E
Y
Y
!
#"$%& '
(
jqcH^`\ ^?a?^·mb_Vo¥
o \ bn]^`cH^·\ bim?_r^`m%_o b#e g
k%ªHb
P Y
*V
Qa`c%o^`m #o ^`cHbiªHjxs%c¬hjq\ ^`_ bg§`o§%b#¥jqs%\ e
R € ƒ M$TLFHPSHK FHM$KH}FHM$FL…$K M$IEK TQ† FHMwFLFX X‡
R { ET ˜ IM$M$F ƒ M IR FHETQ†K M { FwIM$€ ˜ TL} { E ƒ{u{ƒ E TQP$K
‘
€FHJQP$R T}}T‡
‘
)*+ ,-./+ 0132-4$5 +6/ 072+62+895 -.*:
;97.-93-3<+ =5 :>?93+ *A@*B:.>?90+ C9
h6bqcHa?^`c$^va%b#]e
o e bne_rjdlgo ebx\ jg¥
s%c ?o ^`cHbn¥k%cHe\
–I`X ƒ MS$K FHM$T`X€M { RP ƒ
T}}TE~T ƒ }I { I3€FHMOKH}FH€WT {
‘
PRATICA - Lezione 5
N
^?]]^`cH^vb#]]bd_ bgkHjqlv¸?o j#eb ^`m °o ^`c%o
DW
Y
PRATICA (Lez. 5)
B e B ! t•
*™ b• *™ b• *™ b• *™ b• J•
Y
W
–K TR R K
šd–
™ D$D š
™ D$D š
™ D$D š
™ D$D š
‘
‘
‘
‘
šdN
Ÿš
Ÿš
Ÿš
Ÿš
ž
%™D
%™D
%™N6UVD
N#D
W
R K TR R FL}FH€WT {
R K TR R FL™ D
R K TR R FL™ DOP$TER I TE}K FHM$T
R K TR R FLJLT}}IG$G$KH$KH€FM { EFHR R F
R K TR R F qND
W
W
FW
W
W F•
Livello:
§?b#¥jds%\ e`§?bd\ \ bn^`m #o ^`c%o]°s•m?m^`_ e j#e b
Livello:
IPPROTO_TCP
G$T { }F€WFHP { TQ}T { }FH€WFP {
:: /7/7,,: /7&(6* %'%-@,=,e(* * hh(% 66 hh* 4d0d0,,* '::*/=/=h,, 6 %-, ::/7/7,, :: /=/0,, jj;;'hh:: ** hh-66 %%// ,, ) %)/u%n
/uŽ nŽ
+ h8 % :8 :, h8 /7h 3* % %8 h8 %J[” * %€h v
l +8 +.<%-,=8 :
* %',=* h+'6 , =h:;d=+ , hd75 f(<: h# /0% 5 k: %'c %d75 + :;% d7l %<8 # .<+/=, %/7*=%:f=8%'% h +: /=6e,h/(/=%*=*7:h/=% +', =:;+
;* h-h6 : %/ +'&, *=:f=%
&(%',e* h8 6 :, hh8 d0/7,+ + 5 & 8 +# / 5 %-f7k fe+ c :-5@(/i: /0, %8 hAl <: /?# *7:f=%% +-& +-& -*=* :+f=% 8 * % ) -hd7fg:h/7%12
: * +2(: :, +-, + 'hd(d(@(8 % :;%8 * h (+ * % hd7fg:h/=% )+2: : , +, +
C B
Y
°š
E
B
W
9
JŸ
-
Y
F
tW
?sJŸ|b]]bq_ bn]^`\ ^·\ be e jd
B c$^`c]k%_o e ej
¸?_r^?j#§?kHj#]e §%b#h6bgj¸?o \ o e jd_rb
Y
106
PRATICA (Lez. 5)
`sHb#]ejn^`m #o ^`cHb
$bq_ hbgjim%_rb#hbqc•o _ bi¸•_ ^?j§?k$j#]e`j#k$k?o §?bqc$ejd\ o
ž$}“ ƒ MOPEFHGHEIJQJQIQPHETM$$TQK MOK M$P ƒ{ R K M$$K EK SS$FL$K
$T} { K M$IS$K FHM$T
– ƒ{ TM { TLP$F { ET…$…$TQK M$}TEK ET ƒ MLK M$$K EK SSFw$KH…HEFHI$€I} {
‘
™ RHP$EFG$EIJLJQIQ$F #ET…$…$T3€FHM { EFHR R IET
‘
TLR FHPS$K FHM$TL¹3$K }I…$K R K { I { IQK RH…$EFHI$€I} { M$FHM K TM$T
‘
XI {u{ Fw€FHJ ƒ M ƒ T
9
‘
B W
"  :%/7%>8 : , h8 /=-+ , h ) -%8 h8 % e _ k k e 5
B ! š
Y
T
6ž$ž$D
DW
–™
ž
?G AE T F
Autunno 2003
108
PRATICA (Lez. 5)
¶sHjdcH§?^ njq¸?o \ o e j°e jimHbd_Vs%c¬]^?k%¤b#e Ÿf ‹]?mb°§`o ]kb
s•c|lOb]]j#a?a`o ^|§`okH^`cHe_ ^`\ \ ^v]bic^`c¬k g]kjdlg¸?o ^
§`o§?j#eo?mHbd_s•c|m$bq_o ^?§?^v§`o ^`_ b
™ B • !
N
Autunno 2003
Y[Œc¬jqm?m?\ o kHj #o ^`c$bgk%ªHbio c$ebdcH§%bn]°mHb#§`o _ubio c
—B
Y[Z\?m?_r^?kb#]]^„m?s „\ b#a?a?bd_rbio \hjd\ ^`_ubg§`o]^ ?bq__r^`_
e_ jlgo e b `sHb]ejg^`m °o ^`cHb
– K M { TEFwEK { FHEM$I { FL¹LK R IR FHETL$KH}F %TEE~FHE
‘
ϥN
#bdcH§%bxm^?]]?o ¸?o \ bi\ jge_ j]?lgo ]]?o ^`cHbx¸?_ ^?j°§?kj#]e
Y
107
B
š % °š
PRATICA (Lez. 5)
B C™ B •
Autunno 2003
jic$^?e
o ¥o kHjg§?bq\ \ b__ ^`_ubgj°hho bqc$bio c |lw^?§`o
TQK RHP$EF€T}}FL¹QK M ƒ M$I3€ ˜ K IJQI { ILI3}TR T€ { ¯°}K IQP$TE•R I
R T {u{ƒ EIL€ ˜ T3P$TE•R IQ}€EK {u{ƒ EI¯EK { FHEM$IL€FMOK RH$T}€EK {u{ FET
P$EFHM { FLIOT}}TETLR T {~{ F
TQ}KH} { I ƒ }IMH$FL™ ­ štG ƒ K $I { FQ$IKH}TGHM$IR K ¯ ™¢™š
‘
K TM$TLG$TM$TEI { F
‘
105
B *™™ —™
PRATICA (Lez. 5)
›s$jqcH§?^|]?ohbq_o ¥
o kHjis%c¬bd__ ^`_rb ds%c¬kH^?§`o kHbg§`o
bd__ ^ _ bvho bdcHbil‹bqlO^`_o j#e ^·o cts%cHjghj_o jq¸?o \ b
k%ª%o jdlwj#e jg]^ ?b__r^`_
Autunno 2003
~ ) (Z €  rq Hi lu‚
q Hj bƒ1„L… C‹ ‡t‰‡
‹ [ bŒuŠk‡H[ Z?†t‰ Št‹ r‡H[ q
q Hj Cƒ(„u… b‹ ‡‰t‡
‹ [ CŒuŠ m‡H[ Z?†L‰ Št‹ r‡1[ q ) )
 ) Z
=‘J’ “
‘
q Hj t[ Lh Lh v e [ uh v eue _ c  e [ “t[ L]1j 1n
n
Ž
) - UZ Lq ) )
Z b] ) =Ž
q ) JŽ v
q Hj L[ uh uh e [ uh v ere _ c  e [ ] ) *[ 3nŽ
Z ) ) 3[ (Z UUZ Z Z k” ) - ] ) 2 k”)
Y
IPPROTO_IP
Y[Z\?m?_r^?a _ jdlnlwjg]^?k%¤^`mHe ] rk%ªHb#k?¤Q]ejdlnmjxoh6jd\ ^`_o§`o
š %ž
Tg§?bee ^|k%ªHbgesHe ebg]?o jdcH^v]?s%m?m^`_ ej#eb
Y[Š^`c
104
PRATICA (Lez. 5)
Autunno 2003
% %! ! !%
! ! !1&! 0(! ! ((!! !! (!
!! ! 01 % % & ( ! ! !!  %%  & (
!! !! H !! !&!&! ! !  !&
!(! 1 ! @ ! 1(H !! !(! 5- H t * !* 1 0(
!š!š ! & ! !
@% .( ! !+ Livello:
SOL_SOCKET
P3a`c•o?\ o h6bq\ \ ^xª$jghjd_o bg^m #o ^`c•o
Y
+;+;>98: /+;>L698 96 +;8;++ : +3 /
+;/:9: +:
+;,L//-6 5 ' /
+; 5 +/:
+;: ' L> +; +78)L> +;: ' + 6
+;+78 + 6 +;: ' 5 /+
+; +78 5 / +
+;u: / /698)8):
+;:u/ /-+:
+; < -/
+; / +;3
+ ->96 9,
55 --;3+ 8)- : 5 5 3+ + + 5 - t-t, //-6 5 ' /
t- 6 : t- 6 /
t-+7+8/ 69<
103
PRATICA (Lez. 5)
H^`cH^|§`o hŸo ]bio c¬hjd_o?\ o hbd\ \ o
‘
šdPSHK FHM$K
Autunno 2003
Y
$bq_ hbgjil‹jqcHebdcHbd_rbgj°e eo hji\ jgkH^`c%c$b]]°o ^`cHb
Y
|m^?]]°o ¸?o \ okHj#]°o
N $KEK }P$FH} { I R IQ€FHM$M$T}}K FHM$TL¹3IMH€FHEIQI {u{ K I
v$KHEK }P$FH} { I R IL€FM$M$T}}K FHM$TQTEI3} { I { IL€ ˜ K ƒ } I¯
‘
K TM$TQG$TM$TEI { FLR TEEFHETQž$N6š
°ž ž
{
{
ƒ
{
FMO€ ¹QEK }P$FH} I¯ TM$G$FHM$FQ}P$T$K K R TEK FHEK
‘
JLT}}IG$G$KHFG$M$K w}T€FHM$$K
‘
^
^
W
(
W —–š–˜™ B •
" &(d %%/7*=%*7@(8 +/7, +Ah 8 ) :-%d7k h8 *h,l +˜8 % Z(ehc d0chmw “ueu“ y hC. p : /(c @7,=:7%k“ 3* %'6jh/1Z : ‘-; :%/=%
" we%h*=* 5+-c &(p&d*: coe w _ k Z :7%8 h8 %A*=@ ) )+18 %-, %3[; :%/7%&(%/7%8 +', h ) -%8 h8 %
W
–
B B
H– *™
š %–™ •¢ž
109
PRATICA (Lez. 5)
•
Y
]?mb#§`o #o ^`cHb
r !! &J! !u! H
! J!!
R %FM$FXrX
|¯
Z
Z ) q ) Z
)Z
)
Z
R %FM$FXrX
|≠ ¯#R %R K M$G$TE |
J[ Z
Z UZ Z
e ) c w e {D_ c [ l )
))
R %FM$FXrX
|≠ ¯#R %R K M$G$TE ≠ |
(Z t] ] l2
) Z
) J)
Z
Z
l UZ
(Z (Z ) ) ) ) UZ ) )
) *)
‘
‘
B &™
š
°N
–š
• B B – •
¢ ¢
vT
š
œ–
š
<
J2
¢wjebd_l‹jq_¤
™ R$TXI ƒ R { ¹
‘
32
¢
% | BB !99! B •š™ ™ šdN
š N
d
‘
‘
M
Y
ŸD$ž
°ž
°œŸ¢
F
•
•Ÿ™UVžHšŽT
B B – C•
š
2003
%œ ™ UžHAutunno
š
112
PRATICA (Lez. 5)
* ,=8 @76j,,e: . )h%-;/7+&3) , ; * %'6Ž  * %'6jh/(Z: 
) h/7&3, ; @(* %'6 Ž  . : 698 h* %-6eh/(Z : 
9T (W LW W W
QT
W
F T -F B > AC
-F P FG P -F G)G1C
113
PRATICA (Lez. 5)
Y[Z
U
B
°N
bd_§%b#¥jds%\ eLo \eo lwb^`s$e
qK JLTF ƒ{ $K }I…$K R K { I { FH¯
‘
?s |b]]bq_ bn]^`\ ^·\ be e j dc$^`c]k%_o e ej
B &™
Y[Z\eo lwb#^`sHeLo c¬]°mb#§`o °o ^`cHb imb_\ bn¥
s%c °o ^`c%o
tEK { T¯´wEK { T ¯°}TM$H¯°}TM$ { F
‘
Autunno 2003
o bdcHbisH]j#ejn§%jis%ctm?_ ^?kHb#]]^vk%ªbgbd_rb#§`o ejxs%c
F JŸ
Y
™
U
]^?k?¤
be
E < 9T
M J P ? W
Y[Z\eo w
l b#^`sHeLo ct_o kHb #o ^`c$b imHbd_V\ bg¥s%c °o ^`c%o
°TI$¯#ETI ¯ET€ #¯#ET€ XEFHQ
J ¯#ET€ JL}G
‘
‘
o§ i
o \eo m^v§`o]^?k%¤be
Ypf
<
Y
š
110
PRATICA (Lez. 5)
#o ^`cHbgb#§·s•cH^v§`o
tW
o?¸ 6e bn§`o ]°m^`c•o ¸?o \ o°cHbq\?¸?s$¥¥bq_
B J•C¤
W
§`o]?mb§`o #o ^`c$bg§%b°hbgb#]]b_ bgjd\ l‹bdc$^·mjd_ojd\?\ ^
Autunno 2003
o mbd_lOb#ee ^`cH^|§`o?sH]jd_ bis•ceo lwb°^`sHeLmbd_V\ j
?
_o kHb °o ^`cHbnbi\ jg]?mb°§`o °o ^`cHbg]?s%\]^°k?¤
be
wjebd_lOjd_¤Qjd\ \ ^`_ j
Y[Zctlw^?§?^|]?o lgo \ bimbd_Vm^%e bd_]k%_o hb_ bxˆu]^?k%¤be
m?_ ^`cHe ^„o c¬]k?_o e es%_ jd
D•
š
]bq\ bkHeL_o e ^`_cHjv²]^?k%¤b#ewm?_ `
^ c$e^„o ct\ be es%_rjd³
D$TE ŸNDOT3¡%œŸDwK RH$TXI ƒ R { Q
¹ ’
‘
=•
%œŸ 6¡
#f
Œ wb
%Š
Œ wk%o?mbq_l‹bee ^`cH^|§`o
kHjdlg¸?o j_ bx\ jga _rjqc§?b n
j §%bqo?¸?s$¥¥ bd_
Ypf
›s$jqcH§?^„o ct_o kHb °o ^`cHbg]^`cH^„m?_ b#]bdcHeojq\ lwbdcH^·s•c
c%s%lObq_ ^|§`o?¸ ebxmjd_ojq\?\ ^
*P Y
111
Y
š
Y[Z\?¸?sH¥¥bd_Vo c¬]°mb°§`o #o ^`c$b isH]je ^ mbd_•¥ jd_]k%_o h6bd_o
§?j#eo§%jd\ \ jm?m?\ o kHj °o ^`cHbxm%_o lOjg§`o]%mb§`o _\ o
PRATICA (Lez. 5)
H^`cH^„\ bn]^?a`\ o bisH]je bg§?jd\ \ jg¥
s%c °o ^`cHbg]bd\ b#ke
tW
Autunno 2003
Y
¡ LT
Y[Z\?¸?sH¥¥bd_Ÿ§`o?_o kHb #o ^`cHb isH]j°e ^·mb_VlwjdcHebqc$bd_rbio
§?j#eo?cHbd\?¤
bd_c$bd\?m?_o l‹jg§`o?mj#]]j_\ ojd\ \ jqm?m?\ o kHj #o ^`c$b
ƒ TR R FwP ƒ …$…$R K €K SS$I { F
N#FHM ND$¯#R Fw}P$ISHK Fw$K }P$FHM$K …$K R TL¹
‘
ƒ K M$$KM$FHMw$F E~T…$…$TQT}}TE€K
M$TR R I`XK M$T} { EIQ$K ŸN#D
F TE XR F´
N#FHMO¡%œ•D$¯°T TM {ƒ IR KHPHI€€ ˜ T {~{ KHK MOF TE XR F´ #TM$G$FHM$F
‘
€IM$€TR R I { K
" 6(+'h,=* :%A8 %# 8 *=:, :h8@(/7: +1 : . .<% :+# , +.%/7, %% : %8 /7% *ed7% : 8 %';'%/7,=@=+ :
"" # +; :,6=+>h/(: /=c %*=*7: h/0% ; :%c /=%1.<8 +1%* 8%': ,* 6 +-(, :++ +>: (8 +-%,e: /=:-6e8 %+*78 /7:+-@(f9::h/7/7h% /3*=% d=% + :,e:
6eh/(/7%*=*7: h/0%
" (k +'h,=*:%18 %/7*=h: /i@(8:0 :;, h%8/7/7&g+1+/7*7h@(2(*=:, d7h % .<:,e+: +*7d7%-, + : /7&g%8 * %-6=h / : + e: /76 1:
" 3: :. /0&(d7%h88 , *+%-/76e, h%)/ 6jh:-/7: ,=(8 h+,=+:0;8 %1%/7: &g;-h+ /0h3h86e% +/06j:-% 8 : , +-h,e8 :/7hd7%8 6 hd7h
‘
#N
PQa`c%o]^#k%¤b#eLª$jis%ct¸?sH¥¥bd_§`o?_o kHb
P T€K XK €IQK RX ƒ MS$K FHM$IJQTM { FL$TR R IL€R FH}TQP$TE•PHEF { F€FHR R K
$
FHEK TM { I { KHIR R IQ€FHM$M$T}}K FM$TL† ŸND$‡
‘
B &™ J B B – J
9T
E • tW
š
Autunno 2003
Y[Z
‘
LW šdPSHK FHM$K$KH™ D
‘
Autunno 2003
114
PRATICA (Lez. 5)
dZŠf
T ¹Q}T {u{ I { I3P$TE ƒ MwEI´ }FH€WT { $K™ DLIR R FHEI
L
R IP$P$R K €IS$K FM$TL$T TQ€FH} { E ƒ K ET3IM$€ ˜ TQR ™D ˜ TI$TE•€ ˜ T
M$FHEJQIR JQTM { T3¹L}€EK {u{ Fw$IRWTEM$TR
W
AZ ¶Š
D$TEJLT ~{ { TQ$KK M$}TEK ETLR TLFPSHK FHM$KH$TR R ™ D
Y[Z
‘
ƒ K M$$KHR IX ƒ MS$K FM$TL}K…$R FH€€I
t•
• W
˜ TI$TE
D$TEJLT {~{ TQ$K}P$T€K XK €IETQK R ŸK JLT ŸF –K TL† K M
$I { IG$E~IJ
™ RH$TXI ƒ R { ¹
˜ FP$}‡q$TR
t•
šdPSHK FHM$K$K qN6D
" • lu5 % h !8 .<v %'eu, e %Ul _<Z# :c .< hte ]e:6j+8 % ) % <h8 %˜Z :7+', %* +>@(* +', %˜Z(+
• ™•
"}
’ “t[ 8 : ,=8 +*=.%', =:d7%8 * %. d(8 %
"}
" 6eŒD”th[ ‚ /(@(/7* ”t%+1[ *=:8*7) ::,=Z(h8 %+/0]% *7+.<@ %),3, jZ :-:-d7*=%: *8, %Œ1.<* %-+ 6jh/(Z :-d(8 : .<+UZ :0+2(20+/(Zh/7+8 % )+
" • l %8 .< %', B%UZ:.<htZ :]=:6j+8 %>: ) ;'+ ) h8 %UZ% ) ) - w5(5Z(% ) )+6eh/(/=%*=*=:h/7%
" • 6ey & +:+–* Uh 2: Z) : , :7+ 6eh) -+ /7 &()&(¤%h* 8 ,e:,e:h. /7h % Z : d +'& )%6 l=˜% Z : .: /(@: * 6j%A: Z(+',=:(*=d7% Z :,e:: /
ž$}TJLP$K Fw$KH}T { TLGHT { }FH€WFHP {
Autunno 2003
115
Autunno 2003
PRATICA (Lez. 5)
ŸN#D
‘
6ž$ž$D
–™
ž
ŸN#D ?U
‘
ŸN#D ?U
‘
‘
ŸN#D
116
PRATICA (Lez. 5)
#include
"basic.h"
#include
<netinet/tcp.h>
int main(int argc, char **argv) {
int
sockfd, mss, sendbuff, ttl;
socklen_t
optlen;
sockopts-set.c
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
err_sys("socket error");
optlen = sizeof(ttl);
if( getsockopt(sockfd, IPPROTO_IP, IP_TTL, &ttl, &optlen) == -1 )
err_ret("getsockopt error");
printf("getsockopt: TTL = %d\n", ttl);
ž$¢
ttl = ttl + 16;
if( setsockopt(sockfd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) == -1 )
err_ret("getsockopt error");
printf("TTL modificato\n", ttl);
if( getsockopt(sockfd, IPPROTO_IP, IP_TTL, &ttl, &optlen) == -1 )
err_ret("getsockopt error");
printf("getsockopt: TTL = %d\n", ttl);
exit(0);
%šdœŸž$–
}
Scarica

bwt 1 lib