Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Annexe 4 : Code source pingserveurTCP.

1 #i n c l u d e <s t d i o . h>
2 #i n c l u d e < s t d l i b . h>
3 #i n c l u d e <u n i s t d . h>
4 #i n c l u d e < s t r i n g s . h>
5 #i n c l u d e <s t r i n g . h>
6 #i n c l u d e <s y s / t y p e s . h>
7 #i n c l u d e <s y s / s o c k e t . h>
8 #i n c l u d e <n e t i n e t / i n . h>
9 #i n c l u d e <netdb . h>
10
11 i n t main ( argc , argv )
12 int argc ;
13 c h a r ∗ argv [ ] ;
14 {
15 i n t sd ;
16 s t r u c t sockaddr_in s a ; /∗ S t r u c t u r e I n t e r n e t sockaddr_in ∗/
17 s t r u c t hostent ∗ hptr ; /∗ I n f o s s u r l e s e r v e u r ∗/
18 int port ; /∗ Numero de p o r t du s e r v e u r ∗/
19

20 i n t newsd ; /∗ Id de l a s o c k e t e n t r a n t e ∗/
21 s t r u c t sockaddr_in newsa ; /∗ sockaddr_in de l a c o n n e c t i o n e n t r a n t e ∗/
22 i n t newsalength ;
23
24 int i ;
25

26
27 /∗ V e r i f i c a t i o n du nombre d ’ arguments de l a l i g n e de commande ∗/
28 i f ( a r g c != 2 ) {
29 p r i n t f ( " pingserveurTCP . E r r e u r d ’ arguments \n" ) ;
30 p r i n t f ( " Syntaxe : %% pingserveurTCP numero_port \n" ) ;
31 exit (1) ;
32 }
33
34 /∗ R e c u p e r a t i o n numero p o r t p a s s e en argument ∗/
35 p o r t = a t o i ( argv [ 1 ] ) ;
36
37 /∗ I n i t i a l i s a t i o n l a s t r u c t u r e s o c k a d d r s a ∗/
38 /∗ F a m i l l e d ’ a d r e s s e : AF_INET = PF_INET ∗/
39 s a . s i n _ f a m i l y = AF_INET ;
40
41 /∗ I n i t i a l i s a t i o n du numero du p o r t ∗/
42 sa . sin_port = htons ( port ) ;
43 s a . sin_addr . s_addr = INADDR_ANY;
44
45 /∗ C r e a t i o n de l a s o c k e t TCP ∗/
46 i f ( ( sd = s o c k e t (AF_INET, SOCK_STREAM, 0 ) ) < 0 ) { // AF_INET −> IPv4 | |
SOCK_STREAM −> TCP e t SOCK_DGRAM −> UDP

43
47 p r i n t f ( " Probleme l o r s de l a c r e a t i o n de s o c k e t \n" ) ;
48 exit (1) ;
49 }
50
51 /∗ Bind s u r l a s o c k e t ∗/
52 i f ( bind ( sd , ( s t r u c t s o c k a d d r ∗ ) &sa , s i z e o f ( s a ) ) == −1) {
53 p r i n t f ( " Probleme avec l e bind \n" ) ;
54 exit (1) ;
55 }
56 /∗ I n i t i a l i s a t i o n de l a queue d ’ e c o u t e d e s r e q u e t e s ( 5 max) ∗/
57 l i s t e n ( sd , 5 ) ;
58

59 p r i n t f ( " S e r v e u r en e c o u t e . . . \ n" ) ;
60
61
62
63 i = 0;
64

65 while (1) {
66 /∗ n e w s a l e n g t h c o n t i e n t l a t a i l l e t h e o r i q u e de l a s t r u c t u r e newsa ∗/
67 n e w s a l e n g t h = s i z e o f ( newsa ) ;
68 i f ( ( newsd = a c c e p t ( sd , ( s t r u c t s o c k a d d r ∗ ) &newsa , &n e w s a l e n g t h ) ) < 0
) {
69 p r i n t f ( " E r r e u r s u r a c c e p t \n" ) ;
70 exit (1) ;
71 }
72
73 /∗ Compteur nombre c o n n e x i o n s ∗/
74 i ++;
75 p r i n t f ( " Connection No %d s u r l e p o r t %d . . . \ n" , i , n t o h s ( newsa . s i n _ p o r t )
);
76
77 char b u f f e r [ 1 0 0 ] ;
78 int longueur ;
79
80 // Boucle pour r e c e v o i r l e s d o n n e s e t l e s r e n v o y e r
81 w h i l e ( strncmp ( b u f f e r , "QUIT" , 4 ) != 0 ) {
82 l o n g u e u r = r e a d ( newsd , b u f f e r , s i z e o f ( b u f f e r ) ) ;
83 write (1 , buffer , longueur ) ;
84 }
85
86

87 c l o s e ( newsd ) ;
88 }
89
90 /∗ Fermeture du s e r v e u r . Never r e a c h e d ∗/
91 c l o s e ( sd ) ;
92

93 p r i n t f ( " Fin du s e r v e u r . Bye . . . \ n" ) ;


94 exit (0) ;
95 }

re223/pingserveurTCP.c

44

You might also like