L10(2017-12-05 Tue) : Time-stamp: 2017-12-17 Sun 11:59 JST hig. recv/send http://hig3.net ( ) L10 (2017) 1 / 21
IP I swallow.math.ryukoku.ac.jp:13 = 133.83.83.6:13 = : IP ( = ) (well-known ports), :. :,. socket, connect, recv/send, shutdown, close. ( ) L10 (2017) 2 / 21
9 IP 10 ( ) L10 (2017) 3 / 21
daytime :,, 2017-12-03 Sun 13:27. RFC Protocol ().. TCP, IP, HTTP P Protocol P. HTTP penguin count, (c), c Adelie Penguins!. Penguins!., : echo : http(s).,, Web API. ( ) L10 (2017) 4 / 21
( ) 1 #i n c l u d e <s t d i o. h> 2 #i n c l u d e <s t r i n g. h> / memset / 3 #i n c l u d e <s y s / s o c k e t. h> / s o c k e t, c o n n e ct / 4 #i n c l u d e <arpa / i n e t. h> / htons / 5 #i n c l u d e <n e t i n e t / i n. h> / htons, i n e t a d d r / 6 #i n c l u d e <u n i s t d. h> / c l o s e s i z e o f / 7 #i n c l u d e < s t d l i b. h> 8 9 #d e f i n e SERVER ADDR 1 2 7. 0. 0. 1 10 #d e f i n e SERVER PORT 22222 11 #d e f i n e BUFFERSIZE 1024 12 13 i n t main ( ) { 14 15 i n t s ; 16 s t r u c t s o c k a d d r i n sa ; 17 i n t count ; 18 char r e c v b u f [ BUFFERSIZE ] ; ( ) L10 (2017) 5 / 21
19 char s e n d b u f []= count something. ; 20 i n t s t a t u s ; 21 22 / / 23 s = s o c k e t ( AF INET, SOCK STREAM, 0 ) ; 24 i f ( s== 1 ){ 25 p e r r o r ( s o c k e t ) ; 26 e x i t ( 1 ) ; 27 } 28 29 / : / 30 memset(&sa, 0, s i z e o f ( sa ) ) ; 31 sa. s i n f a m i l y=af INET ; 32 sa. s i n p o r t=htons (SERVER PORT ) ; 33 sa. s i n a d d r. s a d d r=i n e t a d d r (SERVER ADDR ) ; 34 35 / = open / 36 s t a t u s=c o n nect ( s, ( s t r u c t s o c k a d d r )& sa, s i z e o f ( sa ) ) ; 37 i f ( s t a t u s == 1){ 38 p e r r o r ( penguin connect ) ; 39 e x i t ( 1 ) ; ( ) L10 (2017) 6 / 21
40 } 41 42 / p r i n t f / 43 count=send ( s, sendbuf, s t r l e n ( s e n d b u f ), 0 ) ; 44 i f ( count== 1 ){ 45 p e r r o r ( penguin send ) ; 46 e x i t ( 1 ) ; 47 } 48 49 / s c a n f / 50 count=r e c v ( s, r e c v b u f, BUFFERSIZE 1, 0 ) ; 51 i f ( count== 1 ){ 52 p e r r o r ( penguin r e c v ) ; 53 e x i t ( 1 ) ; 54 } 55 56 / / 57 r e c v b u f [ count ]= \0 ; 58 p r i n t f ( %s, r e c v b u f ) ; 59 60 / c l o s e / ( ) L10 (2017) 7 / 21
61 62 s t a t u s=shutdown ( s, SHUT RDWR ) ; 63 i f ( s t a t u s == 1){ 64 p e r r o r ( penguin shutdown ) ; 65 e x i t ( 1 ) ; 66 } 67 68 s t a t u s=c l o s e ( s ) ; 69 i f ( s t a t u s == 1){ 70 p e r r o r ( penguin c l o s e ) ; 71 } 72 73 r e t u r n 0 ; 74 } ( ) L10 (2017) 8 / 21
9 IP 10 ( ) L10 (2017) 9 / 21
ex. syntax error ex. segmentation fault,. : return,,., 1. 0. stdlib.h void exit(int status),, OS. 0.. ( ) L10 (2017) 10 / 21
socket, connect, recv, send., OS II ( OS Linux ) / (Linux).,,, perror(3), man 3 perror, (, ) ( ) L10 (2017) 11 / 21
perror perror( );,, errno + + : + stderr. 1 #i n c l u d e <s t d i o. h> 2 void p e r r o r ( char s ) ; perror(3), (if-then),, #include <errno.h>, int errno, char *sterror(int errno). perror.,,. fprintf ( stderr, something ) ( II) ( ) L10 (2017) 12 / 21
s=s o c k e t (... ) ; i f ( s== 1){ / ; / } / s, s o c k e t ( 2 ) / / s o c k e t ( i f ( s o c k e t (...)== 1 ){ / ; /} / s o c k e t s / i f ( ( s=s o c k e t (... ) ) == 1 ){ / ; /} (a=f())==2., a=(f()==2), a 0 1 ( if ) C,, i f ( 1==do something ( ) ) d i e ( ) ;. OR. Get up or you will be late. ( ( 1==do something ( ) ) ( d i e ( ) ) ) ; exception( ), try-catch in Java (3 ) ( ) L10 (2017) 13 / 21
9 IP 10 ( ) L10 (2017) 14 / 21
,, 1 i f ( ()== ){ 2 p e r r o r ( ) ; 3 e x i t ( 1 ) ; 4 }. + ( ) 1 p e r r o r ( ) ; 2 e x i t ( 1 ) ;, void die(char message[]). die.h, die.c, Makefile. Refactoring ( ),,, ( ) L10 (2017) 15 / 21
9 IP 10 ( ) L10 (2017) 16 / 21
telnet telnet(1), nc(1) t e l n e t nc C host port host port,. telnet Control ], quit + Enter. nc Control C. t e l n e t www. a. math. ryukoku. ac. j p 80 GET /, Web URL http://www.a.math.ryukoku.ac.jp,. 80? m ( ) L10 (2017) 17 / 21
t e l n e t www. math. ryukoku. ac. j p 80 #a r g v [ 0 ] a r g v [ 1 ] a r g v [ 2 ] a r g c=3 www.mathr.ryukoku.ac.jp, 80 ( 1, 2).,, man., -c, --help -,. t a i l penguin c l i e n t. c t a i l 30 penguin c l i e n t. c t a i l r 30 penguin c l i e n t. c ( ) L10 (2017) 18 / 21
C main i n t main ( i n t argc, char a r g v [ ] ) {... } / i n t main ( i n t argc, c h a r a r g v ){... } / argc.. argv = 0 1 2 3 4 5 6 argv[0] t e l n e t \0 argv[1] w w w. m a t \0 argv[2] 8 0 \0 ( ) L10 (2017) 19 / 21
1 #i n c l u d e <s t d i o. h> 2 #i n c l u d e <s t r i n g. h> 3 #d e f i n e BUFSIZE 1000 4 5 i n t main ( i n t argc, char a r g v [ ] ) { 6 / i n t main ( i n t argc, c h a r a r g v ){ / 7 / a r g v, = / 8 9 char message [ BUFSIZE ] ; 10 char a n d s t r i n g []= and ; 11 12 s t r c p y ( message, a r g v [ 1 ] ) ; 13 s t r c a t ( message, a n d s t r i n g ) ; 14 s t r c a t ( message, a r g v [ 2 ] ) ; 15 16 / f o r t e s t / 17 / p r i n t f ( %c \n, a r g v [ 0 ] [ 0 ] ) ; / 18 / p r i n t f ( %c \n, a r g v [ 0 ] [ 1 ] ) ; / 19 / p r i n t f ( % s \n, a r g v [ 0 ] ) ; / 20 21 p r i n t f ( %s \n, message ) ; 22 23 r e t u r n 0 ; 24 } ( ) L10 (2017) 20 / 21
3.5 4.5(1-502), 4(1-502) 2017-11-28 12 12. L11.,,, Learn Math Moodle,. ( ) L10 (2017) 21 / 21