Bài giảng Lập trình mạng - Chương 2: Lập trình Socket

pdf 13 trang hoanguyen 4000
Bạn đang xem tài liệu "Bài giảng Lập trình mạng - Chương 2: Lập trình Socket", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfbai_giang_lap_trinh_mang_chuong_2_lap_trinh_socket.pdf

Nội dung text: Bài giảng Lập trình mạng - Chương 2: Lập trình Socket

  1.     !"  # $%&!$'(  # $%)(01    345"(6#'(78'0'0  2      !"#$#%#&' ()01  !" #$%% (!2$3&'4 & 555555555555555 '"! $'($)0123 4$ $'%$$56783 #(6!7869 93 &!9  !" #$%%'0@ #(6(!9!7086!#7@AB#7@CB#7@DB#7@E4F!#77@4 & #(6(!97#0&6(!#7HAB#7#C4F377H4 $'($)012' 0@'($)0123 7&I!37864 0@'B"' 0@'B"3 F!374  !0@'$%% 0@'$%%3 4$ 0@'CD"5E83 F4 93 G A
  2.       !"#  !"!# $ $  %& '#%(") %&' (#&)!0  %&& '%01%'#%(") %&'' (&23&!0  ! '%3345") %4 (540  ! '0"#6 ) %& (5440  %&& '%33'01) 6 7 2 1      !   !"#$%&'(& "  )012345678679 #$% &!$' ( @A48)BC DEB#! #$%% &$01$ (  0! FEGBH # &( PQRST QTRUPVPQRSTWX 2 YRU` QTRUYVYRU`WX PQRSTa UTRQPVPQRSTWX YRU` UTRQYVYRU`WX ) I
  3.        !"#$%&'()01213145#     !"#56789@    !"# ABCD ECFGHIPPQRSTIQUVW  $%#&'(#)     !"#56789@5#  !"#$%&'()0121314 STIQU ECFGHCGBIRABCDVW X 01         !"#$%&'"()$"#$%0 ( ! "#$ 1#$010 ('0 (1&23 % &"'( ")012345678 4 5678 % 9 @0 9  @ 6A7  BCD EC ! ABCD5AE83FG CH 9 H 7H7II@ PQR  7IISTUVCW ! ABCD5PQE3FG RPH 6A7 BHX % 9( ")STU(VW "XY( `a(b 9 7IIH@YI` a7UVCH 7H7II ! Q cdeVf 4 bcdeSf % "1(g G( cd# h0e 9 gQ hHi@pqrs5Wsttqt % ig G67p3i6P5ABCD8 II FG
  4.      !" #$! %"&$'( $! %)00'  !"#$ "%&'#(  0"$)00'&)00'1"23 )!#0123 4 5678 4 56 9 8@ABCD8@AE@FG@ 7 689@AB68C 9 PA8DPPBQ R8@DPPSTUVWXY``aSE 8bc@CDdeSEc@   9 DPPfABgPEhDPA8DPP 7 FG89HIPB C 4 ipqSr 4 QARASTU 9 s tfuBvwxy5by€€w€ 7 VWXGY9`abcdedffaf HI DE      !"#$%""&'()%0 !"#12345    26578993@AB!)%CCB1@%CCBDEF !"#$%&'()!"#$%012&34$56789$@ABB&C4!"#$ G HIPQ D&'E3F4!"#$BDGE%HI R STQUVWQUVX TPVY   PQ  R `aUSTbbaWcTVdVeTSfgPhgipVqp  !"#$%&'(30)!"#$%012&3456789$@ABB&C4!"#$  D&'E3F4!"#$BDGE%4$T#8U5#$TV5W7XX8$ R TbbatXWubvVeT`aUSTbba (&%G((C4$!"#$G((CD&'HI G wf ac axy€  Y`a`bcde R IPt‚WQUVX Sƒ„ Ua Sƒ f ghigpiqerstiquvgwwxst R …t‚W†‡ˆ‰†‘’“”•H q f gyu€eqY`aR rs RS
  5.       !"#   ! $ %&&'( )01%2)345 "#$%&'()0"#$%12(3'456789@%ABCC'&DE"#$%F'GH4I5% "#$%CFPH1QR 3!"  ST! $ !"(67!"3!" "#$%&'()C&2U0"#$%12(3'456789@%ABCC'&DE"#$%12(3'4 ABCC'&DE $ &"(@AB@)0!'C!"@" F'GH4I5%"#$%CFPH15X$9Y6$%X`6a8bb9%@C&2UPcc&5%"#$% @Pcc&F'GQR $ '()01DEFGH'I4 @'P!  defgfhipq !Q! r stuqvwxuy€  $ '!()RS4' ‚wx r ƒy„quefg…V 89 VW       !"   !"#$ # $%&&'()01234506728  %&'()0'123'&45067148(9&450@%&'(9&450A0&B 6 C D EFGHIPQRSSISTU#VW # 9'@ABC)DE! FGHI  XY`"#$  %&'(30'123'&45067148(9&450@%&'(&450A0&B # &9RAS)TUVT01WX!T C D EFGHIPQRSSISTU#VW  WG"#$ # &(Y`Raa()0123bcdefWg72hTWi %&'()0'c254%&&45067148(9&450@%&'(&450A0&B  C D EFGHIPQRSSISTU#VW # Raa(9'@)0pF72qWr2W PQ ab
  6.       !"#$   !"#"$%&'"() %&'()0'1234%&&43056748(9&430@%&'( 012345267897@2A0123678952B30123C5D5CB3 &430A0&B 01237@21EF5B38GEH3I7@2DECB30123 I7@2C51P C D EF!GHIPQRRHRSTU#V f Q RSTUVWXY``V`$abcd  XUY`a bcYdcaeSf  g $ %&!"#"$%&'"() %&'(g0'h008&4305%&'()26i0'@)'8p6'( 012365267897@2A0123678952B30123C5D5CB3 )26i4118(9&430@%&'9(&430A0&B 01237@21EF5B38GEH3I7@2DECB30123 C D EF!GHIPQRRHRSTU#V I7@2C51P f Q RSTUVWXY``V`$abcd W ee       !"#   !"#$%"&'( ) $%&'(%)01$%23%)452%01$%673892@(08&) 0123415062768159@610627AB8CD6E4FC25 43892AB 98CD6G54FC259H2I1IPQ C D EFGHIPQRS! R S T"#UVWX'Y!   !"VWXY   !"#$%"&'( b $%&'(%)01$%23%)452%01$%678``&@(08&) 0123415062768159@610627ABHI21Ec815 48``&ab3%)c23a)b3%)%7d2AB HI21G54FC259H2I1IPQ C D EFGHIPQRS! R S T"#UVWX'Y! TU `a
  7.      !"# !$%&'( )   !"#$% 0123415627178915@A8162717BC9DE8F4GD25 & '()01!21!$)01! @9DE8HI 3141!5$678(1918@1)1 P Q R"#STUV'W!  #A8#718B8#$   !"# !$%&'( `a  E#A8#714F1!#GA!"#$ 0123415627178915@A8162717BC93EB82Fb915 93EB82HI P Q R"#STUV'W! XY CD         !"#$% 4#56#5 !"#$%& 7$89$8 &'()0123 &'()0123 '()01234 '()01234 789@23 EA5634 6F2F @1)1AB1C@(D34 @H1H '1562(34 &09@1'23 C0(08D0EC'F23 )I('134 '()01234 @H1H 6F2F C0(08D0EC'F23 &09@1'23 @1)1AB1C@(D34 QRST '1562(34 UVWXRYY '1562(34P@1)1AB1C@(D34 (G'&023 (G'&023 6F2F '1562(34P@1)1AB1C@(D34 )I('134 )I('134 AB GH
  8.         !"#$% 4#56#5 !"#$%& 5$67$6 &'()0123 &'()0123 '()01234 '()01234 789@23 89@A34 A8&10923 B9'21@34 ('990(18'9IE0QR0&1 )(@@1)29(@PF1QR1'2 ('990(123 G((0H123 90SI&'()01 )(@@1)234 H))1I234 )B('134P @G1G SdefTW &09@23DE0(08F023 &09@23DE0(08F023 TUVWXY`aUb XY`aUbc AH2H '1@A34EF1)19G134 '1@A34EF1)19G134 ghiP (A'&023 (A'&023I2PG89I&'()013 pqrshtt (A'&023I290SI&'()013 )B('134 )B('134STUVWXY`aUbc BC CD     !"#$$%&$% '()$ '($%'$(01!2 '3 '4567  5)$"1%('3!8$% "2$022'2%43!$'(  ! "#$%&' ( 9 '@#30$A(0 1B)C )0 123!45)0 123!600! 7748 9 '@#30$AD(D"$B)C 9 '@#30$AD1@E$(B)C "9@224%ABC&8 9 '@#30$A'$( '$( 'B)C )@ )0 133D)4$D33! 7D338 8 &1 0$%%1%G@)2%!4H I 2 FA'( "$%%1%G!4HP 262)34!GHIIPI!06060$343QG'8 $Q (GRHP S 4RS'8 T U F E
  9.      !"#$%&'()0123$0(4'"5)678  !"#$% # !&$'( 9!@67 )0$12&$"300$12&$((45( AAA!B'4414CD9DE B78 0$$$678898@6(A FGA!!HIAP7QAR$IA) B#0 !"C(A 9G!AR$IA778 BB0E30 B2&$(A CA DI 9!IAERSTU78 0F !E& 0G# !"#$% #  !&$'() B2&$") BB0(A AR$IAV9D$IW9XY"#$%&'(8 0F !45( AR$IAV9D$IAV$IA%&"``4$"&a8 0$$$678898& 0G#6(A AR$IAV9D$CA H D!CA D78    D      !"#$%&'%"&()"()0&"()&1(&)23#4"(5!"(6  !"#$ 789 %&'&()%0123) !!#$ 70&$31)(5#@&1"!$4!!&24!4(4165#&() 4&%&5&6#& 7899@9&()4%A&B&123C7#$ A"('%52&B!)2"3C0D A"('%52&B!E!)E$&!C0D A"('%52&B!E!!3'F&)C0D E4%C7F&41&CG&B11(AHI1P%7#$ A"('%52&B(&)"(&)"(C0D %&'&04C%0123)7Q&AC&R& B11(A7ST#$ I3"2&1131P'041#!6Q 4&%&5&6#& R 7899@9&04C4%A&C&123C7#$ $&1131P#!6QS 2U1123)#$2U1%0123)#$ &T")PUQS C%&6$& V V D GH
  10.        !"#"$ % !&'()0  !" #$%&'() 12"345#62 2#7 012340125"6789@ABCDEFG8DBHA6ICP() 1 8"12"355 91@ &955 7 !3"01234QP( 1 8"$21@%1@ &@ 7 5"RAHHEH5!#0125R() "$ A844@ 'BCD(7 05$5#5T0UV W5" #$%X'() 4 !"GH)0 !3"05$5@Y``(a 46 415@ #I81!@P1$21@62 QI#  !&'R()7 3!3"045CRAHHEHC0b1TT0cR() 5d!"P() @SR)7 e T   EF S        !"#$"%&'(!))0"&12&'(!))$$3"  !"#$%#&##&'#&(#%%$)#0& 12& 34#536#789@12& &'(!))4&15(2!6178"9"@A(BCDE3" )#%36#78997%A12 F8 !"#$&'GH (!))0" !" #$%&'(!))4&15(!))4&(!))0"&'GH (75PQ $3" &C&D#%5EFA736#7%"#36#112& &'(!))4&15(FQ"9" Q5&FQ5$3" &&G&H1& #5 IPPQP&D)&5&%5EF# 12 12"55Q&R2)0%&'(!))0&12&'(!))$$"S" R T$" UDVVWV"55Q15PU$3"  I B
  11.       !" # ! $ %"&' ()01!  !"#$%&' 23%45"0 ""# " 2 $3%2!' ()5" "&32"61& (')'01234 1! !"##%& 752893#@"A"!A0"')B 5 '('6'7%' 752893#@"A""18C!')B @ABBCB'015(D' E'234F@%& 752893#@2!52!52')B 752893#@2!#4')B G5(F @H2I(@! %&' 752893#@"!#51')B 3P2234 1%& F('7& Q' 89 DE        ! "#$%$& "' "#()01  !"#$%&'()012345%678#901@AB 2 CD1"1E1@A1 FFF"G3H ICIP1 !GAB 34$5%67#&%8"467%9 3"@$34$5 AA"B37"(7"9 Q IP!R11CS D" FT FAB 3"@$34$5 AA"B8"49 US F"V FT F0Q IP!RAB $& "E@8)FGHI09  FT FXCI%DY`CQa#$%&'()B  FT FXCI%YbbFX%YbbF&'#668%#'cB 8! "#$PH12  FT FXCI%HF!R!I"Y!C"YFPTdefAAB 8Q"8!3A7""%RSTTUT%4Q4"Q"4(A7AVR19 CD1"UCIb"0"!Fg!1YbbF1 7W!G19 hAV FT F0Q IP!RAE@A1 X FFF"GUCIbCIPGAB   CD WW
  12.       !"#$%&'$&'(%$&'%)&%'0123$&  !"#"$%& '$()0("$012334%!56 4$&"#50%6787'89%7@(A 78% "'95"@ !"#" 0A'$01BC)B9DEFBDB'$()0(" 4$&"#50%678771"B%'@(A $01233"G5HBH !56 4$&"#50%6&%'$&%'$&@(A %"'!"I"D5" 'P 0AP56 7%( '9BPR 0 %A 3"2"32(2S2T"PBE956 4$&"#50%63)93$&%'@(A 7%( '9BC)B!56 4$&"#50%6&%'0E@(A !"#"$ !3('$01BPU("V)" $$2S W!PB9XB 4$&"#50%67'0$1@(A DB'$()0("$01233" G5HB !56 %"'! I"D5" 'P$ !3(P56 Y Y Q CD        !"# "$%&' &("$ )"$%0123 4  !" #$%&'()0123"4567(89@&A3"BCD  56&7' 89%(' @ EF"% "G"BC"#HHH%I #$ICD 5$A& 56&7"BB$C 59$09$' D$6!@ 5$A& (659 )(E@ &("$ HADD9$1IPQ2@ #HR#HSET(FUVEWX"!"&'()012D Y`"!"a#$Y$bXTUV#%UHaRcdeCD D #"$%& RS T3 4 E$D#UV5"%9W 59$09$ E6$XU3@ 9Y#`3@ EF"%Y`!!BC"#HHH%IfT TgT"Y$ICD a   FG PQ
  13.        !""#$%&'(# )0#1$22%3&  !"#$%&'()'0' !'0!1'02'))(3'40$56 "#$%&'(456%76%859@1$22%3 78'9#7@""'ABCD56 # )0#1A6@BC#(D "3'!)#7@""'ABCCA)!E 56 F)' E!9#)9GHA7@""'A )!&' #7@""'5AQAR)'S'A&' 3! 56%76%859@1 %C&G&#C@5"$C9"$%B7HIP((D 156 "0# 0T0Q50'9#$U' E!9$56 A6@BC#G59Q6R"5C%SC&5T$22%19@(D   EF IP     !"#$ %&'(%")012$$! 03450607$ %&0 78!9@ABC D$'E6B! % 'F !"#$ %&FBC H D@!'I0FP%@QQ")RF0I3BC H D@!'I012$$! 0BC S G