lap trinh mang

  • View
    214

  • Download
    0

Embed Size (px)

DESCRIPTION

cho nghang ky thuat may tinh

Transcript

  • Bi ging

    LP TRNH MNG

    Ngi son: Lng nh Hong

    B mn K thut my tnh

    Vin Cng ngh thng tin v Truyn thng, HBK H ni

    H ni, 8/2010

  • 2

    Mc lc bi ging

    Chng 1. Gii thiu cc m hnh lp trnh mng ............................................................. 4

    1.1 Tng quan v lp trnh mng .......................................................................................... 4

    1.1.1 Khi nim ........................................................................................................................ 4

    1.1.2 Ngn ng lp trnh ..................................................................................................... 4

    1.1.3 Th vin h tr ............................................................................................................ 5

    1.2 Giao thc Internet ............................................................................................................... 5

    Chng 2. B giao thc Internet (TCP/IP) ........................................................................... 6

    2.1 Gii thiu ................................................................................................................................. 6

    2.2 IPv4 ............................................................................................................................................ 7

    2.2.1 a ch IPv4 .................................................................................................................... 8

    2.2.2 Cc lp a ch ............................................................................................................... 8

    2.2.3 Mt n mng ................................................................................................................. 9

    2.2.4 Cc di a ch c bit ........................................................................................... 10

    2.3 IPv6 ......................................................................................................................................... 10

    2.4 TCP .......................................................................................................................................... 11

    2.5 UDP .......................................................................................................................................... 11

    2.6 H thng phn gii tn min ........................................................................................ 12

    Chng 3. Winsock ....................................................................................................................... 14

    3.1 Kin trc................................................................................................................................ 14

    3.2 c tnh ................................................................................................................................. 15

    3.2.1 Giao thc hng thng ip ............................................................................... 15

    3.2.2 Giao thc hng dng ........................................................................................... 16

    3.2.3.Giao thc gi dng ................................................................................................... 16

    3.2.4 Giao thc hng kt ni v khng kt ni .................................................... 16

    3.2.5 Tnh tin cy v{ ng trt t ................................................................................. 17

    3.2.6 Qu| trnh ng kt ni ........................................................................................... 17

    3.2.7 Qung b d liu ....................................................................................................... 17

    3.2.8 Multicast ....................................................................................................................... 18

    3.2.9 Cht lng dch v (QoS) ..................................................................................... 18

    3.3 Lp trnh Winsock ............................................................................................................ 19

  • 3

    3.3.1 Mi trng .................................................................................................................. 19

    3.3.2 Khi to Winsock ..................................................................................................... 19

    3.3.3 X|c nh a ch ......................................................................................................... 22

    3.3.4 To socket .................................................................................................................... 26

    3.3.5 Truyn d liu s dng giao thc (TCP) ....................................................... 26

    3.3.6 Truyn d liu s dng giao thc UDP .......................................................... 40

    3.3.7 Mt vi hm khc ..................................................................................................... 43

    3.4 C|c phng ph|p v{o ra ................................................................................................ 44

    3.4.1 Cc ch hot ng ca Winsock .................................................................. 44

    3.4.2 Cc m hnh vo ra .................................................................................................. 48

    Chng 4. MFC Socket ................................................................................................................ 66

    4.1 Gii thiu .............................................................................................................................. 66

    4.2 CSocket .................................................................................................................................. 66

    4.2.1 Khi to CSocket ...................................................................................................... 66

    4.2.2 Kt ni n my khc ............................................................................................. 67

    4.2.3 Chp nhn kt ni t my khc ......................................................................... 67

    4.2.4 Gi d liu ................................................................................................................... 68

    4.2.5 Nhn d liu ............................................................................................................... 68

    4.2.6 ng kt ni................................................................................................................ 69

    4.2.7 Xy dng Client bng CSocket ............................................................................ 69

    4.2.8 Xy dng Server bng CSocket .......................................................................... 69

    4.3 CAsyncSocket ...................................................................................................................... 70

    4.3.1 Khi to i tng CAsyncSocket .................................................................... 70

    4.3.2 X l cc s kin ....................................................................................................... 71

    Chng 5. NET Socket ................................................................................................................. 74

    5.1. Gii thiu v NameSpace System.Net v System.Net.Sockets ..................... 74

    5.2. Chng trnh cho pha my ch s dng giao thc TCP ........................ 76

    5.3. Chng trnh cho pha my khch s dng giao thc TCP ................... 78

    5.4 Chng trnh pha m|y ch s dng UDP ............................................................. 79

    5.5 Chng trnh cho m|y kh|ch s dng UDP .......................................................... 80

  • 4

    CChhnngg 11.. GGiiii tthhiiuu cccc mm hhnnhh llpp ttrrnnhh mmnngg

    Bi ging s 1

    Thi lng: 3 tit.

    Tm tt ni dung :

    nh ngha lp trnh mng.

    ng dng ca lp trnh mng.

    Cc ngn ng lp trnh.

    Cc th vin v mi trng h tr lp trnh mng

    Giao thc IP.

    1.1 Tng quan v lp trnh mng

    1.1.1 Khi nim

    Lp trnh mng l cc k thut lp trnh nhm xy dng nhng ng dng, phn

    mm khai thc hiu qu ti nguyn mng my tnh.

    Mng m|y tnh ang ng{y mt pht trin, ng dng ca mng em li l khng

    th ph nhn. Gio trnh ny s cp n mt v{i phng ph|p x}y dng cc

    ng dng tn dng c h tng mng sn c.

    1.1.2 Ngn ng lp trnh

    Hu ht cc ngn ng lp trnh u c th s dng lp trnh mng, tuy nhin

    vic lp trnh mng cn ph thuc v{o c|c th vin v{ mi trng lp trnh c

    h tr hay khng. C th lit k cc ngn ng lp trnh c th s dng lp

    trnh mng nh sau:

    C/C++: Ngn ng lp trnh rt mnh v ph bin, dng vit mi loi

    ng dng trong c ng dng mng.

    Java: Ngn ng lp trnh kh thng dng v h tr trn nhiu mi

    trng, trong c th vit ng dng chy trn in thoi di ng.

    C#: Ngn ng lp trnh cng rt mnh v d s dng, ch h tr trn h

    h iu hnh Windows ca Microsoft.

    Python, Perl, Php: C|c ngn ng thng dch, s dng vit nhanh cc

    tin ch nh mt c|ch nhanh chng, trong c th s dng vit ng

    dng mng.

    Hc phn ny s trnh b{y phng ph|p lp trnh mng da trn hai ngn ng:

    C/C++ v C#.

  • 5

    1.1.3 Th vin h tr

    Vic lp trnh mng ph thuc rt nhiu v{o c|c th vin h tr n t h

    thng. Ty thuc vo nn tng pht trin ng dng m c th s dng c|c th

    vin khc nhau. C th lit k mt v{i th vin h tr lp trnh mng nh sau:

    Winsock: Th vin lin kt ng ca Microsoft, c phn phi cng h

    iu hnh Windows. Winsock cung cp kh nhiu API pht trin ng

    dng mng. Winsock c th s dng cng bt k ngn ng lp trnh no,

    nhng b i C/C++ v{ Winsock em li