289 82 494KB
Russian Pages 18 Year 2002
Ƚɨɫɭɞɚɪɫɬɜɟɧɧɵɣ ɤɨɦɢɬɟɬ Ɋɨɫɫɢɣɫɤɨɣ Ɏɟɞɟɪɚɰɢɢ ɩɨ ɜɵɫɲɟɦɭ ɨɛɪɚɡɨɜɚɧɢɸ Ⱦɨɧɫɤɨɣ Ƚɨɫɭɞɚɪɫɬɜɟɧɧɵɣ Ɍɟɯɧɢɱɟɫɤɢɣ ɍɧɢɜɟɪɫɢɬɟɬ
Ʉɚɮɟɞɪɚ "ɉɪɨɝɪɚɦɦɧɨɟ Ɉɛɟɫɩɟɱɟɧɢɟ ȼɵɱɢɫɥɢɬɟɥɶɧɨɣ Ɍɟɯɧɢɤɢ ɢ Ⱥɜɬɨɦɚɬɢɡɢɪɨɜɚɧɧɵɯ ɋɢɫɬɟɦ"
П
е POSIX
е
ɋɨɫɬɚɜɢɬɟɥɶ: ɋɭɤɢɹɡɨɜ ɋ.Ⱥ. Ɇɟɬɨɞɢɱɟɫɤɢɟ ɭɤɚɡɚɧɢɹ ɩɪɟɞɧɚɡɧɚɱɟɧɵ ɞɥɹ ɩɪɨɜɟɞɟɧɢɹ ɥɚɛɨɪɚɬɨɪɧɵɯ ɡɚɧɹɬɢɣ ɫɨ ɫɬɭɞɟɧɬɚɦɢ ɫɩɟɰɢɚɥɶɧɨɫɬɢ 22.04 " ɉɪɨɝɪɚɦɦɧɨɟ Ɉɛɟɫɩɟɱɟɧɢɟ ȼɵɱɢɫɥɢɬɟɥɶɧɨɣ Ɍɟɯɧɢɤɢ ɢ Ⱥɜɬɨɦɚɬɢɡɢɪɨɜɚɧɧɵɯ ɋɢɫɬɟɦ" ɩɨ ɤɭɪɫɭ "ɉɪɢɧɰɢɩɵ ɩɨɫɬɪɨɟɧɢɹ ɢ ɭɫɥɭɝɢ ɫɟɬɢ Internet".
ɝ.Ɋɨɫɬɨɜ-ɧɚ-Ⱦɨɧɭ 2002 ɝ.
Б
е
Socket API
Ʉɨɦɩɶɸɬɟɪ, ɡɚ ɤɨɬɨɪɵɦ ɜɵ ɪɚɛɨɬɚɟɬɟ, ɧɚɜɟɪɧɹɤɚ ɩɨɞɤɥɸɱɟɧ ɤ ɤɚɤɨɣ-ɧɢɛɭɞɶ ɫɟɬɢ. ɗɬɨ ɦɨɠɟɬ ɛɵɬɶ ɤɪɭɩɧɚɹ ɤɨɪɩɨɪɚɬɢɜɧɚɹ ɫɟɬɶ ɫ ɜɵɯɨɞɨɦ ɜ Internet ɱɟɪɟɡ ɩɪɨɤɫɢ-ɫɟɪɜɟɪ ɢɥɢ ɞɨɦɚɲɧɹɹ ɦɢɤɪɨɫɟɬɶ, ɨɛɴɟɞɢɧɹɸɳɚɹ ɞɜɚ ɤɨɦɩɶɸɬɟɪɚ ɜ ɪɚɡɧɵɯ ɤɨɦɧɚɬɚɯ. ɋɟɬɹɦɢ ɫɨɟɞɢɧɹɸɬɫɹ ɪɚɛɨɱɢɟ ɫɬɚɧɰɢɢ, ɫɟɪɜɟɪɵ, ɩɪɢɧɬɟɪɵ, ɞɢɫɤɨɜɵɟ ɦɚɫɫɢɜɵ, ɮɚɤɫɵ, ɦɨɞɟɦɵ ɢ ɬ.ɞ. Ʉɚɠɞɨɟ ɫɟɬɟɜɨɟ ɫɨɟɞɢɧɟɧɢɟ ɢɫɩɨɥɶɡɭɟɬ ɤɚɤɢɟ-ɧɢɛɭɞɶ ɪɟɫɭɪɫɵ ɢɥɢ ɫɚɦɨ ɩɪɟɞɨɫɬɚɜɥɹɟɬ ɢɯ. ȼ ɧɟɤɨɬɨɪɵɯ ɫɨɟɞɢɧɟɧɢɹɯ ɞɥɹ ɩɨɥɭɱɟɧɢɹ ɢɧɮɨɪɦɚɰɢɢ ɧɟ ɬɪɟɛɭɟɬɫɹ ɞɜɭɫɬɨɪɨɧɧɟɟ ɜɡɚɢɦɨɞɟɣɫɬɜɢɟ. ɉɨɞɨɛɧɨ ɨɩɢɫɚɧɧɨɦɭ ɜɵɲɟ ɡɜɨɧɤɭ ɜ ɫɩɪɚɜɨɱɧɭɸ, ɫɟɬɟɜɨɣ ɤɥɢɟɧɬ ɜ ɩɪɨɫɬɟɣɲɟɦ ɫɥɭɱɚɟ ɩɪɨɫɬɨ ɩɨɞɤɥɸɱɚɟɬɫɹ ɤ ɫɟɪɜɟɪɭ ɢ ɩɪɢɧɢɦɚɟɬ ɨɬ ɧɟɝɨ ɞɚɧɧɵɟ. Ʉɚɤɢɟ ɪɟɫɭɪɫɵ, ɢɥɢ ɫɟɪɜɢɫɵ, ɦɨɝɭɬ ɩɪɟɞɨɫɬɚɜɥɹɬɶɫɹ ɫɟɪɜɟɪɨɦ? ɂɯ ɦɧɨɠɟɫɬɜɨ, ɧɨ ɜɫɟ ɨɧɢ ɞɟɥɹɬɫɹ ɧɚ ɱɟɬɵɪɟ ɤɚɬɟɝɨɪɢɢ: • ɨɛɳɢɟ — ɞɢɫɤɨɜɵɟ ɪɟɫɭɪɫɵ; • ɨɝɪɚɧɢɱɟɧɧɵɟ — ɩɪɢɧɬɟɪɵ, ɦɨɞɟɦɵ, ɞɢɫɤɨɜɵɟ ɦɚɫɫɢɜɵ; • ɫɨɜɦɟɫɬɧɨ ɢɫɩɨɥɶɡɭɟɦɵɟ — ɛɚɡɵ ɞɚɧɧɵɯ, ɩɪɨɝɪɚɦɦɧɵɟ ɩɪɨɟɤɬɵ, ɞɨɤɭɦɟɧɬɚɰɢɹ; • ɞɟɥɟɝɢɪɭɟɦɵɟ — ɭɞɚɥɟɧɧɵɟ ɩɪɨɝɪɚɦɦɵ, ɪɚɫɩɪɟɞɟɥɟɧɧɵɟ ɡɚɩɪɨɫɵ. ȼ ɷɬɨɦ ɦɟɬɨɞɢɱɟɫɤɨɦ ɩɨɫɨɛɢɢ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨ ɪɚɫɫɤɚɡɵɜɚɟɬɫɹ ɨ ɬɨɦ, ɤɚɤ ɧɚɩɢɫɚɬɶ ɩɪɨɫɬɟɣɲɟɟ ɤɥɢɟɧɬɫɤɨɟ ɩɪɢɥɨɠɟɧɢɟ, ɩɨɞɤɥɸɱɚɸɳɟɟɫɹ ɤ ɧɟɤɨɬɨɪɨɦɭ ɫɟɪɜɟɪɭ. Ɍɚɤ ɠɟ ɪɚɡɛɢɪɚɟɬɫɹ, ɱɬɨ ɫɨɛɨɣ ɩɪɟɞɫɬɚɜɥɹɟɬ ɩɪɨɰɟɫɫ ɧɚɩɢɫɚɧɢɹ ɫɟɬɟɜɵɯ ɩɪɨɝɪɚɦɦ. ɇɚɲ ɤɥɢɟɧɬ ɩɟɪɜɨɧɚɱɚɥɶɧɨ ɛɭɞɟɬ ɨɛɪɚɳɚɬɶɫɹ ɤ ɫɟɪɜɟɪɭ ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɨɩɪɟɞɟɥɢɬɶ ɩɪɚɜɢɥɶɧɨɟ ɜɪɟɦɹ (ɷɬɨ ɩɪɢɦɟɪ ɫɨɟɞɢɧɟɧɢɹ, ɜ ɤɨɬɨɪɨɦ ɨɬ ɤɥɢɟɧɬɚ ɧɟ ɬɪɟɛɭɟɬɫɹ ɩɟɪɟɞɚɜɚɬɶ ɞɚɧɧɵɟ ɧɚ ɫɟɪɜɟɪ). ɇɢɠɟ ɛɭɞɭɬ ɪɚɫɫɦɨɬɪɟɧɵ ɪɚɡɥɢɱɧɵɟ ɮɭɧɤɰɢɢ, ɢɯ ɩɚɪɚɦɟɬɪɵ ɢ ɧɚɢɛɨɥɟɟ ɱɚɫɬɨ ɜɨɡɧɢɤɚɸɳɢɟ ɨɲɢɛɤɢ. Ⱦɥɹ ɩɨɞɤɥɸɱɟɧɢɹ ɤ ɫɟɪɜɟɪɭ ɤɥɢɟɧɬ ɞɨɥɠɟɧ ɡɧɚɬɶ ɟɝɨ ɚɞɪɟɫ ɢ ɩɪɟɞɨɫɬɚɜɢɬɶ ɟɦɭ ɫɜɨɣ. ɑɬɨɛɵ ɨɛɦɟɧɢɜɚɬɶɫɹ ɫɨɨɛɳɟɧɢɹɦɢ ɧɟɡɚɜɢɫɢɦɨ ɨɬ ɫɜɨɟɝɨ ɦɟɫɬɨɩɨɥɨɠɟɧɢɹ, ɤɥɢɟɧɬ ɢ ɫɟɪɜɟɪ ɢɫɩɨɥɶɡɭɸɬ ɫɨɤɟɬɵ. Ɉɛɪɚɬɢɦɫɹ ɟɳɟ ɪɚɡ ɤ ɩɪɢɦɟɪɭ ɫ ɬɟɥɟɮɨɧɧɵɦ ɡɜɨɧɤɨɦ. Ɍɟɥɟɮɨɧɧɚɹ ɬɪɭɛɤɚ ɢɦɟɟɬ ɞɜɚ ɨɫɧɨɜɧɵɯ ɷɥɟɦɟɧɬɚ: ɦɢɤɪɨɮɨɧ (ɩɟɪɟɞɚɬɱɢɤ) ɢ ɞɢɧɚɦɢɤ (ɩɪɢɟɦɧɢɤ). Ⱥ ɬɟɥɟɮɨɧɧɵɣ ɧɨɦɟɪ, ɩɨ ɫɭɬɢ, ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɭɧɢɤɚɥɶɧɵɣ ɚɞɪɟɫ ɬɪɭɛɤɢ. ɍ ɫɨɤɟɬɚ ɢɦɟɸɬɫɹ ɬɚɤɢɟ ɠɟ ɞɜɚ ɤɚɧɚɥɚ: ɨɞɢɧ ɞɥɹ ɩɪɨɫɥɭɲɢɜɚɧɢɹ, ɚ ɞɪɭɝɨɣ ɞɥɹ ɩɟɪɟɞɚɱɢ (ɩɨɞɨɛɧɨ ɤɚɧɚɥɚɦ ɜɜɨɞɚ-ɜɵɜɨɞɚ ɜ ɮɚɣɥɨɜɨɣ ɫɢɫɬɟɦɟ). Ʉɥɢɟɧɬ (ɡɜɨɧɹɳɢɣ) ɩɨɞɤɥɸɱɚɟɬɫɹ ɤ ɫɟɪɜɟɪɭ (ɚɛɨɧɟɧɬɭ), ɱɬɨɛɵ ɧɚɱɚɬɶ ɫɟɬɟɜɨɣ ɪɚɡɝɨɜɨɪ. Ʉɚɠɞɵɣ ɭɱɚɫɬɧɢɤ ɪɚɡɝɨɜɨɪɚ ɩɪɟɞɥɚɝɚɟɬ ɧɟɫɤɨɥɶɤɨ ɫɬɚɧɞɚɪɬɧɵɯ, ɡɚɪɚɧɟɟ ɢɡɜɟɫɬɧɵɯ ɫɟɪɜɢɫɨɜ (ɫɦ. ɮɚɣɥ /etc/ services), ɧɚɩɪɢɦɟɪ ɬɟɥɟɮɨɧ, ɩɨ ɤɨɬɨɪɨɦɭ ɦɨɠɧɨ ɭɡɧɚɬɶ ɩɪɚɜɢɥɶɧɨɟ ɜɪɟɦɹ. Ȼɨɥɶɲɢɧɫɬɜɨ ɩɪɨɝɪɚɦɦ ɩɪɢɜɨɞɢɦɵɯ ɧɢɠɟ, ɦɨɠɧɨ ɜɵɩɨɥɧɹɬɶ, ɧɟ ɢɦɟɹ ɩɨɞɤɥɸɱɟɧɢɹ ɤ ɫɟɬɢ, ɩɪɢ ɭɫɥɨɜɢɢ, ɱɬɨ ɫɟɬɟɜɵɟ ɮɭɧɤɰɢɢ ɹɞɪɚ Ɉɋ ɫɤɨɧɮɢɝɭɪɢɪɨɜɚɧɵ, ɢ ɞɟɦɨɧ inetd ɡɚɩɭɳɟɧ. ȼ ɷɬɢɯ ɩɪɨɝɪɚɦɦɚɯ ɢɫɩɨɥɶɡɭɟɬɫɹ ɥɨɤɚɥɶɧɵɣ ɫɟɬɟɜɨɣ ɚɞɪɟɫ 127.0.0.1 (ɬɚɤ ɧɚɡɵɜɚɟɦɵɣ ). Ⱦɚɠɟ ɟɫɥɢ ɫɟɬɟɜɵɟ ɞɪɚɣɜɟɪɵ ɨɬɫɭɬɫɬɜɭɸɬ, ɞɢɫɬɪɢɛɭɬɢɜɵ Unux ɫɨɞɟɪɠɚɬ ɜɫɟ ɧɟɨɛɯɨɞɢɦɵɟ ɫɪɟɞɫɬɜɚ ɞɥɹ ɨɪɝɚɧɢɡɚɰɢɢ ɫɟɬɟɜɨɝɨ ɜɡɚɢɦɨɞɟɣɫɬɜɢɹ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɚɞɪɟɫɚ ɨɛɪɚɬɧɨɣ Ʉɥɢɟɧɬɫɤɚɹ ɩɪɨɝɪɚɦɦɚ ɞɨɥɠɧɚ ɩɪɟɞɩɪɢɧɹɬɶ ɧɟɫɤɨɥɶɤɨ ɞɟɣɫɬɜɢɣ ɞɥɹ ɭɫɬɚɧɨɜɥɟɧɢɹ ɫɨɟɞɢɧɟɧɢɹ ɫ ɞɪɭɝɢɦ ɤɨɦɩɶɸɬɟɪɨɦ ɢɥɢ ɫɟɪɜɟɪɨɦ. ɉɪɢɱɟɦ ɷɬɢ ɞɟɣɫɬɜɢɹ ɫɥɟɞɭɟɬ ɜɵɩɨɥɧɹɬɶ ɜ ɨɩɪɟɞɟɥɟɧɧɨɦ ɩɨɪɹɞɤɟ. Ʉɨɧɟɱɧɨ, ɜɨɡɧɢɤɚɟɬ ɜɨɩɪɨɫ: "Ⱥ ɩɨɱɟɦɭ ɧɟɥɶɡɹ ɜɫɟ ɭɩɪɨɫɬɢɬɶ?" Ⱦɟɥɨ ɜ ɬɨɦ, ɱɬɨ ɧɚ ɤɚɠɞɨɦ ɢɡ ɷɬɚɩɨɜ ɩɪɨɝɪɚɦɦɚ ɦɨɠɟɬ ɡɚɞɚɜɚɬɶ ɪɚɡɥɢɱɧɵɟ ɨɩɰɢɢ. ɇɨ ɧɟ ɩɭɝɚɣɬɟɫɶ: ɧɟ ɜɫɟ ɞɟɣɫɬɜɢɹ ɹɜɥɹɸɬɫɹ ɨɛɹɡɚɬɟɥɶɧɵɦɢ. ȿɫɥɢ ɩɪɨɩɭɫɬɢɬɶ ɧɟɤɨɬɨɪɵɟ ɢɡ ɧɢɯ, ɨɩɟɪɚɰɢɨɧɧɚɹ ɫɢɫɬɟɦɚ ɜɨɫɩɨɥɶɡɭɟɬɫɹ ɭɫɬɚɧɨɜɤɚɦɢ ɩɨ ɭɦɨɥɱɚɧɢɸ. Ȼɚɡɨɜɚɹ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɞɟɣɫɬɜɢɣ ɢɦɟɟɬ ɬɚɤɨɣ ɜɢɞ: ɫɨɡɞɚɧɢɟ ɫɨɤɟɬɚ, ɩɨɢɫɤ ɚɞɪɟɫɚɬɚ, ɨɪɝɚɧɢɡɚɰɢɹ ɤɚɧɚɥɚ ɫɜɹɡɢ ɫ ɞɪɭɝɨɣ ɩɪɨɝɪɚɦɦɨɣ ɢ ɪɚɡɪɵɜ ɫɨɟɞɢɧɟɧɢɹ. ɇɢɠɟ ɜ ɝɪɚɮɢɱɟɫɤɨɦ ɜɢɞɟ ɩɪɟɞɫɬɚɜɥɟɧɵ ɞɟɣɫɬɜɢɹ, ɤɨɬɨɪɵɟ ɞɨɥɠɟɧ ɩɪɟɞɩɪɢɧɹɬɶ ɤɥɢɟɧɬ ɩɪɢ ɩɨɞɤɥɸɱɟɧɢɢ ɤ ɫɟɪɜɟɪɭ (Ɋɢɫɭɧɨɤ 1).
Ê ë è åí ò socket()
bind()
connect()
read()
write()
close()
1.
,
-
Ɉɩɢɲɟɦ ɤɚɠɞɵɣ ɢɡ ɷɬɚɩɨɜ: 1.ɋɨɡɞɚɧɢɟ ɫɨɤɟɬɚ. ȼɵɛɨɪ ɫɟɬɟɜɨɝɨ ɞɨɦɟɧɚ ɢ ɬɢɩɚ ɫɨɤɟɬɚ. 2.Ɂɚɞɚɧɢɟ ɩɚɪɚɦɟɬɪɨɜ ɫɨɤɟɬɚ (ɧɟɨɛɹɡɚɬɟɥɶɧɨ). ɉɨɜɟɞɟɧɢɟ ɫɨɤɟɬɚ ɪɟɝɭɥɢɪɭɟɬɫɹ ɦɧɨɠɟɫɬɜɨɦ ɩɚɪɚɦɟɬɪɨɜ. ɉɨɤɚ ɫɨɤɟɬ ɨɬɤɪɵɬ, ɩɪɨɝɪɚɦɦɚ ɦɨɠɟɬ ɦɟɧɹɬɶ ɥɸɛɨɣ ɢɡ ɧɢɯ. 3.ɉɪɢɜɹɡɤɚ ɤ ɨɩɪɟɞɟɥɟɧɧɨɦɭ ɚɞɪɟɫɭ/ɩɨɪɬɭ (ɧɟɨɛɹɡɚɬɟɥɶɧɨ). Ɂɚɞɚɧɢɟ ɤɨɧɤɪɟɬɧɨɝɨ IP-ɚɞɪɟɫɚ, ɚ ɬɚɤɠɟ ɜɵɛɨɪ ɩɨɪɬɚ. ȿɫɥɢ ɩɪɨɩɭɫɬɢɬɶ ɷɬɨɬ ɷɬɚɩ, ɨɩɟɪɚɰɢɨɧɧɚɹ ɫɢɫɬɟɦɚ ɪɚɡɪɟɲɢɬ ɫɜɹɡɶ ɫ ɥɸɛɵɦ IP-ɚɞɪɟɫɨɦ ɢ ɧɚɡɧɚɱɢɬ ɩɪɨɢɡɜɨɥɶɧɵɣ ɧɨɦɟɪ ɩɨɪɬɚ. 4.ɉɨɞɤɥɸɱɟɧɢɟ ɤ ɨɞɧɨɪɚɧɝɨɜɨɦɭ ɤɨɦɩɶɸɬɟɪɭ/ɫɟɪɜɟɪɭ (ɧɟɨɛɹɡɚɬɟɥɶɧɨ). Ɉɪɝɚɧɢɡɚɰɢɹ ɞɜɭɧɚɩɪɚɜɥɟɧɧɨɝɨ ɤɚɧɚɥɚ ɫɜɹɡɢ ɦɟɠɞɭ ɤɥɢɟɧɬɫɤɨɣ ɢ ɞɪɭɝɨɣ ɫɟɬɟɜɨɣ ɩɪɨɝɪɚɦɦɨɣ. ȿɫɥɢ ɩɪɨɩɭɫɬɢɬɶ ɷɬɨɬ ɷɬɚɩ, ɛɭɞɟɬ ɫɨɡɞɚɧ ɤɚɧɚɥ ɚɞɪɟɫɧɨɣ ɩɟɪɟɞɚɱɢ ɫɨɨɛɳɟɧɢɣ ɛɟɡ ɭɫɬɚɧɨɜɥɟɧɢɹ ɫɨɟɞɢɧɟɧɢɹ. 5.ɑɚɫɬɢɱɧɵɣ ɪɚɡɪɵɜ ɫɨɟɞɢɧɟɧɢɹ (ɧɟɨɛɹɡɚɬɟɥɶɧɨ). ȼɵɛɨɪ ɨɞɧɨɝɨ ɢɡ ɞɜɭɯ ɪɟɠɢɦɨɜ ɪɚɛɨɬɵ: ɩɪɢɟɦ ɢɥɢ ɩɟɪɟɞɚɱɚ. ɗɬɨɬ ɷɬɚɩ ɦɨɠɧɨ ɜɵɩɨɥɧɢɬɶ, ɟɫɥɢ ɫɨɡɞɚɧ ɡɚɩɚɫɧɨɣ ɤɚɧɚɥ ɫɜɹɡɢ. 6.ɉɪɢɟɦ/ɩɟɪɟɞɚɱɚ ɫɨɨɛɳɟɧɢɣ (ɧɟɨɛɹɡɚɬɟɥɶɧɨ). ɗɬɨɬ ɷɬɚɩ ɦɨɠɧɨ ɩɪɨɩɭɫɬɢɬɶ, ɟɫɥɢ ɬɪɟɛɭɟɬɫɹ ɜɫɟɝɨ ɥɢɲɶ ɩɪɨɜɟɪɢɬɶ, ɞɨɫɬɭɩɟɧ ɥɢ ɫɟɪɜɟɪ. 7.Ɋɚɡɪɵɜ ɫɨɟɞɢɧɟɧɢɹ. ȿɫɬɟɫɬɜɟɧɧɨ, ɷɬɨɬ ɷɬɚɩ ɜɚɠɟɧ: ɞɨɥɝɨ ɜɵɩɨɥɧɹɸɳɢɟɫɹ ɩɪɨɝɪɚɦɦɵ ɦɨɝɭɬ ɫɨ ɜɪɟɦɟɧɟɦ ɢɫɱɟɪɩɚɬɶ ɥɢɦɢɬ ɞɟɫɤɪɢɩɬɨɪɨɜ ɮɚɣɥɨɜ, ɟɫɥɢ ɧɟ ɡɚɤɪɵɜɚɬɶ ɧɟɢɫɩɨɥɶɡɭɟɦɵɟ ɫɟɚɧɫɵ. ɇɢɠɟ ɧɟɤɨɬɨɪɵɟ ɢɡ ɷɬɚɩɨɜ ɨɩɢɫɵɜɚɸɬɫɹ ɩɨɞɪɨɛɧɟɟ: ɩɪɢɜɨɞɹɬɫɹ ɩɪɢɦɟɪɵ ɢ ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɫɢɫɬɟɦɧɵɟ ɜɵɡɨɜɵ.
ɇɟɫɤɨɥɶɤɨ ɥɟɬ ɧɚɡɚɞ ɩɨɞ ɫɟɬɶɸ ɩɨɞɪɚɡɭɦɟɜɚɥɫɹ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɵɣ ɤɚɧɚɥ ɫɜɹɡɢ ɦɟɠɞɭ ɞɜɭɦɹ ɤɨɦɩɶɸɬɟɪɚɦɢ. ȼɫɟ ɤɨɦɩɶɸɬɟɪɵ ɨɛɳɚɥɢɫɶ ɦɟɠɞɭ ɫɨɛɨɣ ɩɨ ɪɚɡɧɵɦ ɤɚɧɚɥɚɦ, ɚ ɞɥɹ ɩɟɪɟɞɚɱɢ ɮɚɣɥɨɜ ɜ UNIX ɩɪɢɦɟɧɹɥɚɫɶ ɫɢɫɬɟɦɚ UUCP (UNIX-to-UNIX Copy). ɋ ɭɫɨɜɟɪɲɟɧɫɬɜɨɜɚɧɢɟɦ ɬɟɯɧɨɥɨɝɢɢ ɤɚɛɟɥɶɧɨɣ ɩɟɪɟɞɚɱɢ ɞɚɧɧɵɯ ɤɨɧɰɟɩɰɢɹ ɪɚɡɞɟɥɟɧɢɹ ɤɚɧɚɥɚ ɫɜɹɡɢ ɫɬɚɥɚ ɪɟɚɥɶɧɨɣ. Ɉɧɚ ɨɡɧɚɱɚɥɚ, ɱɬɨ ɤɚɠɞɵɣ ɤɨɦɩɶɸɬɟɪ ɞɨɥɠɟɧ ɛɵɥ ɢɞɟɧɬɢɮɢɰɢɪɨɜɚɬɶ ɫɟɛɹ ɭɧɢɤɚɥɶɧɵɦ ɨɛɪɚɡɨɦ ɢ ɠɞɚɬɶ ɫɜɨɟɣ ɨɱɟɪɟɞɢ ɞɥɹ ɩɟɪɟɞɚɱɢ ɞɚɧɧɵɯ. ɋɭɳɟɫɬɜɭɸɬ ɪɚɡɥɢɱɧɵɟ ɫɩɨɫɨɛɵ ɫɨɜɦɟɫɬɧɨɝɨ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɤɚɧɚɥɨɜ ɫɜɹɡɢ, ɢ ɦɧɨɝɢɟ ɢɡ ɧɢɯ ɨɛɟɫɩɟɱɢɜɚɸɬ ɞɨɫɬɚɬɨɱɧɨ ɯɨɪɨɲɭɸ ɩɪɨɢɡɜɨɞɢɬɟɥɶɧɨɫɬɶ. ɂɧɨɝɞɚ ɤɨɦɩɶɸɬɟɪɵ ɩɵɬɚɸɬɫɹ ɩɟɪɟɞɚɜɚɬɶ ɞɚɧɧɵɟ ɨɞɧɨɜɪɟɦɟɧɧɨ, ɜ ɪɟɡɭɥɶɬɚɬɟ ɱɟɝɨ ɜɨɡɧɢɤɚɸɬ ɤɨɧɮɥɢɤɬɵ ɩɚɤɟɬɨɜ. Ɂɚ ɪɟɲɟɧɢɟ ɩɨɞɨɛɧɵɯ ɩɪɨɛɥɟɦ ɢ ɨɪɝɚɧɢɡɚɰɢɸ ɩɨɜɬɨɪɧɨɣ ɩɟɪɟɞɚɱɢ ɞɚɧɧɵɯ ɨɬɜɟɱɚɸɬ ɚɩɩɚɪɚɬɧɵɟ ɢ ɞɪɭɝɢɟ ɧɢɡɤɨɭɪɨɜɧɟɜɵɟ ɞɪɚɣɜɟɪɵ. ɗɬɨ ɩɨɡɜɨɥɹɟɬ ɩɪɨɝɪɚɦɦɢɫɬɭ ɫɤɨɧɰɟɧɬɪɢɪɨɜɚɬɶɫɹ ɧɚ ɪɟɲɟɧɢɢ ɜɨɩɪɨɫɨɜ ɩɪɢɟɦɚ ɢ ɩɟɪɟɞɚɱɢ ɫɨɨɛɳɟɧɢɣ. Ȼɢɛɥɢɨɬɟɤɚ ɮɭɧɤɰɢɣ ɪɚɛɨɬɵ ɫ ɫɨɤɟɬɚɦɢ — Socket API (Application Programming Interface) — ɹɜɥɹɟɬɫɹ ɨɫɧɨɜɧɵɦ ɢɧɫɬɪɭɦɟɧɬɨɦ ɩɪɨɝɪɚɦɦɢɫɬɚ. ɉɪɨɝɪɚɦɦɢɪɨɜɚɧɢɟ ɫɨɤɟɬɨɜ ɨɬɥɢɱɚɟɬɫɹ ɨɬ ɩɪɢɤɥɚɞɧɨɝɨ ɢ ɢɧɫɬɪɭɦɟɧɬɚɥɶɧɨɝɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɩɨɫɤɨɥɶɤɭ ɩɪɢɯɨɞɢɬɫɹ ɢɦɟɬɶ ɞɟɥɨ ɫ ɨɞɧɨɜɪɟɦɟɧɧɨ ɜɵɩɨɥɧɹɸɳɢɦɢɫɹ ɩɪɨɝɪɚɦɦɚɦɢ. ɗɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɬɪɟɛɭɟɬɫɹ ɞɨɩɨɥɧɢɬɟɥɶɧɨ ɪɟɲɚɬɶ ɜɨɩɪɨɫɵ ɫɢɧɯɪɨɧɢɡɚɰɢɢ ɢ ɭɩɪɚɜɥɟɧɢɹ ɪɟɫɭɪɫɚɦɢ. ɋɨɤɟɬɵ ɩɨɡɜɨɥɹɸɬ ɚɫɢɧɯɪɨɧɧɨ ɩɟɪɟɞɚɜɚɬɶ ɞɚɧɧɵɟ ɱɟɪɟɡ ɞɜɭɧɚɩɪɚɜɥɟɧɧɵɣ ɤɚɧɚɥ. ɉɪɢ ɷɬɨɦ ɦɨɝɭɬ ɜɨɡɧɢɤɚɬɶ ɪɚɡɥɢɱɧɨɝɨ ɪɨɞɚ ɩɪɨɛɥɟɦɵ, ɧɚɩɪɢɦɟɪ ɜɡɚɢɦɨɛɥɨɤɢɪɨɜɤɢ ɩɪɨɰɟɫɫɨɜ ɢ ɡɚɜɢɫɚɧɢɹ ɩɪɨɝɪɚɦɦ. ɉɪɢ ɬɳɚɬɟɥɶɧɨɦ ɩɪɨɟɤɬɢɪɨɜɚɧɢɢ ɩɪɢɥɨɠɟɧɢɣ ɛɨɥɶɲɢɧɫɬɜɨ ɬɚɤɢɯ ɩɪɨɛɥɟɦ ɜɩɨɥɧɟ ɦɨɠɧɨ ɢɡɛɟɠɚɬɶ. Ɉɛɵɱɧɨ ɩɟɪɟɝɪɭɠɟɧɧɵɣ ɫɟɪɜɟɪ ɡɚɦɟɞɥɹɟɬ ɪɚɛɨɬɭ ɜ ɫɟɬɢ. ɉɪɚɜɢɥɶɧɚɹ ɫɢɧɯɪɨɧɢɡɚɰɢɹ ɩɪɨɰɟɫɫɨɜ ɢ ɪɚɰɢɨɧɚɥɶɧɨɟ ɪɚɫɩɪɟɞɟɥɟɧɢɟ ɪɟɫɭɪɫɨɜ ɩɨɡɜɨɥɹɸɬ ɫɧɢɡɢɬɶ ɧɚɝɪɭɡɤɭ ɧɚ ɫɟɪɜɟɪ, ɩɨɜɵɫɢɜ ɩɪɨɩɭɫɤɧɭɸ ɫɩɨɫɨɛɧɨɫɬɶ ɫɟɬɢ. Ɇɟɬɨɞɵ ɩɨɜɵɲɟɧɢɹ ɩɪɨɢɡɜɨɞɢɬɟɥɶɧɨɫɬɢ ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ ɜ ɱɚɫɬɢ II, "ɋɨɡɞɚɧɢɟ ɫɟɪɜɟɪɧɵɯ ɩɪɢɥɨɠɟɧɢɣ".
Internet — ɷɬɨ ɫɟɬɶ ɫ ɤɨɦɦɭɬɚɰɢɟɣ ɩɚɤɟɬɨɜ. Ʉɚɠɞɵɣ ɩɚɤɟɬ ɞɨɥɠɟɧ ɫɨɞɟɪɠɚɬɶ ɜɫɸ ɧɟɨɛɯɨɞɢɦɭɸ ɢɧɮɨɪɦɚɰɢɸ, ɤɨɬɨɪɚɹ ɩɨɡɜɨɥɢɬ ɟɦɭ ɞɨɫɬɢɝɧɭɬɶ ɩɭɧɤɬɚ ɧɚɡɧɚɱɟɧɢɹ. ɉɨɞɨɛɧɨ ɩɢɫɶɦɭ, ɩɚɤɟɬ ɫɨɞɟɪɠɢɬ ɚɞɪɟɫɚ ɨɬɩɪɚɜɢɬɟɥɹ ɢ ɩɨɥɭɱɚɬɟɥɹ. ɉɚɤɟɬ ɩɭɬɟɲɟɫɬɜɭɟɬ ɨɬ ɤɨɦɩɶɸɬɟɪɚ ɤ ɤɨɦɩɶɸɬɟɪɭ ɩɨ ɤɚɧɚɥɚɦ ɫɜɹɡɢ ( ). ȿɫɥɢ ɜ ɩɪɨɰɟɫɫɟ ɩɟɪɟɞɚɱɢ ɫɨɨɛɳɟɧɢɹ ɩɪɨɢɫɯɨɞɢɬ ɪɚɡɪɵɜ ɫɨɟɞɢɧɟɧɢɹ, ɩɚɤɟɬ ɧɚɯɨɞɢɬ ɞɪɭɝɨɣ ɦɚɪɲɪɭɬ (ɩɪɨɢɫɯɨɞɢɬ ɤɨɦɦɭɬɚɰɢɹ) ɥɢɛɨ ɦɚɪɲɪɭɬɢɡɚɬɨɪ ɩɨɫɵɥɚɟɬ ɨɬɩɪɚɜɢɬɟɥɸ ɫɨɨɛɳɟɧɢɟ ɨɛ ɨɲɢɛɤɟ, ɢɧɮɨɪɦɢɪɭɸɳɟɟ ɨ ɧɟɜɨɡɦɨɠɧɨɫɬɢ ɨɛɧɚɪɭɠɟɧɢɹ ɩɨɥɭɱɚɬɟɥɹ. Ɍɟɦ ɫɚɦɵɦ ɨɛɟɫɩɟɱɢɜɚɟɬɫɹ ɨɩɪɟɞɟɥɟɧɧɚɹ ɧɚɞɟɠɧɨɫɬɶ ɫɨɟɞɢɧɟɧɢɹ. ɇɨ ɜ ɥɸɛɨɦ ɫɥɭɱɚɟ ɪɚɡɪɵɜɵ ɫɟɬɢ ɩɪɢɜɨɞɹɬ ɤ ɩɨɬɟɪɹɦ ɞɚɧɧɵɯ. ɑɢɬɚɬɟɥɢ ɧɚɜɟɪɧɹɤɚ ɧɟɨɞɧɨɤɪɚɬɧɨ ɫ ɷɬɢɦ ɫɬɚɥɤɢɜɚɥɢɫɶ.
:
TCP/IP
ȼ ɫɟɬɹɯ ɩɪɢɦɟɧɹɟɬɫɹ ɦɧɨɠɟɫɬɜɨ ɪɚɡɥɢɱɧɵɯ ɩɪɨɬɨɤɨɥɨɜ. ɉɪɨɝɪɚɦɦɢɫɬɵ ɩɪɢɫɩɨɫɨɛɢɥɢ ɧɟɤɨɬɨɪɵɟ ɩɪɨɬɨɤɨɥɵ ɞɥɹ ɪɟɲɟɧɢɹ ɫɩɟɰɢɮɢɱɟɫɤɢɯ ɡɚɞɚɱ, ɬɚɤɢɯ ɤɚɤ ɩɟɪɟɞɚɱɚ ɞɚɧɧɵɯ ɩɨɫɪɟɞɫɬɜɨɦ ɞɥɢɧɧɵɯ ɢɥɢ ɭɥɶɬɪɚɤɨɪɨɬɤɢɯ ɜɨɥɧ. Ⱦɪɭɝɢɟ ɩɪɨɬɨɤɨɥɵ ɩɪɟɞɧɚɡɧɚɱɟɧɵ ɞɥɹ ɩɨɜɵɲɟɧɢɹ ɧɚɞɟɠɧɨɫɬɢ ɫɟɬɢ. ɋɟɦɟɣɫɬɜɨ ɩɪɨɬɨɤɨɥɨɜ TCP/IP (Transmission Control Protocol/Internet Protocol) ɨɪɢɟɧɬɢɪɨɜɚɧɨ ɧɚ ɩɟɪɟɞɚɱɭ ɩɚɤɟɬɨɜ ɢ ɜɵɹɜɥɟɧɢɟ ɧɟɮɭɧɤɰɢɨɧɢɪɭɸɳɢɯ ɫɨɟɞɢɧɟɧɢɣ. ȿɫɥɢ ɜ ɤɚɤɨɣ-ɬɨ ɦɨɦɟɧɬ ɨɛɧɚɪɭɠɢɜɚɟɬɫɹ ɧɚɪɭɲɟɧɢɟ ɫɟɝɦɟɧɬɚɰɢɢ ɫɟɬɢ, ɫɢɫɬɟɦɚ ɬɭɬ ɠɟ ɧɚɱɢɧɚɟɬ ɢɫɤɚɬɶ ɧɨɜɵɣ ɦɚɪɲɪɭɬ. ɋɨɩɪɨɜɨɠɞɟɧɢɟ ɩɚɤɟɬɨɜ, ɨɛɧɚɪɭɠɟɧɢɟ ɩɨɬɟɪɶ ɢ ɪɟɬɪɚɧɫɥɹɰɢɹ — ɷɬɨ ɫɥɨɠɧɵɟ ɚɥɝɨɪɢɬɦɵ, ɩɨɫɤɨɥɶɤɭ ɜ ɧɢɯ ɩɪɢɯɨɞɢɬɫɹ ɭɱɢɬɵɜɚɬɶ ɦɧɨɠɟɫɬɜɨ ɪɚɡɥɢɱɧɵɯ ɮɚɤɬɨɪɨɜ. Ʉ ɫɱɚɫɬɶɸ, ɧɚɞɟɠɧɨɫɬɶ ɚɥɝɨɪɢɬɦɨɜ ɞɨɤɚɡɚɧɚ ɨɩɵɬɨɦ. Ɉɛɵɱɧɨ ɜ ɩɪɨɰɟɫɫɟ ɩɪɨɟɤɬɢɪɨɜɚɧɢɹ ɩɪɢɥɨɠɟɧɢɣ ɨɛ ɷɬɢɯ ɚɥɝɨɪɢɬɦɚɯ ɧɟ ɜɫɩɨɦɢɧɚɸɬ, ɩɨɫɤɨɥɶɤɭ ɢɯ ɞɟɬɚɥɢ ɫɤɪɵɬɵ ɝɥɭɛɨɤɨ ɜ ɧɟɞɪɚɯ ɩɪɨɬɨɤɨɥɨɜ. TCP/IP — ɦɧɨɝɨɭɪɨɜɧɟɜɵɣ ɫɬɟɤ: ɜɵɫɨɤɨɭɪɨɜɧɟɜɵɟ ɩɪɨɬɨɤɨɥɵ ɛɨɥɟɟ ɧɚɞɟɠɧɵ, ɧɨ ɦɟɧɟɟ ɝɢɛɤɢ, ɧɚ ɧɢɠɧɢɯ ɭɪɨɜɧɹɯ ɩɨɜɵɲɚɟɬɫɹ ɝɢɛɤɨɫɬɶ, ɧɨ ɡɚ ɫɱɟɬ ɧɚɞɟɠɧɨɫɬɢ. Ȼɢɛɥɢɨɬɟɤɚ Socket API ɢɧɤɚɩɫɭɥɢɪɭɟɬ ɜɫɟ ɧɟɨɛɯɨɞɢɦɵɟ ɢɧɬɟɪɮɟɣɫɵ. ɗɬɨ ɫɭɳɟɫɬɜɟɧɧɵɣ ɨɬɯɨɞ ɨɬ ɩɪɢɜɵɱɧɨɣ ɢɞɟɨɥɨɝɢɢ UNIX, ɤɨɝɞɚ ɡɚ ɤɚɠɞɵɦ ɭɪɨɜɧɟɦ ɡɚɤɪɟɩɥɟɧ ɫɨɛɫɬɜɟɧɧɵɣ ɧɚɛɨɪ ɮɭɧɤɰɢɣ. ɋɬɚɧɞɚɪɬɧɚɹ ɩɨɞɫɢɫɬɟɦɚ ɮɭɧɤɰɢɣ ɜɜɨɞɚ-ɜɵɜɨɞɚ ɬɚɤɠɟ ɹɜɥɹɟɬɫɹ ɦɧɨɝɨɭɪɨɜɧɟɜɨɣ. ɇɨ ɤɨɦɩɶɸɬɟɪɵ, ɪɚɛɨɬɚɸɳɢɟ ɫ TCP/IP, ɞɥɹ ɜɡɚɢɦɨɞɟɣɫɬɜɢɹ ɞɪɭɝ ɫ ɞɪɭɝɨɦ ɢɫɩɨɥɶɡɭɸɬ ɩɨɱɬɢ ɢɫɤɥɸɱɢɬɟɥɶɧɨ ɫɨɤɟɬɵ. ɗɬɨ ɦɨɠɟɬ ɩɨɤɚɡɚɬɶɫɹ ɫɬɪɚɧɧɵɦ, ɟɫɥɢ ɭɱɟɫɬɶ, ɫɤɨɥɶɤɨ ɪɚɡɥɢɱɧɵɯ ɩɪɨɬɨɤɨɥɨɜ ɫɭɳɟɫɬɜɭɟɬ, ɢ ɜɫɩɨɦɧɢɬɶ, ɫɤɨɥɶɤɨ ɪɚɡ ɧɚɦ ɝɨɜɨɪɢɥɢ ɨ ɬɨɦ, ɱɬɨ ɮɭɧɤɰɢɢ ɨɪɟn() (ɜɨɡɜɪɚɳɚɟɬ ɞɟɫɤɪɢɩɬɨɪ ɮɚɣɥɚ) ɢ fopen() (ɜɨɡɜɪɚɳɚɟɬ ɫɫɵɥɤɭ ɧɚ ɮɚɣɥ) ɩɪɚɤɬɢɱɟɫɤɢ ɧɟɫɨɜɦɟɫɬɢɦɵ. ȼ ɞɟɣɫɬɜɢɬɟɥɶɧɨɫɬɢ ɞɨɫɬɭɩ ɤɨ ɜɫɟɦ ɫɟɦɟɣɫɬɜɚɦ ɩɪɨɬɨɤɨɥɨɜ (TCP/IP, IPX, Rose) ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɫ ɩɨɦɨɳɶɸ ɟɞɢɧɫɬɜɟɧɧɨɣ ɮɭɧɤɰɢɢ socket(). Ɉɧɚ ɫɤɪɵɜɚɟɬ ɜ ɫɟɛɟ ɜɫɟ ɞɟɬɚɥɢ ɪɟɚɥɢɡɚɰɢɢ. Ʌɸɛɨɣ ɩɟɪɟɞɚɜɚɟɦɵɣ ɩɚɤɟɬ ɫɨɞɟɪɠɢɬ ɜ ɫɟɛɟ ɞɚɧɧɵɟ, ɚɞɪɟɫɚ ɨɬɩɪɚɜɢɬɟɥɹ ɢ ɩɨɥɭɱɚɬɟɥɹ. ɉɥɸɫ ɤɚɠɞɵɣ ɢɡ ɩɪɨɬɨɤɨɥɨɜ ɞɨɛɚɜɥɹɟɬ ɤ ɩɚɤɟɬɭ ɫɜɨɸ ɫɢɝɧɚɬɭɪɭ, ɡɚɝɨɥɨɜɨɤ ɢ ɩɪɨɱɭɸ ɫɥɭɠɟɛɧɭɸ ɢɧɮɨɪɦɚɰɢɸ. ɗɬɚ ɢɧɮɨɪɦɚɰɢɹ ɩɨɡɜɨɥɹɟɬ ɪɚɫɩɪɨɫɬɪɚɧɹɬɶ ɩɚɤɟɬ ɧɚ ɬɨɦ ɭɪɨɜɧɟ, ɞɥɹ ɤɨɬɨɪɨɝɨ ɨɧ ɩɪɟɞɧɚɡɧɚɱɟɧ. Ʉɨɦɩɶɸɬɟɪ, ɩɨɞɤɥɸɱɟɧɧɵɣ ɤ Internet, ɨɛɹɡɚɬɟɥɶɧɨ ɢɦɟɟɬ ɫɨɛɫɬɜɟɧɧɵɣ IP-ɚɞɪɟɫ, ɹɜɥɹɸɳɢɣɫɹ ɭɧɢɤɚɥɶɧɵɦ 32-ɪɚɡɪɹɞɧɵɦ ɱɢɫɥɨɦ. ȿɫɥɢ ɛɵ ɚɞɪɟɫɚ ɧɟ ɛɵɥɢ ɭɧɢɤɚɥɶɧɵɦɢ, ɛɵɥɨ ɛɵ ɧɟɩɨɧɹɬɧɨ, ɤɭɞɚ ɞɨɫɬɚɜɥɹɬɶ ɩɚɤɟɬ. ȼ TCP/IP ɤɨɧɰɟɩɰɢɹ ɚɞɪɟɫɨɜ ɪɚɫɲɢɪɹɟɬɫɹ ɩɨɧɹɬɢɟɦ . ɉɨɞɨɛɧɨ ɤɨɞɭ ɝɨɪɨɞɚ ɢɥɢ ɫɬɪɚɧɵ, ɧɨɦɟɪ ɩɨɪɬɚ ɞɨɛɚɜɥɹɟɬɫɹ ɤ ɚɞɪɟɫɭ ɤɨɦɩɶɸɬɟɪɚ. ɉɨɪɬɨɜ ɛɵɜɚɟɬ ɦɧɨɠɟɫɬɜɨ, ɢ ɨɧɢ ɧɟ ɹɜɥɹɸɬɫɹ ɮɢɡɢɱɟɫɤɢɦɢ ɫɭɳɧɨɫɬɹɦɢ — ɷɬɨ ɚɛɫɬɪɚɤɰɢɢ, ɫɭɳɟɫɬɜɭɸɳɢɟ ɜ ɪɚɦɤɚɯ ɨɩɟɪɚɰɢɨɧɧɨɣ ɫɢɫɬɟɦɵ. ɋɬɚɧɞɚɪɬɧɵɣ ɮɨɪɦɚɬ IP-ɚɞɪɟɫɚ ɬɚɤɨɜ: [ 0-255 ].[ 0-255 ].[ 0-255 ].[ 0-255 ], ɧɚɩɪɢɦɟɪ 123.45.6.78. Ɂɧɚɱɟɧɢɹ 0 ɢ 255 ɹɜɥɹɸɬɫɹ ɫɩɟɰɢɚɥɶɧɵɦɢ. Ɉɧɢ ɢɫɩɨɥɶɡɭɸɬɫɹ ɜ ɫɟɬɟɜɵɯ ɦɚɫɤɚɯ ɢ ɜ ɪɟɠɢɦɟ ɲɢɪɨɤɨɜɟɳɚɧɢɹ, ɩɨɷɬɨɦɭ ɩɪɢɦɟɧɹɣɬɟ ɢɯ ɫ ɨɫɬɨɪɨɠɧɨɫɬɶɸ. ɇɨɦɟɪ ɩɨɪɬɚ ɨɛɵɱɧɨ ɞɨɛɚɜɥɹɟɬɫɹ ɤ ɚɞɪɟɫɭ ɱɟɪɟɡ ɞɜɨɟɬɨɱɢɟ: [0-255].[0-255].[0-255].[0-255]:[0-65535] ɇɚɩɪɢɦɟɪ, 128.34.26.101:9090 (IP-ɚɞɪɟɫ — 128.34.26.101, ɩɨɪɬ — 9090). ɇɨ ɨɧ ɦɨɠɟɬ ɞɨɛɚɜɥɹɬɶɫɹ ɢ ɱɟɪɟɡ ɬɨɱɤɭ: [0-255].[0-255].[0-255].[0-255].[0-65535] ɇɚɩɪɢɦɟɪ, 64.3.24.24.9999 (IP-ɚɞɪɟɫ — 64.3.24.24, ɩɨɪɬ — 9999). ɇɨɦɟɪ ɩɨɪɬɚ ɱɚɳɟ ɨɬɞɟɥɹɟɬɫɹ ɞɜɨɟɬɨɱɢɟɦ, ɚ ɧɟ ɬɨɱɤɨɣ.ɋ ɤɚɠɞɵɦ IP-ɚɞɪɟɫɨɦ ɦɨɠɟɬ ɛɵɬɶ ɫɜɹɡɚɧɨ ɛɨɥɟɟ 65000 ɩɨɪɬɨɜ, ɱɟɪɟɡ ɤɨɬɨɪɵɟ ɩɨɞɤɥɸɱɚɸɬɫɹ ɫɨɤɟɬɵ.
:
-
ɉɪɨɫɬɟɣɲɢɦ ɫɨɟɞɢɧɟɧɢɟɦ ɹɜɥɹɟɬɫɹ ɬɨ, ɜ ɤɨɬɨɪɨɦ ɤɥɢɟɧɬ ɩɨɞɤɥɸɱɚɟɬɫɹ ɤ ɫɟɪɜɟɪɭ, ɩɨɫɵɥɚɟɬ ɡɚɩɪɨɫ ɢ ɩɨɥɭɱɚɟɬ ɨɬɜɟɬ. ɇɟɤɨɬɨɪɵɟ ɫɬɚɧɞɚɪɬɧɵɟ ɫɟɪɜɢɫɵ ɞɚɠɟ ɧɟ ɬɪɟɛɭɸɬ ɧɚɥɢɱɢɹ ɡɚɩɪɨɫɚ, ɧɚɩɪɢɦɟɪ ɫɟɪɜɢɫ ɬɟɤɭɳɟɝɨ ɜɪɟɦɟɧɢ, ɞɨɫɬɭɩɧɵɣ ɱɟɪɟɡ ɩɨɪɬ ɫ ɧɨɦɟɪɨɦ 13. Ʉ ɫɨɠɚɥɟɧɢɸ, ɜɨ ɦɧɨɝɢɯ ɫɢɫɬɟɦɚɯ Linux ɷɬɨɬ ɫɟɪɜɢɫ ɩɨ ɭɦɨɥɱɚɧɢɸ ɧɟɞɨɫɬɭɩɟɧ, ɢ ɱɬɨɛɵ ɢɦɟɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɨɛɪɚɳɚɬɶɫɹ ɤ ɧɟɦɭ, ɬɪɟɛɭɟɬɫɹ ɦɨɞɢɮɢɰɢɪɨɜɚɬɶ ɮɚɣɥ /etc/inetd.conf. ȿɫɥɢ ɭ ɜɚɫ ɟɫɬɶ ɞɨɫɬɭɩ ɤ ɤɨɦɩɶɸɬɟɪɭ, ɪɚɛɨɬɚɸɳɟɦɭ ɩɨɞ ɭɩɪɚɜɥɟɧɢɟɦ ɨɩɟɪɚɰɢɨɧɧɨɣ ɫɢɫɬɟɦɵ BSD, HP-UX ɢɥɢ Solaris, ɩɨɩɪɨɛɭɣɬɟ ɨɛɪɚɬɢɬɶɫɹ ɤ ɭɤɚɡɚɧɧɨɦɭ ɩɨɪɬɭ. ȿɫɬɶ ɧɟɫɤɨɥɶɤɨ ɫɟɪɜɢɫɨɜ, ɤ ɤɨɬɨɪɵɦ ɦɨɠɧɨ ɫɜɨɛɨɞɧɨ ɩɨɥɭɱɢɬɶ ɞɨɫɬɭɩ. Ɂɚɩɭɫɬɢɬɟ ɩɪɨɝɪɚɦɦɭ Telnet ɢ ɫɜɹɠɢɬɟɫɶ ɫ ɩɨɪɬɨɦ 21 (FTP): telnet 127.0.0.1 21
Ʉɨɝɞɚ ɫɨɟɞɢɧɟɧɢɟ ɛɭɞɟɬ ɭɫɬɚɧɨɜɥɟɧɨ, ɩɪɨɝɪɚɦɦɚ ɩɨɥɭɱɢɬ ɩɪɢɜɟɬɫɬɜɟɧɧɨɟ ɫɨɨɛɳɟɧɢɟ ɨɬ ɫɟɪɜɟɪɚ. Telnet — ɧɟ ɥɭɱɲɚɹ ɩɪɨɝɪɚɦɦɚ ɞɥɹ ɪɚɛɨɬɵ ɫ FTP-ɫɟɪɜɟɪɨɦ, ɧɨ ɫ ɟɟ ɩɨɦɨɳɶɸ ɦɨɠɧɨ ɩɪɨɫɥɟɞɢɬɶ ɛɚɡɨɜɵɣ ɚɥɝɨɪɢɬɦ ɜɡɚɢɦɨɞɟɣɫɬɜɢɹ ɦɟɠɞɭ ɤɥɢɟɧɬɨɦ ɢ ɫɟɪɜɟɪɨɦ, ɫɯɟɦɚɬɢɱɟɫɤɢ ɩɪɟɞɫɬɚɜɥɟɧɧɵɣ ɜ Ʌɢɫɬɢɧɝ 1. ȼ ɧɟɦ ɤɥɢɟɧɬ ɩɨɞɤɥɸɱɚɟɬɫɹ ɤ ɫɟɪɜɟɪɭ, ɩɨɥɭɱɚɟɬ ɩɪɢɜɟɬɫɬɜɟɧɧɨɟ ɫɨɨɛɳɟɧɢɟ ɢ ɨɬɤɥɸɱɚɟɬɫɹ. 1. ɉɪɨɫɬɟɣɲɢɣ ɚɥɝɨɪɢɬɦ TCP-ɤɥɢɟɧɬɚ /***********************************/ /*** ***/ /***********************************/
Ч
.
Ɉɩɢɫɚɧɧɵɣ ɚɥɝɨɪɢɬɦ ɦɨɠɟɬ ɩɨɤɚɡɚɬɶɫɹ ɱɟɪɟɫɱɭɪ ɭɩɪɨɳɟɧɧɵɦ. ȼ ɩɪɢɧɰɢɩɟ, ɬɚɤ ɨɧɨ ɢ ɟɫɬɶ. ɇɨ ɫɚɦɚ ɩɪɨɰɟɞɭɪɚ ɩɨɞɤɥɸɱɟɧɢɹ ɤ ɫɟɪɜɟɪɭ ɢ ɨɪɝɚɧɢɡɚɰɢɢ ɜɡɚɢɦɨɞɟɣɫɬɜɢɹ ɫ ɧɢɦ ɞɟɣɫɬɜɢɬɟɥɶɧɨ ɩɪɨɫɬɚ. ȼ ɫɥɟɞɭɸɳɢɯ ɪɚɡɞɟɥɚɯ ɪɚɫɫɦɚɬɪɢɜɚɟɬɫɹ ɤɚɠɞɵɣ ɢɡ ɭɤɚɡɚɧɧɵɯ ɜɵɲɟ ɷɬɚɩɨɜ.
socket() Ɏɭɧɤɰɢɹ socket () ɹɜɥɹɟɬɫɹ ɭɧɢɜɟɪɫɚɥɶɧɵɦ ɢɧɫɬɪɭɦɟɧɬɨɦ, ɫ ɩɨɦɨɳɶɸ ɤɨɬɨɪɨɝɨ ɨɪɝɚɧɢɡɭɟɬɫɹ ɤɚɧɚɥ ɫɜɹɡɢ ɫ ɞɪɭɝɢɦ ɤɨɦɩɶɸɬɟɪɨɦ ɢ ɡɚɩɭɫɤɚɟɬɫɹ ɩɪɨɰɟɫɫ ɩɪɢɟɦɚ/ɩɟɪɟɞɚɱɢ ɫɨɨɛɳɟɧɢɣ. ɗɬɚ ɮɭɧɤɰɢɹ ɨɛɪɚɡɭɟɬ ɟɞɢɧɵɣ ɢɧɬɟɪɮɟɣɫ ɦɟɠɞɭ ɜɫɟɦɢ ɩɪɨɬɨɤɨɥɚɦɢ ɜ Linux/UNIX. ɉɨɞɨɛɧɨ ɫɢɫɬɟɦɧɨɦɭ ɜɵɡɨɜɭ ɨɪɟɩ(), ɫɨɡɞɚɸɳɟɦɭ ɞɟɫɤɪɢɩɬɨɪ ɞɥɹ ɞɨɫɬɭɩɚ ɤ ɮɚɣɥɚɦ ɢ ɫɢɫɬɟɦɧɵɦ ɭɫɬɪɨɣɫɬɜɚɦ, ɮɭɧɤɰɢɹ socket () ɫɨɡɞɚɟɬ ɞɟɫɤɪɢɩɬɨɪ, ɩɨɡɜɨɥɹɸɳɢɣ ɨɛɪɚɳɚɬɶɫɹ ɤ ɤɨɦɩɶɸɬɟɪɚɦ ɩɨ ɫɟɬɢ. ɉɨɫɪɟɞɫɬɜɨɦ ɩɚɪɚɦɟɬɪɨɜ ɮɭɧɤɰɢɢ ɧɟɨɛɯɨɞɢɦɨ ɭɤɚɡɚɬɶ, ɤ ɤɚɤɨɦɭ ɭɪɨɜɧɸ ɫɬɟɤɚ ɫɟɬɟɜɵɯ ɩɪɨɬɨɤɨɥɨɜ ɬɪɟɛɭɟɬɫɹ ɩɨɥɭɱɢɬɶ ɞɨɫɬɭɩ. ɋɢɧɬɚɤɫɢɫ ɮɭɧɤɰɢɢ ɬɚɤɨɜ: #include #include int socket(int domain, int type, int protocol);
Ɂɧɚɱɟɧɢɹ ɩɚɪɚɦɟɬɪɨɜ ɮɭɧɤɰɢɢ ɦɨɝɭɬ ɛɵɬɶ ɫɚɦɵɦɢ ɪɚɡɧɵɦɢ. ɉɨɥɧɵɣ ɢɯ ɫɩɢɫɨɤ ɩɪɢɜɨɞɢɬɫɹ ɜ ɩɪɢɥɨɠɟɧɢɢ Ⱥ, "ɂɧɮɨɪɦɚɰɢɨɧɧɵɟ ɬɚɛɥɢɰɵ". Ɉɫɧɨɜɧɵɟ ɩɚɪɚɦɟɬɪɵ ɩɟɪɟɱɢɫɥɟɧɵ ɜ ɬɚɛɥ.. 1.1 Т
1
socket ()
domain
PF_INET PF_LOCAL
PF_IPX PF_INET6 SOCK_STREAM
type
SOCK_RDM SOCK_DGRAM SOCK_RAW protocol
ɉɪɨɬɨɤɨɥɵ ɫɟɦɟɣɫɬɜɚ IPv4; ɫɬɟɤ TCP/IP Ʌɨɤɚɥɶɧɵɟ ɢɦɟɧɨɜɚɧɧɵɟ ɤɚɧɚɥɵ ɜ ɫɬɢɥɟ BSD; ɢɫɩɨɥɶɡɭɟɬɫɹ ɭɬɢɥɢɬɨɣ ɠɭɪɧɚɥɶɧɨɣ ɪɟɝɢɫɬɪɚɰɢɢ, ɚ ɬɚɤɠɟ ɩɪɢ ɨɪɝɚɧɢɡɚɰɢɢ ɨɱɟɪɟɞɟɣ ɩɪɢɧɬɟɪɚ ɉɪɨɬɨɤɨɥɵ Novell ɉɪɨɬɨɤɨɥɵ ɫɟɦɟɣɫɬɜɚ IPv6; ɫɬɟɤ TCP/IP ɉɪɨɬɨɤɨɥ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɣ ɩɟɪɟɞɚɱɢ ɞɚɧɧɵɯ (ɜ ɜɢɞɟ ɛɚɣɬɨɜɨɝɨ ɩɨɬɨɤɚ) ɫ ɩɨɞɬɜɟɪɠɞɟɧɢɟɦ ɞɨɫɬɚɜɤɢ (TCP) ɉɪɨɬɨɤɨɥ ɩɚɤɟɬɧɨɣ ɩɟɪɟɞɚɱɢ ɞɚɧɧɵɯ ɫ ɩɨɞɬɜɟɪɠɞɟɧɢɟɦ ɞɨɫɬɚɜɤɢ (ɟɳɟ ɧɟ ɪɟɚɥɢɡɨɜɚɧ ɜ ɛɨɥɶɲɢɧɫɬɜɟ ɨɩɟɪɚɰɢɨɧɧɵɯ ɫɢɫɬɟɦ) ɉɪɨɬɨɤɨɥ ɩɚɤɟɬɧɨɣ ɩɟɪɟɞɚɱɢ ɞɚɧɧɵɯ ɛɟɡ ɩɨɞɬɜɟɪɠɞɟɧɢɹ ɞɨɫɬɚɜɤɢ (UDP - User Datagram Protocol) ɉɪɨɬɨɤɨɥ ɩɚɤɟɬɧɨɣ ɩɟɪɟɞɚɱɢ ɧɢɡɤɨɭɪɨɜɧɟɜɵɯ ɞɚɧɧɵɯ ɛɟɡ ɩɨɞɬɜɟɪɠɞɟɧɢɹ ɞɨɫɬɚɜɤɢ ɉɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ 32-ɪɚɡɪɹɞɧɨɟ ɰɟɥɨɟ ɱɢɫɥɨ ɫ ɫɟɬɟɜɵɦ ɩɨɪɹɞɤɨɦ ɫɥɟɞɨɜɚɧɢɹ ɛɚɣɬɨɜ. ȼ ɛɨɥɶɲɢɧɫɬɜɟ ɬɢɩɨɜ ɫɨɟɞɢɧɟɧɢɣ ɞɨɩɭɫɤɚɟɬɫɹ ɟɞɢɧɫɬɜɟɧɧɨɟ ɡɧɚɱɟɧɢɟ ɞɚɧɧɨɝɨ ɩɚɪɚɦɟɬɪɚ: 0 (ɧɭɥɶ), ɚ ɜ ɫɨɟɞɢɧɟɧɢɹɯ ɬɢɩɚ SOCK RAW ɩɚɪɚɦɟɬɪ ɞɨɥɠɟɧ ɩɪɢɧɢɦɚɬɶ ɡɧɚɱɟɧɢɹ ɨɬ 0 ɞɨ 255.
ȼ ɩɪɢɦɟɪɚɯ, ɩɪɢɜɟɞɟɧɧɵɯ ɜ ɞɚɧɧɨɣ ɤɧɢɝɟ, ɛɭɞɭɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɬɚɤɢɟ ɩɚɪɚɦɟɬɪɵ: domain=PF_INET, type=SOCK_STREAM, protocol=0. П
PF_
AF_ socket()
col family—
PF_
(proto-
).
AF_ (address
family—
).
, ȼɵɡɨɜ ɩɪɨɬɨɤɨɥɚ TCP ɜɵɝɥɹɞɢɬ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
.
int sd; sd = socket(PF_INET, SOCK_STREAM, 0);
ȼ ɩɟɪɟɦɟɧɧɭɸ sd ɛɭɞɟɬ ɡɚɩɢɫɚɧ ɞɟɫɤɪɢɩɬɨɪ ɫɨɤɟɬɚ, ɮɭɧɤɰɢɨɧɚɥɶɧɨ ɷɤɜɢɜɚɥɟɧɬɧɵɣ ɞɟɫɤɪɢɩɬɨɪɭ ɮɚɣɥɚ: int fd; fd = open(...);
ȼ ɫɥɭɱɚɟ ɜɨɡɧɢɤɧɨɜɟɧɢɹ ɨɲɢɛɤɢ ɮɭɧɤɰɢɹ socket () ɜɨɡɜɪɚɳɚɟɬ ɨɬɪɢɰɚɬɟɥɶɧɨɟ ɱɢɫɥɨ ɢ ɩɨɦɟɳɚɟɬ ɤɨɞ ɨɲɢɛɤɢ ɜ ɫɬɚɧɞɚɪɬɧɭɸ ɛɢɛɥɢɨɬɟɱɧɭɸ ɩɟɪɟɦɟɧɧɭɸ errno. ȼɨɬ ɧɚɢɛɨɥɟɟ ɪɚɫɩɪɨɫɬɪɚɧɟɧɧɵɟ ɤɨɞɵ ɨɲɢɛɨɤ. • EPROTONOSUPPORT. Ɍɢɩ ɩɪɨɬɨɤɨɥɚ ɢɥɢ ɭɤɚɡɚɧɧɵɣ ɩɪɨɬɨɤɨɥ ɧɟ ɩɨɞɞɟɪɠɢɜɚɸɬɫɹ ɜ ɞɚɧɧɨɦ ɞɨɦɟɧɟ. ȼ ɛɨɥɶɲɢɧɫɬɜɟ ɞɨɦɟɧɨɜ ɩɚɪɚɦɟɬɪ protocol ɞɨɥɠɟɧ ɪɚɜɧɹɬɶɫɹ ɧɭɥɸ. • EACCES. Ɉɬɫɭɬɫɬɜɭɟɬ ɪɚɡɪɟɲɟɧɢɟ ɧɚ ɫɨɡɞɚɧɢɟ ɫɨɤɟɬɚ ɭɤɚɡɚɧɧɨɝɨ ɬɢɩɚ. ɉɪɢ ɫɨɡɞɚɧɢɢ ɫɨɤɟɬɨɜ ɬɢɩɚ SOCK_RAW ɢ PF_PACKET ɩɪɨɝɪɚɦɦɚ ɞɨɥɠɧɚ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɢ ɩɨɥɶɡɨɜɚɬɟɥɹ root. • EINVAL. ɇɟɢɡɜɟɫɬɧɵɣ ɩɪɨɬɨɤɨɥ ɥɢɛɨ ɫɟɦɟɣɫɬɜɨ ɩɪɨɬɨɤɨɥɨɜ ɧɟɞɨɫɬɭɩɧɨ. Ⱦɚɧɧɚɹ ɨɲɢɛɤɚ ɦɨɠɟɬ ɜɨɡɧɢɤɧɭɬɶ ɩɪɢ ɧɟɩɪɚɜɢɥɶɧɨɦ ɭɤɚɡɚɧɢɢ ɩɚɪɚɦɟɬɪɚ domain ɢɥɢ type. Ʉɨɧɟɱɧɨ ɠɟ, ɫɥɟɞɭɟɬ ɡɧɚɬɶ ɨ ɬɨɦ, ɤɚɤɢɟ ɮɚɣɥɵ ɡɚɝɨɥɨɜɤɨɜ ɬɪɟɛɭɟɬɫɹ ɜɤɥɸɱɚɬɶ ɜ ɩɪɨɝɪɚɦɦɭ. ȼ Linux ɨɧɢ ɬɚɤɨɜɵ: #include #include
/*
*/
/* */
#include
/* */
ȼ ɮɚɣɥɟ sys/socket.h ɧɚɯɨɞɹɬɫɹ ɨɛɴɹɜɥɟɧɢɹ ɮɭɧɤɰɢɣ ɛɢɛɥɢɨɬɟɤɢ Socket API (ɜɤɥɸɱɚɹ ɮɭɧɤɰɢɸ socket(), ɟɫɬɟɫɬɜɟɧɧɨ). ȼ ɮɚɣɥɟ sys/types.h ɨɩɪɟɞɟɥɟɧɵ ɦɧɨɝɢɟ ɬɢɩɵ ɞɚɧɧɵɟ, ɢɫɩɨɥɶɡɭɟɦɵɟ ɩɪɢ ɪɚɛɨɬɟ ɫ ɫɨɤɟɬɚɦɢ. Ф
resolv.h
sys/types.h resolv.h, , sys/types.h
, ,
( ).
,
. Mandrake 6.0-7.0 netinet/in.h, Unux UNIX
. Ⱦɟɣɫɬɜɢɟ ɮɭɧɤɰɢɢ socket() ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɫɨɡɞɚɧɢɢ ɨɱɟɪɟɞɟɣ, ɩɪɟɞɧɚɡɧɚɱɟɧɧɵɯ ɞɥɹ ɩɪɢɟɦɚ ɢ ɨɬɩɪɚɜɤɢ ɞɚɧɧɵɯ. ȼ ɷɬɨɦ ɟɟ ɨɬɥɢɱɢɟ ɨɬ ɮɭɧɤɰɢɢ ɨɪɟɩ(), ɤɨɬɨɪɚɹ ɨɬɤɪɵɜɚɟɬ ɮɚɣɥ ɢ ɱɢɬɚɟɬ ɫɨɞɟɪɠɢɦɨɟ ɟɝɨ ɩɟɪɜɨɝɨ ɛɥɨɤɚ. ɉɨɞɤɥɸɱɟɧɢɟ ɨɱɟɪɟɞɟɣ ɤ ɫɟɬɟɜɵɦ ɩɨɬɨɤɚɦ ɩɪɨɢɫɯɨɞɢɬ ɬɨɥɶɤɨ ɩɪɢ ɜɵɩɨɥɧɟɧɢɢ ɫɢɫɬɟɦɧɨɝɨ ɜɵɡɨɜɚ bind(). ȿɫɥɢ ɩɪɨɜɟɫɬɢ ɚɧɚɥɨɝɢɸ ɫ ɬɟɥɟɮɨɧɧɵɦ ɡɜɨɧɤɨɦ, ɬɨ ɫɨɤɟɬ — ɷɬɨ ɬɪɭɛɤɚ, ɧɟ ɩɨɞɤɥɸɱɟɧɧɚɹ ɧɢ ɤ ɛɚɡɨɜɨɦɭ ɚɩɩɚɪɚɬɭ, ɧɢ ɤ ɬɟɥɟɮɨɧɧɨɣ ɥɢɧɢɢ. Ɏɭɧɤɰɢɢ bind(), connect() ɢ ɧɟɤɨɬɨɪɵɟ ɮɭɧɤɰɢɢ ɜɜɨɞɚ-ɜɵɜɨɞɚ ɫɨɟɞɢɧɹɸɬ ɬɪɭɛɤɭ ɫ ɬɟɥɟɮɨɧɨɦ, ɚ ɬɟɥɟɮɨɧ — ɫ ɥɢɧɢɟɣ. (ȿɫɥɢ ɜ ɩɪɨɝɪɚɦɦɟ ɧɟ ɫɨɞɟɪɠɢɬɫɹ ɹɜɧɨɝɨ ɜɵɡɨɜɚ ɮɭɧɤɰɢɢ bind (), ɬɨ ɟɝɨ ɨɫɭɳɟɫɬɜɥɹɟɬ ɨɩɟɪɚɰɢɨɧɧɚɹ ɫɢɫɬɟɦɚ; ɨɛɪɚɬɢɬɟɫɶ ɤ ɝɥɚɜɟ 4).
ɉɨɫɥɟ ɫɨɡɞɚɧɢɹ ɫɨɤɟɬɚ ɧɟɨɛɯɨɞɢɦɨ ɩɨɞɤɥɸɱɢɬɶɫɹ ɤ ɫɟɪɜɟɪɭ. ɗɬɭ ɡɚɞɚɱɭ ɜɵɩɨɥɧɹɟɬ ɮɭɧɤɰɢɹ connect (), ɞɟɣɫɬɜɢɟ ɤɨɬɨɪɨɣ ɧɚɩɨɦɢɧɚɟɬ ɡɜɨɧɨɤ ɩɨ ɬɟɥɟɮɨɧɭ. •Ʉɨɝɞɚ ɜɵ ɡɜɨɧɢɬɟ ɚɛɨɧɟɧɬɭ, ɜɵ ɧɚɛɢɪɚɟɬɟ ɟɝɨ ɧɨɦɟɪ, ɤɨɬɨɪɵɣ ɢɞɟɧɬɢɮɢɰɢɪɭɟɬ ɬɟɥɟɮɨɧɧɵɣ ɚɩɩɚɪɚɬ, ɪɚɫɩɨɥɨɠɟɧɧɵɣ ɝɞɟ-ɬɨ ɜ ɬɟɥɟɮɨɧɧɨɣ ɫɟɬɢ. Ɍɨɱɧɨ ɬɚɤ ɠɟ IP-ɚɞɪɟɫ ɢɞɟɧɬɢɮɢɰɢɪɭɟɬ ɤɨɦɩɶɸɬɟɪ. Ʉɚɤ ɢ ɭ ɬɟɥɟɮɨɧɧɨɝɨ ɧɨɦɟɪɚ, ɭ IP-ɚɞɪɟɫɚ ɟɫɬɶ ɨɩɪɟɞɟɥɟɧɧɵɣ ɮɨɪɦɚɬ.
•ɋɨɟɞɢɧɟɧɢɟ, ɬɟɥɟɮɨɧɧɨɟ ɢɥɢ ɫɟɬɟɜɨɟ, ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɤɚɧɚɥ ɩɟɪɟɞɚɱɢ ɫɨɨɛɳɟɧɢɣ. Ʉɨɝɞɚ ɱɟɥɨɜɟɤ ɧɚ ɞɪɭɝɨɦ ɤɨɧɰɟ ɩɪɨɜɨɞɚ ɫɧɢɦɚɟɬ ɬɪɭɛɤɭ, ɫɨɟɞɢɧɟɧɢɟ ɫɱɢɬɚɟɬɫɹ ɭɫɬɚɧɨɜɥɟɧɧɵɦ. ȼɚɲ ɬɟɥɟɮɨɧɧɵɣ ɧɨɦɟɪ ɧɟ ɢɦɟɟɬ ɡɧɚɱɟɧɢɹ, ɟɫɥɢ ɬɨɥɶɤɨ ɱɟɥɨɜɟɤ, ɫ ɤɨɬɨɪɵɦ ɜɵ ɨɛɳɚɟɬɟɫɶ, ɧɟ ɡɚɯɨɱɟɬ ɜɚɦ ɩɟɪɟɡɜɨɧɢɬɶ. •ɇɨɦɟɪ ɜɚɲɟɝɨ ɚɩɩɚɪɚɬɚ ɨɩɪɟɞɟɥɹɟɬɫɹ ɜɧɭɬɪɢ ȺɌɋ, ɝɞɟ ɩɪɨɢɫɯɨɞɢɬ ɧɚɩɪɚɜɥɟɧɢɟ ɩɨɬɨɤɨɜ ɫɨɨɛɳɟɧɢɣ, ɩɟɪɟɞɚɜɚɟɦɵɯ ɜ ɪɚɦɤɚɯ ɬɟɤɭɳɟɝɨ ɫɨɟɞɢɧɟɧɢɹ. ȼ ɤɨɦɩɶɸɬɟɪɧɨɣ ɫɟɬɢ ɚɛɨɧɟɧɬɫɤɢɣ ɤɨɦɩɶɸɬɟɪ ɢɥɢ ɫɟɪɜɟɪ ɞɨɥɠɟɧ ɜ ɩɪɨɰɟɫɫɟ ɫɨɟɞɢɧɟɧɢɹ ɭɡɧɚɬɶ ɚɞɪɟɫ ɢ ɩɨɪɬ, ɩɨ ɤɨɬɨɪɵɦ ɦɨɠɧɨ ɛɭɞɟɬ ɫɜɹɡɚɬɶɫɹ ɫ ɜɚɲɟɣ ɩɪɨɝɪɚɦɦɨɣ. ȼɵ ɞɨɥɠɧɵ ɫɨɨɛɳɢɬɶ ɫɜɨɣ ɬɟɥɟɮɨɧɧɵɣ ɧɨɦɟɪ ɥɸɞɹɦ, ɤɨɬɨɪɵɟ ɦɨɝɭɬ ɜɚɦ ɩɨɡɜɨɧɢɬɶ. ȼ ɫɥɭɱɚɟ ɩɪɨɝɪɚɦɦɵ, ɩɪɢɧɢɦɚɸɳɟɣ ɜɯɨɞɧɵɟ ɡɜɨɧɤɢ, ɧɟɨɛɯɨɞɢɦɨ ɧɚɡɧɚɱɢɬɶ ɟɣ ɤɚɧɚɥ (ɢɥɢ ɩɨɪɬ) ɢ ɫɨɨɛɳɢɬɶ ɨ ɧɟɦ ɫɜɨɢɦ ɤɥɢɟɧɬɚɦ.
ɋɢɧɬɚɤɫɢɫ ɮɭɧɤɰɢɢ connect () ɬɚɤɨɜ:
#include #include int connect(int sd, struct sockaddr * server, int addr_len);
ɉɟɪɜɵɣ ɩɚɪɚɦɟɬɪ (sd) ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɞɟɫɤɪɢɩɬɨɪ ɫɨɤɟɬɚ, ɤɨɬɨɪɵɣ ɛɵɥ ɫɨɡɞɚɧ ɮɭɧɤɰɢɟɣ socket(). ɉɨɫɥɟɞɧɢɣ, ɬɪɟɬɢɣ, ɩɚɪɚɦɟɬɪ ɡɚɞɚɟɬ ɞɥɢɧɭ ɫɬɪɭɤɬɭɪɵ sockaddr, ɩɟɪɟɞɚɜɚɟɦɨɣ ɜɨ ɜɬɨɪɨɦ ɩɚɪɚɦɟɬɪɟ, ɬɚɤ ɤɚɤ ɨɧɚ ɦɨɠɟɬ ɢɦɟɬɶ ɪɚɡɧɵɣ ɬɢɩ ɢ ɪɚɡɦɟɪ. ɗɬɨ ɫɚɦɵɣ ɜɚɠɧɵɣ ɦɨɦɟɧɬ, ɞɟɥɚɸɳɢɣ ɮɭɧɤɰɢɸ socket () ɩɪɢɧɰɢɩɢɚɥɶɧɨ ɨɬɥɢɱɧɨɣ ɨɬ ɮɭɧɤɰɢɣ ɮɚɣɥɨɜɨɝɨ ɜɜɨɞɚ-ɜɵɜɨɞɚ. Ɏɭɧɤɰɢɹ socket () ɩɨɞɞɟɪɠɢɜɚɟɬ ɩɨ ɤɪɚɣɧɟɣ ɦɟɪɟ ɞɜɚ ɞɨɦɟɧɚ: PF_INET ɢ PF_IPX. ȼ ɤɚɠɞɨɦ ɢɡ ɫɟɬɟɜɵɯ ɞɨɦɟɧɨɜ ɢɫɩɨɥɶɡɭɟɬɫɹ ɫɜɨɹ ɫɬɪɭɤɬɭɪɚ ɚɞɪɟɫɚ. ȼɫɟ ɫɬɪɭɤɬɭɪɵ ɹɜɥɹɸɬɫɹ ɩɪɨɢɡɜɨɞɧɵɦɢ ɨɬ ɨɞɧɨɝɨ ɨɛɳɟɝɨ ɩɪɟɞɤɚ — ɫɬɪɭɤɬɭɪɵ sockaddr. ɂɦɟɧɧɨ ɨɧɚ ɭɤɚɡɚɧɚ ɜ ɡɚɝɨɥɨɜɤɟ ɮɭɧɤɰɢɢ connect(). я sockaddr
sockaddr , sockaddr. П .
,
.
-
, . ,
push ( _family
), pop (
)
:
. .
sockaddr :
,
16-
.
.
ɉɪɢɜɟɞɟɦ ɨɛɳɢɣ ɜɢɞ ɫɬɪɭɤɬɭɪɵ ɚɞɪɟɫɚ ɢ ɪɹɞɨɦ ɞɥɹ ɫɪɚɜɧɟɧɢɹ — ɫɬɪɭɤɬɭɪɭ ɚɞɪɟɫɚ ɜ ɞɨɦɟɧɟ PF_INET (ɜɡɹɬɨ ɢɡ ɮɚɣɥɨɜ ɡɚɝɨɥɨɜɤɨɜ): struct sockaddr { unsigned short int sa_family; unsigned char sa_data[14]; };
struct sockaddr_in { sa_family_t unsigned short int struct in_addr unsigned char }
я , AF_).
sockaddr ,
socket (), sockaddr ( ,
sin_family; sin_port; sin_addr; __pad[];
:
PF_,
—
PF INET6,
AF_INET6,
.
Ɉɛɪɚɬɢɬɟ ɜɧɢɦɚɧɢɟ: ɩɨɥɹ sa_family ɢ sin_family ɜ ɨɛɟɢɯ ɫɬɪɭɤɬɭɪɚɯ ɹɜɥɹɸɬɫɹ ɨɛɳɢɦɢ. Ʌɸɛɚɹ ɮɭɧɤɰɢɹ, ɩɨɥɭɱɚɸɳɚɹ ɩɨɞɨɛɧɨɝɨ ɪɨɞɚ ɫɬɪɭɤɬɭɪɭ, ɫɧɚɱɚɥɚ ɩɪɨɜɟɪɹɟɬ ɩɟɪɜɨɟ ɩɨɥɟ. ɋɥɟɞɭɟɬ ɬɚɤɠɟ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɷɬɨ ɟɞɢɧɫɬɜɟɧɧɨɟ ɩɨɥɟ ɫ ɫɟɪɜɟɪɧɵɦ ɩɨɪɹɞɤɨɦ ɫɥɟɞɨɜɚɧɢɹ ɛɚɣɬɨɜ. ɉɨɥɹ-ɡɚɩɨɥɧɢɬɟɥɢ (sa data ɢ pad) ɢɫɩɨɥɶɡɭɸɬɫɹ ɜɨ ɦɧɨɝɢɯ ɫɬɪɭɤɬɭɪɚɯ. ɉɨ ɫɭɳɟɫɬɜɭɸɳɟɣ ɞɨɝɨɜɨɪɟɧɧɨɫɬɢ ɫɬɪɭɤɬɭɪɵ sockaddr ɢ sockaddr in ɞɨɥɠɧɵ ɢɦɟɬɶ ɪɚɡɦɟɪ 16 ɛɚɣɬɨɜ (ɜ ɫɬɚɧɞɚɪɬɟ IPv6 ɫɬɪɭɤɬɭɪɚ sockaddr_in6 ɢɦɟɟɬ ɪɚɡɦɟɪ 24 ɛɚɣɬɚ), ɩɨɷɬɨɦɭ ɬɚɤɢɟ ɩɨɥɹ ɞɨɩɨɥɧɹɸɬ ɬɟɥɨ ɫɬɪɭɤɬɭɪɵ ɧɟɡɧɚɱɚɳɢɦɢ ɛɚɣɬɚɦɢ. ɇɟɨɛɯɨɞɢɦɨ ɨɛɪɚɬɢɬɶ ɜɧɢɦɚɧɢɟ ɱɬɨ ɪɚɡɦɟɪ ɦɚɫɫɢɜɚ __pad[] ɧɟ ɭɤɚɡɚɧ. ɇɢɱɟɝɨ ɧɟɩɪɚɜɢɥɶɧɨɝɨ ɜ ɷɬɨɦ ɧɟɬ — ɬɚɤɨɜɨ ɨɛɳɟɩɪɢɧɹɬɨɟ ɫɨɝɥɚɲɟɧɢɟ. ɉɨɫɤɨɥɶɤɭ ɞɚɧɧɵɣ ɦɚɫɫɢɜ ɡɚɩɨɥɧɹɟɬɫɹ ɧɭɥɹɦɢ, ɟɝɨ ɪɚɡɦɟɪ ɧɟ ɢɦɟɟɬ ɡɧɚɱɟɧɢɹ (ɜ ɫɥɭɱɚɟ ɫɬɪɭɤɬɭɪɵ sockaddr_in ɨɧ ɪɚɜɟɧ ɜɨɫɶɦɢ ɛɚɣɬɚɦ). ȼ ɧɟɤɨɬɨɪɵɯ ɫɢɫɬɟɦɚɯ ɜ ɫɬɪɭɤɬɭɪɟ sockaddr in ɜɵɞɟɥɹɸɬɫɹ ɞɨɩɨɥɧɢɬɟɥɶɧɵɟ ɩɨɥɹ ɞɥɹ ɜɧɭɬɪɟɧɧɢɯ ɜɵɱɢɫɥɟɧɢɣ. ɇɟ ɫɬɨɢɬ ɨɛɪɚɳɚɬɶ ɧɚ ɧɢɯ ɜɧɢɦɚɧɢɟ, ɚ ɬɚɤɠɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɢɯ, ɩɨɫɤɨɥɶɤɭ ɧɟɬ ɝɚɪɚɧɬɢɢ, ɱɬɨ ɷɬɢ ɩɨɥɹ ɛɭɞɭɬ ɩɨɞɞɟɪɠɢɜɚɬɶɫɹ ɜ ɞɪɭɝɨɣ ɫɢɫɬɟɦɟ. ȼ ɥɸɛɨɦ ɫɥɭɱɚɟ ɞɨɫɬɚɬɨɱɧɨ ɢɧɢɰɢɚɥɢɡɢɪɨɜɚɬɶ ɞɚɧɧɭɸ ɫɬɪɭɤɬɭɪɭ ɧɭɥɹɦɢ. ɇɢɠɟ ɨɩɢɫɚɧɨ ɧɚɡɧɚɱɟɧɢɟ ɩɨɥɟɣ ɫɬɪɭɤɬɭɪɵ, ɚ ɬɚɤɠɟ ɩɪɢɜɟɞɟɧɵ ɩɪɢɦɟɪɵ ɢɯ ɫɨɞɟɪɠɢɦɨɝɨ. sin_family sin_port sin_addr
ɋɟɦɟɣɫɬɜɨ ɩɪɨɬɨɤɨɥɨɜ ɇɨɦɟɪ ɩɨɪɬɚ ɫɟɪɜɟɪɚ IP-ɚɞɪɟɫ ɫɟɪɜɟɪɚ
ɋɟɪɜɟɪɧɵɣ ɋɟɬɟɜɨɣ ɋɟɬɟɜɨɣ
AF_INET 13 127.0.0.1
ɉɪɟɠɞɟ ɱɟɦ ɜɵɡɜɚɬɶ ɮɭɧɤɰɢɸ connect(), ɩɪɨɝɪɚɦɦɚ ɞɨɥɠɧɚ ɡɚɩɨɥɧɢɬɶ ɨɩɢɫɚɧɧɵɟ ɩɨɥɹ. ȼ ɥɢɫɬɢɧɝɟ 1.2 ɩɨɤɚɡɚɧɨ, ɤɚɤ ɷɬɨ ɫɞɟɥɚɬɶ (ɩɨɥɧɵɣ ɬɟɤɫɬ ɩɪɢɦɟɪɚ ɢɦɟɟɬɫɹ ɧɚ Web-ɭɡɥɟ). ȼɨɨɛɳɟ ɝɨɜɨɪɹ, ɜ Linux ɧɟ ɬɪɟɛɭɟɬɫɹ ɩɪɢɜɨɞɢɬɶ ɫɬɪɭɤɬɭɪɭ sockaddr in ɤ ɬɢɩɭ sockaddr. ȿɫɥɢ ɠɟ ɩɪɟɞɩɨɥɚɝɚɟɬɫɹ ɢɫɩɨɥɶɡɨɜɚɬɶ ɩɪɨɝɪɚɦɦɭ ɜ ɪɚɡɧɵɯ ɫɢɫɬɟɦɚɯ, ɦɨɠɧɨ ɥɟɝɤɨ ɞɨɛɚɜɢɬɶ ɨɩɟɪɚɰɢɸ ɩɪɢɜɟɞɟɧɢɹ ɬɢɩɚ. П
sockaddr
sockaddr. Э
UNIX-
-
. , ).
(
Unux
-
2
connect ()
/***************************************************************/ /*** , ***/ /*** connect(). ***/ /***************************************************************/ #define PORTJTIME 13 struct sockaddr_in dest; char *host = "127.0.0.1"; int sd; /**** ****/ ... bzero(&dest, sizeof(dest)); /* */ dest.sin_family = AF_INET; /* */ dest.sin_port = htons(PORT_TIME); /* */ inet_aton(host, &dest.sin_addr); /* */ if ( connect(sd, &dest, sizeof(dest)) != 0 ) /* ! */ { perror("socket connection"); abort(); }
ɉɟɪɟɞ ɩɨɞɤɥɸɱɟɧɢɟɦ ɤ ɫɟɪɜɟɪɭ ɜɵɩɨɥɧɹɟɬɫɹ ɪɹɞ ɩɨɞɝɨɬɨɜɢɬɟɥɶɧɵɯ ɞɟɣɫɬɜɢɣ. ȼ ɩɟɪɜɭɸ ɨɱɟɪɟɞɶ ɫɨɡɞɚɟɬɫɹ ɫɬɪɭɤɬɭɪɚ sockaddr_in. Ɂɚɬɟɦ ɨɛɴɹɜɥɹɟɬɫɹ ɩɟɪɟɦɟɧɧɚɹ, ɫɨɞɟɪɠɚɳɚɹ ɚɞɪɟɫ, ɩɨ ɤɨɬɨɪɨɦɭ ɛɭɞɟɬ ɩɪɨɢɡɜɟɞɟɧɨ ɨɛɪɚɳɟɧɢɟ. ɉɨɫɥɟ ɷɬɨɝɨ ɜɵɩɨɥɧɹɸɬɫɹ ɞɪɭɝɢɟ, ɧɟ ɩɨɤɚɡɚɧɧɵɟ ɡɞɟɫɶ, ɨɩɟɪɚɰɢɢ, ɜɤɥɸɱɚɹ ɜɵɡɨɜ ɮɭɧɤɰɢɢ socket(). Ɏɭɧɤɰɢɹ bzero() ɡɚɩɨɥɧɹɟɬ ɫɬɪɭɤɬɭɪɭ sockaddr_in ɧɭɥɹɦɢ. ɉɨɥɟ sin family ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɪɚɜɧɵɦ AF_INET. Ⱦɚɥɟɟ ɡɚɞɚɸɬɫɹ ɧɨɦɟɪ ɩɨɪɬɚ ɢ IP-ɚɞɪɟɫ. Ɏɭɧɤɰɢɢ htons () ɢ inet_aton(), ɜɵɩɨɥɧɹɸɳɢɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɬɢɩɨɜ ɞɚɧɧɵɯ, ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ ɜ ɝɥɚɜɟ 2, "Ɉɫɧɨɜɵ TCP/IP". ɇɚɤɨɧɟɰ, ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɩɨɞɤɥɸɱɟɧɢɟ ɤ ɫɟɪɜɟɪɭ. Ɉɛɪɚɬɢɬɟ ɜɧɢɦɚɧɢɟ ɧɚ ɧɟɨɛɯɨɞɢɦɨɫɬɶ ɩɪɨɜɟɪɤɢ ɡɧɚɱɟɧɢɹ, ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɮɭɧɤɰɢɟɣ connect(). ɗɬɨ ɨɞɢɧ ɢɡ ɦɧɨɝɢɯ ɩɪɢɟɦɨɜ, ɩɨɡɜɨɥɹɸɳɢɯ ɩɨɜɵɫɢɬɶ ɧɚɞɟɠɧɨɫɬɶ ɫɟɬɟɜɵɯ ɩɪɢɥɨɠɟɧɢɣ. ɉɨɫɥɟ ɭɫɬɚɧɨɜɥɟɧɢɹ ɫɨɟɞɢɧɟɧɢɹ ɞɟɫɤɪɢɩɬɨɪ ɫɨɤɟɬɚ, sd, ɫɬɚɧɨɜɢɬɫɹ ɞɟɫɤɪɢɩɬɨɪɨɦ ɜɜɨɞɚ-ɜɵɜɨɞɚ, ɞɨɫɬɭɩɧɵɦ ɨɛɟɢɦ ɩɪɨɝɪɚɦɦɚɦ. Ȼɨɥɶɲɢɧɫɬɜɨ ɫɟɪɜɟɪɨɜ ɨɪɢɟɧɬɢɪɨɜɚɧɨ ɧɚ ɜɵɩɨɥɧɟɧɢɟ ɟɞɢɧɫɬɜɟɧɧɨɣ ɬɪɚɧɡɚɤɰɢɢ, ɩɨɫɥɟ ɱɟɝɨ ɪɚɡɪɵɜɚɸɬ ɫɨɟɞɢɧɟɧɢɟ (ɧɚɩɪɢɦɟɪ, ɫɟɪɜɟɪ HTTP 1.0 ɨɬɩɪɚɜɥɹɟɬ ɡɚɩɪɚɲɢɜɚɟɦɵɣ ɮɚɣɥ ɢ ɨɬɤɥɸɱɚɟɬɫɹ). ȼɡɚɢɦɨɞɟɣɫɬɜɭɹ ɫ ɬɚɤɢɦɢ ɫɟɪɜɟɪɚɦɢ, ɩɪɨɝɪɚɦɦɚ ɞɨɥɠɧɚ ɩɨɫɵɥɚɬɶ ɡɚɩɪɨɫ, ɩɨɥɭɱɚɬɶ ɨɬɜɟɬ ɢ ɡɚɤɪɵɜɚɬɶ ɫɨɤɟɬ.
ɂɬɚɤ, ɫɨɤɟɬ ɨɬɤɪɵɬ, ɢ ɫɨɟɞɢɧɟɧɢɟ ɭɫɬɚɧɨɜɥɟɧɨ. Ɇɨɠɧɨ ɧɚɱɢɧɚɬɶ ɪɚɡɝɨɜɨɪ. ɇɟɤɨɬɨɪɵɟ ɫɟɪɜɟɪɵ ɢɧɢɰɢɢɪɭɸɬ ɞɢɚɥɨɝ ɩɨɞɨɛɧɨ ɥɸɞɹɦ, ɪɚɡɝɨɜɚɪɢɜɚɸɳɢɦ ɩɨ ɬɟɥɟɮɨɧɭ. Ɉɧɢ ɤɚɤ ɛɵ ɝɨɜɨɪɹɬ: "Ⱥɥɥɨ!" ɉɪɢɜɟɬɫɬɜɟɧɧɨɟ ɫɨɨɛɳɟɧɢɟ ɦɨɠɟɬ ɜɤɥɸɱɚɬɶ ɢɦɹ ɫɟɪɜɟɪɚ ɢ ɨɩɪɟɞɟɥɟɧɧɵɟ ɢɧɫɬɪɭɤɰɢɢ. Ʉɨɝɞɚ ɫɨɤɟɬ ɨɬɤɪɵɬ, ɦɨɠɧɨ ɜɵɡɵɜɚɬɶ ɫɬɚɧɞɚɪɬɧɵɟ ɧɢɡɤɨɭɪɨɜɧɟɜɵɟ ɮɭɧɤɰɢɢ ɜɜɨɞɚ-ɜɵɜɨɞɚ ɞɥɹ ɩɪɢɟɦɚ ɢ ɩɟɪɟɞɚɱɢ ɞɚɧɧɵɯ. ɇɢɠɟ ɩɪɢɜɟɞɟɧɨ ɨɛɴɹɜɥɟɧɢɟ ɮɭɧɤɰɢɢ read(): #include ssize_t read(int fd, void *buf, size t count);
ɗɬɚ ɮɭɧɤɰɢɹ ɞɨɥɠɧɚ ɛɵɬɶ ɜɚɦ ɡɧɚɤɨɦɚ. ȼɵ ɦɧɨɝɨ ɪɚɡ ɩɪɢɦɟɧɹɥɢ ɟɟ ɩɪɢ ɪɚɛɨɬɟ ɫ ɮɚɣɥɚɦɢ, ɬɨɥɶɤɨ ɧɚ ɷɬɨɬ ɪɚɡ ɧɟɨɛɯɨɞɢɦɨ ɭɤɚɡɵɜɚɬɶ ɞɟɫɤɪɢɩɬɨɪ ɧɟ ɮɚɣɥɚ (fd), ɚ ɫɨɤɟɬɚ (sd) , ȼɨɬ ɤɚɤ ɨɛɵɱɧɨ ɨɪɝɚɧɢɡɭɟɬɫɹ ɜɵɡɨɜ ɮɭɧɤɰɢɢ ɮɭɧɤɰɢɢ read () : ... int sd, bytes_read; sd = socket(PF_INET, SOCK_STREAM, 0); /**** ****/ bytes_read = read(sd, buffer, MAXBUF); if ( bytes_read < 0 ) /* ;
/*
*/
/*
*/
*/
Ⱦɟɫɤɪɢɩɬɨɪ ɫɨɤɟɬɚ ɦɨɠɧɨ ɞɚɠɟ ɩɪɟɨɛɪɚɡɨɜɚɬɶ ɜ ɮɚɣɥɨɜɵɣ ɞɟɫɤɪɢɩɬɨɪ (FILE*), ɟɫɥɢ ɬɪɟɛɭɟɬɫɹ ɪɚɛɨɬɚɬɶ ɫ ɜɵɫɨɤɨɭɪɨɜɧɟɜɵɦɢ ɮɭɧɤɰɢɹɦɢ ɜɜɨɞɚ-ɜɵɜɨɞɚ. ɇɚɩɪɢɦɟɪ, ɜ ɫɥɟɞɭɸɳɟɦ ɮɪɚɝɦɟɧɬɟ ɩɪɨɝɪɚɦɦɵ ɞɟɦɨɧɫɬɪɢɪɭɟɬɫɹ, ɤɚɤ ɩɪɢɦɟɧɢɬɶ ɮɭɧɤɰɢɸ fscanf() ɞɥɹ ɱɬɟɧɢɹ ɞɚɧɧɵɯ ɫ ɫɟɪɜɟɪɚ (ɫɬɪɨɤɢ, ɧɚ ɤɨɬɨɪɵɟ ɫɥɟɞɭɟɬ ɨɛɪɚɬɢɬɶ ɜɧɢɦɚɧɢɟ, ɜɵɞɟɥɟɧɵ ɩɨɥɭɠɢɪɧɵɦ ɲɪɢɮɬɨɦ): char Name[NAME], Address[ADDRESS], Phone[PHONE]; FILE *sp;
int sd; sd = socket(PF_INET, SOCK_STREAM, 0); /****
/*
*/
****/
if ( (sp = fopen(sd, "r")) == NOLL ) /* FILE* */ perror("FILE* conversion failed"); else if ( fscanf(sp, "%*s, %*s, %*s\n",
/* ч
ы
*/
NAME, Name, ADDRESS, Address, PHONE, Phone) < 0) { perror("fscanf"); ...
Ɍɨɥɶɤɨ ɞɟɫɤɪɢɩɬɨɪɵ ɫɨɤɟɬɨɜ ɩɨɬɨɤɨɜɨɝɨ ɬɢɩɚ ɦɨɝɭɬ ɛɵɬɶ ɫɜɨɛɨɞɧɨ ɤɨɧɜɟɪɬɢɪɨɜɚɧɵ ɜ ɮɨɪɦɚɬ FILE *. ɉɪɢɱɢɧɚ ɷɬɨɝɨ ɩɪɨɫɬɚ: ɜ ɩɪɨɬɨɤɨɥɟ UDP ɫɨɟɞɢɧɟɧɢɟ ɧɟ ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɜɨɜɫɟ — ɞɟɣɬɚɝɪɚɦɦɵ ɩɪɨɫɬɨ ɩɨɫɵɥɚɸɬɫɹ ɢ ɜɫɟ. Ʉɪɨɦɟ ɬɨɝɨ, ɩɨɬɨɤɨɜɵɟ ɫɨɤɟɬɵ ɨɛɟɫɩɟɱɢɜɚɸɬ ɰɟɥɨɫɬɧɨɫɬɶ ɞɚɧɧɵɯ ɢ ɧɚɞɟɠɧɭɸ ɞɨɫɬɚɜɤɭ ɫɨɨɛɳɟɧɢɣ, ɬɨɝɞɚ ɤɚɤ ɞɨɫɬɚɜɤɚ ɞɟɣɬɚɝɪɚɦɦ ɧɟ ɝɚɪɚɧɬɢɪɭɟɬɫɹ. ɉɪɢɦɟɧɟɧɢɟ ɞɟɣɬɚɝɪɚɦɦ ɩɨɞɨɛɧɨ ɜɥɨɠɟɧɢɸ ɩɢɫɶɦɚ ɜ ɤɨɧɜɟɪɬ ɫ ɚɞɪɟɫɨɦ, ɨɬɩɪɚɜɥɹɟɦɵɣ ɩɨ ɩɨɱɬɟ: ɧɟɥɶɡɹ ɛɵɬɶ ɩɨɥɧɨɫɬɶɸ ɭɜɟɪɟɧɧɵɦ ɜ ɬɨɦ, ɱɬɨ ɩɢɫɶɦɨ ɞɨɣɞɟɬ ɞɨ ɚɞɪɟɫɚɬɚ. ɋɨɟɞɢɧɟɧɢɟ, ɢɦɟɸɳɟɟ ɞɟɫɤɪɢɩɬɨɪ ɬɢɩɚ FILE*, ɞɨɥɠɧɨ ɛɵɬɶ ɨɬɤɪɵɬɵɦ. ȿɫɥɢ ɩɪɟɨɛɪɚɡɨɜɚɬɶ ɞɚɧɧɵɟ ɜ ɮɨɪɦɚɬ ɞɟɣɬɚɝɪɚɦɦɵ, ɢɯ ɦɨɠɧɨ ɩɨɬɟɪɹɬɶ. ɋɨɟɞɢɧɟɧɢɹ, ɫ ɤɨɬɨɪɵɦɢ ɫɜɹɡɚɧɵ ɮɚɣɥɨɜɵɟ ɞɟɫɤɪɢɩɬɨɪɵ, ɹɜɥɹɸɬɫɹ ɞɥɹ ɫɟɬɟɜɨɝɨ ɩɪɨɝɪɚɦɦɢɫɬɚ ɭɞɨɛɧɵɦ ɫɪɟɞɫɬɜɨɦ ɚɧɚɥɢɡɚ ɩɨɫɬɭɩɚɸɳɢɯ ɞɚɧɧɵɯ. ɋɥɟɞɭɟɬ, ɨɞɧɚɤɨ, ɛɵɬɶ ɩɪɟɞɟɥɶɧɨ ɜɧɢɦɚɬɟɥɶɧɵɦ: ɧɟɨɛɯɨɞɢɦɨ ɩɪɨɜɟɪɹɬɶ ɜɨɡɜɪɚɳɚɟɦɵɟ ɡɧɚɱɟɧɢɹ, ɞɚɠɟ ɭ ɮɭɧɤɰɢɣ fprintf() ɢ fscanf(). Ɉɛɪɚɬɢɬɟ ɜɧɢɦɚɧɢɟ ɧɚ ɬɨ, ɱɬɨ ɜ ɩɨɤɚɡɚɧɧɨɦ ɜɵɲɟ ɩɪɢɦɟɪɟ ɨɬɪɢɰɚɬɟɥɶɧɵɣ ɤɨɞ, ɜɨɡɜɪɚɳɚɟɦɵɣ ɮɭɧɤɰɢɟɣ fscanf (), ɫɢɝɧɚɥɢɡɢɪɭɟɬ ɨɛ ɨɲɢɛɤɟ. —
,
. .
.П -
.
ȼɨɡɜɪɚɳɚɹɫɶ ɤ ɮɭɧɤɰɢɢ read(), ɨɬɦɟɬɢɦ, ɱɬɨ ɱɚɳɟ ɜɫɟɝɨ ɜ ɪɟɡɭɥɶɬɚɬɟ ɟɟ ɜɵɩɨɥɧɟɧɢɹ ɜɨɡɧɢɤɚɸɬ ɬɚɤɢɟ ɨɲɢɛɤɢ.
•EAGAIN. Ɂɚɞɚɧ ɪɟɠɢɦ ɧɟɛɥɨɤɢɪɭɟɦɨɝɨ ɜɜɨɞɚ-ɜɵɜɨɞɚ, ɚ ɞɚɧɧɵɟ ɧɟɞɨɫɬɭɩɧɵ. ɗɬɚ ɨɲɢɛɤɚ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɩɪɨɝɪɚɦɦɚ ɞɨɥɠɧɚ ɜɵɡɜɚɬɶ ɮɭɧɤɰɢɸ ɩɨɜɬɨɪɧɨ. •EBADF. ɍɤɚɡɚɧ ɧɟɜɟɪɧɵɣ ɞɟɫɤɪɢɩɬɨɪ ɮɚɣɥɚ, ɥɢɛɨ ɮɚɣɥ ɧɟ ɛɵɥ ɨɬɤɪɵɬ ɞɥɹ ɱɬɟɧɢɹ. ɗɬɚ ɨɲɢɛɤɚ ɦɨɠɟɬ ɜɨɡɧɢɤɧɭɬɶ, ɟɫɥɢ ɜɵɡɨɜ ɮɭɧɤɰɢɢ socket () ɡɚɜɟɪɲɢɥɫɹ ɧɟɭɫɩɟɲɧɨ ɢɥɢ ɠɟ ɩɪɨɝɪɚɦɦɚ ɡɚɤɪɵɥɚ ɜɯɨɞɧɨɣ ɩɨɬɨɤ (ɤɚɧɚɥ ɞɨɫɬɭɩɟɧ ɬɨɥɶɤɨ ɞɥɹ ɡɚɩɢɫɢ). • EINVAL. ɍɤɚɡɚɧɧɵɣ ɞɟɫɤɪɢɩɬɨɪ ɫɜɹɡɚɧ ɫ ɨɛɴɟɤɬɨɦ, ɱɬɟɧɢɟ ɢɡ ɤɨɬɨɪɨɝɨ ɧɟɜɨɡɦɨɠɧɨ.
Ɏɭɧɤɰɢɹ read() ɧɟ ɢɦɟɟɬ ɢɧɮɨɪɦɚɰɢɢ ɨ ɬɨɦ, ɤɚɤ ɪɚɛɨɬɚɟɬ ɫɨɤɟɬ. ȼ Linux ɟɫɬɶ ɞɪɭɝɚɹ ɮɭɧɤɰɢɹ, recv(), ɤɨɬɨɪɚɹ ɧɚɪɹɞɭ ɫ ɱɬɟɧɢɟɦ ɞɚɧɧɵɯ ɩɨɡɜɨɥɹɟɬ ɤɨɧɬɪɨɥɢɪɨɜɚɬɶ ɪɚɛɨɬɭ ɫɨɤɟɬɚ: #include #include int recv(int sd, void *buf, int len, unsigned int flags);
ɗɬɚ ɮɭɧɤɰɢɹ ɩɪɢɧɢɦɚɟɬ ɬɚɤɢɟ ɠɟ ɩɚɪɚɦɟɬɪɵ, ɤɚɤ ɢ ɮɭɧɤɰɢɹ read(), ɡɚ ɢɫɤɥɸɱɟɧɢɟɦ ɮɥɚɝɨɜ. Ɏɥɚɝɢ ɦɨɠɧɨ ɨɛɴɟɞɢɧɹɬɶ ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɰɢɢ ɩɨɛɢɬɨɜɨɝɨ ɫɥɨɠɟɧɢɹ (ɮɥɚɝ1 | ɮɥɚɝ2 | ...). Ɉɛɵɱɧɨ ɩɨɫɥɟɞɧɢɣ ɩɚɪɚɦɟɬɪ ɡɚɞɚɟɬɫɹ ɪɚɜɧɵɦ ɧɭɥɸ. ɑɢɬɚɬɟɥɢ ɦɨɝɭɬ ɩɨɢɧɬɟɪɟɫɨɜɚɬɶɫɹ, ɞɥɹ ɱɟɝɨ ɜ ɬɚɤɨɦ ɫɥɭɱɚɟ ɜɨɨɛɳɟ ɜɵɡɵɜɚɬɶ ɮɭɧɤɰɢɸ recv()? He ɩɪɨɳɟ ɥɢ ɜɵɡɜɚɬɶ ɮɭɧɤɰɢɸ read()? Ʌɭɱɲɟ ɩɪɢɦɟɧɹɬɶ ɮɭɧɤɰɢɸ recv() — ɷɬɨ ɦɨɠɟɬ ɩɨɦɨɱɶ ɜɚɦ, ɟɫɥɢ ɜɩɨɫɥɟɞɫɬɜɢɢ ɪɚɛɨɬɚ ɩɪɨɝɪɚɦɦɵ ɭɫɥɨɠɧɢɬɫɹ. Ⱦɚ ɢ, ɜɨɨɛɳɟ ɝɨɜɨɪɹ, ɜɫɟɝɞɚ ɫɥɟɞɭɟɬ ɩɪɢɞɟɪɠɢɜɚɬɶɫɹ ɤɚɤɨɝɨ-ɬɨ ɨɞɧɨɝɨ ɫɬɢɥɹ. ɇɢɠɟ ɩɟɪɟɱɢɫɥɟɧɵ ɩɨɥɟɡɧɵɟ ɮɥɚɝɢ, ɫ ɩɨɦɨɳɶɸ ɤɨɬɨɪɵɯ ɦɨɠɧɨ ɭɩɪɚɜɥɹɬɶ ɪɚɛɨɬɨɣ ɫɨɤɟɬɚ.
•MSG_OOB - Ɉɛɪɚɛɨɬɤɚ ɜɧɟɩɨɥɨɫɧɵɯ ɞɚɧɧɵɯ. ɉɪɢɦɟɧɹɟɬɫɹ ɞɥɹ ɫɨɨɛɳɟɧɢɣ ɫ ɩɨɜɵɲɟɧɧɵɦ ɩɪɢɨɪɢɬɟɬɨɦ. ɇɟɤɨɬɨɪɵɟ ɩɪɨɬɨɤɨɥɵ ɩɨɡɜɨɥɹɸɬ ɜɵɛɢɪɚɬɶ, ɫ ɤɚɤɢɦ ɩɪɢɨɪɢɬɟɬɨɦ ɫɥɟɞɭɟɬ ɩɨɫɥɚɬɶ ɫɨɨɛɳɟɧɢɟ: ɨɛɵɱɧɵɦ ɢɥɢ ɜɵɫɨɤɢɦ. ɍɫɬɚɧɨɜɢɬɟ ɷɬɨɬ ɮɥɚɝ, ɱɬɨɛɵ ɞɢɫɩɟɬɱɟɪ ɨɱɟɪɟɞɢ ɢɫɤɚɥ ɢ ɜɨɡɜɪɚɳɚɥ ɬɨɥɶɤɨ ɜɧɟɩɨɥɨɫɧɵɟ ɫɨɨɛɳɟɧɢɹ (ɩɨɞɪɨɛɧɨ ɨɛ ɷɬɨɦ — ɜ ɝɥɚɜɟ 10, "ɋɨɡɞɚɧɢɟ ɭɫɬɨɣɱɢɜɵɯ ɫɨɤɟɬɨɜ"). •MSG_PEEK - Ɋɟɠɢɦ ɧɟɪɚɡɪɭɲɚɸɳɟɝɨ ɱɬɟɧɢɹ. Ɂɚɫɬɚɜɥɹɟɬ ɞɢɫɩɟɬɱɟɪ ɨɱɟɪɟɞɢ ɢɡɜɥɟɤɚɬɶ ɫɨɨɛɳɟ-
ɧɢɹ, ɧɟ ɩɟɪɟɦɟɳɚɹ ɭɤɚɡɚɬɟɥɶ ɨɱɟɪɟɞɢ. Ⱦɪɭɝɢɦɢ ɫɥɨɜɚɦɢ, ɩɪɢ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɵɯ ɨɩɟɪɚɰɢɹɯ ɱɬɟɧɢɹ ɛɭɞɭɬ ɜɨɡɜɪɚɳɚɬɶɫɹ ɨɞɧɢ ɢ ɬɟ ɠɟ ɞɚɧɧɵɟ (ɬɨɱɧɟɟ, ɞɨɥɠɧɵ ɜɨɡɜɪɚɳɚɬɶɫɹ; ɨɛɪɚɬɢɬɟɫɶ ɤɨ ɜɪɟɡɤɟ "ɉɨɥɭɱɟɧɢɟ ɮɪɚɝɦɟɧɬɢɪɨɜɚɧɧɵɯ ɩɚɤɟɬɨɜ").
•MSG_WAITALL - ɋɨɨɛɳɟɧɢɟ ɧɟ ɛɭɞɟɬ ɜɨɡɜɪɚɳɟɧɨ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɧɟ ɡɚɩɨɥɧɢɬɫɹ ɭɤɚɡɚɧɧɵɣ ɛɭɮɟɪ. ɉɪɢ ɨɬɫɭɬɫɬɜɢɢ ɷɬɨɝɨ ɮɥɚɝɚ ɜɨɡɦɨɠɧɨ ɩɨɥɭɱɟɧɢɟ ɱɚɫɬɢɱɧɨ ɡɚɩɨɥɧɟɧɧɨɝɨ ɛɭɮɟɪɚ, ɩɨɫɤɨɥɶɤɭ ɨɫɬɚɥɶɧɵɟ ɞɚɧɧɵɟ ɟɳɟ "ɜ ɩɭɬɢ". ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɩɪɨɝɪɚɦɦɟ ɩɪɢɞɟɬɫɹ "ɫɨɛɢɪɚɬɶ" ɢɯ ɫɚɦɨɫɬɨɹɬɟɥɶɧɨ.
•MSG_DONTWAIT Ɂɚɩɪɨɫ ɤ ɫɨɤɟɬɭ ɧɟ ɛɭɞɟɬ ɛɥɨɤɢɪɨɜɚɧ, ɟɫɥɢ ɨɱɟɪɟɞɶ ɫɨɨɛɳɟɧɢɣ ɩɭɫɬɚ. Ⱥɧɚɥɨɝɢɱɧɵɣ ɪɟɠɢɦ (ɧɟɛɥɨɤɢɪɭɟɦɵɣ ɜɜɨɞ-ɜɵɜɨɞ) ɦɨɠɧɨ ɬɚɤɠɟ ɡɚɞɚɬɶ ɜ ɫɜɨɣɫɬɜɚɯ ɫɚɦɨɝɨ ɫɨɤɟɬɚ. Ɉɛɵɱɧɨ, ɟɫɥɢ ɞɚɧɧɵɟ ɜ ɨɱɟɪɟɞɢ ɨɬɫɭɬɫɬɜɭɸɬ, ɞɢɫɩɟɬɱɟɪ ɨɱɟɪɟɞɢ ɠɞɟɬ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɨɧɢ ɧɟ ɩɨɫɬɭɩɹɬ. Ⱥ ɤɨɝɞɚ ɷɬɨɬ ɮɥɚɝ ɭɫɬɚɧɨɜɥɟɧ, ɮɭɧɤɰɢɹ, ɡɚɩɪɚɲɢɜɚɸɳɚɹ ɞɚɧɧɵɟ, ɧɟɦɟɞɥɟɧɧɨ ɡɚɜɟɪɲɚɟɬɫɹ, ɜɨɡɜɪɚɳɚɹ ɤɨɞ ɨɲɢɛɤɢ EWOULDBLK. (ȼ ɧɚɫɬɨɹɳɟɟ ɜɪɟɦɹ ɜ Linux ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ ɷɬɨɬ ɮɥɚɝ. ɑɬɨɛɵ ɞɨɫɬɢɝɧɭɬɶ ɬɪɟɛɭɟɦɨɝɨ ɪɟɡɭɥɶɬɚɬɚ, ɧɟɨɛɯɨɞɢɦɨ ɜɵɡɜɚɬɶ ɮɭɧɤɰɢɸ fcntl() ɫ ɮɥɚɝɨɦ Ɉ_NONBLOCK. ɗɬɨ ɡɚɫɬɚɜɢɬ ɫɨɤɟɬ ɜɫɟɝɞɚ ɪɚɛɨɬɚɬɶ ɜ ɪɟɠɢɦɟ ɧɟɛɥɨɤɢɪɭɟɦɨɝɨ ɜɜɨɞɚ-ɜɵɜɨɞɚ.) П П
.И
,
, .
recv(), MSG_PEEK recv() : , 500 , MSG_WAITALL.
.
]
-
-|
750.
Ɏɭɧɤɰɢɹ recv() ɹɜɥɹɟɬɫɹ ɛɨɥɟɟ ɝɢɛɤɨɣ, ɱɟɦ read(). ɇɢɠɟ ɩɨɤɚɡɚɧɨ, ɤɚɤ ɩɪɨɱɢɬɚɬɶ ɞɚɧɧɵɟ ɢɡ ɤɚɧɚɥɚ ɫɨɤɟɬɚ (ɷɤɜɢɜɚɥɟɧɬɧɨ ɮɭɧɤɰɢɢ read()): int bytes_read; bytes_read = recv(sd, buffer, MAXBUF, 0); ...
Ⱥ ɜɨɬ ɤɚɤ ɨɫɭɳɟɫɬɜɢɬɶ
ɱɬɟɧɢɟ:
int bytes_read; bytes_read = recv(sd, buffer, MAXBUF, MSG_PEEK); ...
Ɇɨɠɧɨ ɞɚɠɟ ɡɚɞɚɬɶ ɪɟɠɢɦ ɧɟɪɚɡɪɭɲɚɸɳɟɝɨ ɱɬɟɧɢɹ ɜɧɟɩɨɥɨɫɧɵɯ ɞɚɧɧɵɯ: int bytes read; bytes_read = recv(sd, buffer, MAXBUF, MSG_OOB | MSG_PEEK); ...
ȼ ɩɟɪɜɨɦ ɜɚɪɢɚɧɬɟ ɮɭɧɤɰɢɹ ɩɪɨɫɬɨ ɩɟɪɟɞɚɟɬ ɫɟɪɜɟɪɭ ɭɤɚɡɚɬɟɥɶ ɛɭɮɟɪɚ ɢ ɡɧɚɱɟɧɢɟ ɟɝɨ ɞɥɢɧɵ. ȼɨ ɜɬɨɪɨɦ ɮɪɚɝɦɟɧɬɟ ɢɧɮɨɪɦɚɰɢɹ ɤɨɩɢɪɭɟɬɫɹ ɢɡ ɨɱɟɪɟɞɢ, ɧɨ ɧɟ ɢɡɜɥɟɤɚɟɬɫɹ ɢɡ ɧɟɟ. ȼɨ ɜɫɟɯ ɬɪɟɯ ɮɪɚɝɦɟɧɬɚɯ ɟɫɬɶ ɨɞɧɨ ɩɪɟɞɧɚɦɟɪɟɧɧɨɟ ɭɩɭɳɟɧɢɟ. ɑɬɨ ɟɫɥɢ ɫɟɪɜɟɪ ɩɨɲɥɟɬ ɛɨɥɶɲɟ ɢɧɮɨɪɦɚɰɢɢ, ɱɟɦ ɦɨɠɟɬ ɜɦɟɫɬɢɬɶ ɛɭɮɟɪ? ȼ ɞɟɣɫɬɜɢɬɟɥɶɧɨɫɬɢ ɧɢɱɟɝɨ ɫɬɪɚɲɧɨɝɨ ɧɟ ɩɪɨɢɡɨɣɞɟɬ, ɷɬɨ ɧɟ ɤɪɢɬɢɱɟɫɤɚɹ ɨɲɢɛɤɚ. ɉɪɨɫɬɨ ɩɪɨɝɪɚɦɦɚ ɩɨɬɟɪɹɟɬ ɬɟ ɞɚɧɧɵɟ, ɤɨɬɨɪɵɟ ɧɟ ɛɵɥɢ ɩɪɨɱɢɬɚɧɵ. Ɏɭɧɤɰɢɹ recv() ɜɨɡɜɪɚɳɚɟɬ ɬɟ ɠɟ ɤɨɞɵ ɨɲɢɛɨɤ, ɱɬɨ ɢ ɮɭɧɤɰɢɹ read(), ɧɨ ɟɫɬɶ ɢ ɞɨɩɨɥɧɟɧɢɹ.
•ENOTCONN. ɉɪɟɞɨɫɬɚɜɥɟɧɧɵɣ ɞɟɫɤɪɢɩɬɨɪ ɫɨɤɟɬɚ ɧɟ ɫɜɹɡɚɧ ɫ ɨɞɧɨɪɚɧɝɨɜɵɦ ɤɨɦɩɶɸɬɟɪɨɦ ɢɥɢ ɫɟɪɜɟɪɨɦ. •ENOTSOCK. ɉɪɟɞɨɫɬɚɜɥɟɧɧɵɣ ɞɟɫɤɪɢɩɬɨɪ ɧɟ ɫɨɞɟɪɠɢɬ ɫɢɝɧɚɬɭɪɭ, ɭɤɚɡɵɜɚɸɳɭɸ ɧɚ ɬɨ, ɱɬɨ ɨɧ ɛɵɥ ɫɨɡɞɚɧ ɮɭɧɤɰɢɟɣ socket (). ȼɨɨɛɳɟ ɝɨɜɨɪɹ, ɮɭɧɤɰɢɹ read() ɬɨɠɟ ɦɨɠɟɬ ɜɟɪɧɭɬɶ ɷɬɢ ɤɨɞɵ, ɩɨɫɤɨɥɶɤɭ ɧɚ ɫɚɦɨɦ ɞɟɥɟ ɨɧɚ ɩɪɨɜɟɪɹɟɬ, ɤɚɤɨɣ ɞɟɫɤɪɢɩɬɨɪ ɟɣ ɩɟɪɟɞɚɧ, ɢ ɟɫɥɢ ɷɬɨ ɞɟɫɤɪɢɩɬɨɪ ɫɨɤɟɬɚ, ɨɧɚ ɩɪɨɫɬɨ ɜɵɡɵɜɚɟɬ ɮɭɧɤɰɢɸ recv().
ɂɧɮɨɪɦɚɰɢɹ ɨɬ ɫɟɪɜɟɪɚ ɩɨɥɭɱɟɧɚ, ɫɟɚɧɫ ɩɪɨɲɟɥ ɧɨɪɦɚɥɶɧɨ — ɧɚɫɬɚɥɨ ɜɪɟɦɹ ɩɪɟɤɪɚɳɚɬɶ ɫɜɹɡɶ. Ɉɩɹɬɶ-ɬɚɤɢ, ɟɫɬɶ ɞɜɚ ɫɩɨɫɨɛɚ ɫɞɟɥɚɬɶ ɷɬɨ. ȼ ɛɨɥɶɲɢɧɫɬɜɟ ɩɪɨɝɪɚɦɦ ɢɫɩɨɥɶɡɭɟɬɫɹ ɫɬɚɧɞɚɪɬɧɵɣ ɫɢɫɬɟɦɧɵɣ ɜɵɡɨɜ close (): #include
int close(int fd);
ȼɦɟɫɬɨ ɞɟɫɤɪɢɩɬɨɪɚ ɮɚɣɥɚ (fd) ɦɨɠɟɬ ɛɵɬɶ ɭɤɚɡɚɧ ɞɟɫɤɪɢɩɬɨɪ ɫɨɤɟɬɚ (sd) — ɪɚɛɨɬɚ ɮɭɧɤɰɢɢ ɨɬ ɷɬɨɝɨ ɧɟ ɢɡɦɟɧɢɬɫɹ. ȼ ɫɥɭɱɚɟ ɭɫɩɟɲɧɨɝɨ ɡɚɜɟɪɲɟɧɢɹ ɜɨɡɜɪɚɳɚɟɬɫɹ ɡɧɚɱɟɧɢɟ 0. .П
,
" .
,
.
, .
"
-
Ɏɭɧɤɰɢɹ close () ɜɨɡɜɪɚɳɚɟɬ ɜɫɟɝɨ ɨɞɢɧ ɤɨɞ ɨɲɢɛɤɢ. •EBADF. ɍɤɚɡɚɧ ɧɟɜɟɪɧɵɣ ɞɟɫɤɪɢɩɬɨɪ ɮɚɣɥɚ.
Ɏɭɧɤɰɢɹ shutdown () ɩɨɡɜɨɥɹɟɬ ɥɭɱɲɟ ɭɩɪɚɜɥɹɬɶ ɩɪɨɰɟɫɫɨɦ ɪɚɡɪɵɜɚ ɫɨɟɞɢɧɟɧɢɹ, ɩɨɫɤɨɥɶɤɭ ɦɨɠɟɬ ɡɚɤɪɵɜɚɬɶ ɨɬɞɟɥɶɧɨ ɜɯɨɞɧɵɟ ɢ ɜɵɯɨɞɧɵɟ ɤɚɧɚɥɵ. ɗɬɚ ɮɭɧɤɰɢɹ ɨɫɨɛɟɧɧɨ ɩɨɥɟɡɧɚ, ɤɨɝɞɚ ɫɨɤɟɬ ɡɚɦɟɳɚɟɬ ɫɬɚɧɞɚɪɬɧɵɟ ɩɨɬɨɤɢ stdin ɢ stdout. П Ф
shutdown shutdown () UNIX),
shutdown (
.
8
.
ɋ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɢ shutdown () ɦɨɠɧɨ ɡɚɤɪɵɬɶ ɤɚɧɚɥ ɜ ɨɞɧɨɦ ɧɚɩɪɚɜɥɟɧɢɢ, ɫɞɟɥɚɜ ɟɝɨ ɞɨɫɬɭɩɧɵɦ ɬɨɥɶɤɨ ɞɥɹ ɱɬɟɧɢɹ ɢɥɢ ɬɨɥɶɤɨ ɞɥɹ ɡɚɩɢɫɢ: #include int shutdown(int s, int how);
ɉɚɪɚɦɟɬɪ how ɦɨɠɟɬ ɩɪɢɧɢɦɚɬɶ ɬɪɢ ɡɧɚɱɟɧɢɹ. В Ɂɚɤɪɵɬɶ ɤɚɧɚɥ ɱɬɟɧɢɹ Ɂɚɤɪɵɬɶ ɤɚɧɚɥ ɡɚɩɢɫɢ Ɂɚɤɪɵɬɶ ɨɛɚ ɤɚɧɚɥɚ
0 1 2
: Ʉɨɝɞɚ ɩɪɨɝɪɚɦɦɚ ɫɨɡɞɚɟɬ ɫɨɤɟɬ ɢ ɩɨɞɤɥɸɱɚɟɬɫɹ ɤ TCP-ɫɟɪɜɟɪɭ, ɩɪɨɢɫɯɨɞɢɬ ɰɟɥɵɣ ɪɹɞ ɞɟɣɫɬɜɢɣ. ɋɚɦ ɩɨ ɫɟɛɟ ɫɨɤɟɬ ɨɪɝɚɧɢɡɭɟɬ ɥɢɲɶ ɨɱɟɪɟɞɶ ɫɨɨɛɳɟɧɢɣ. Ɉɫɧɨɜɧɨɣ ɩɪɨɰɟɫɫ ɧɚɱɢɧɚɟɬɫɹ ɩɪɢ ɩɨɞɤɥɸɱɟɧɢɢ. ɇɢɠɟ ɩɨɷɬɚɩɧɨ ɪɚɫɩɢɫɚɧɨ, ɱɬɨ ɩɪɨɢɫɯɨɞɢɬ ɧɚ ɫɬɨɪɨɧɟ ɤɥɢɟɧɬɚ ɢ ɫɟɪɜɟɪɚ (Ɍɚɛɥɢɰɚ 2). Т
2Д
,
Д Д 1. ȼɵɡɨɜ ɮɭɧɤɰɢɢ socket (): ɫɨɡ-(Ɉɠɢɞɚɧɢɟ ɩɨɞɤɥɸɱɟɧɢɹ) ɞɚɧɢɟ ɨɱɟɪɟɞɢ ɫɨɨɛɳɟɧɢɣ, ɭɫɬɚɧɨɜɤɚ ɮɥɚɝɨɜ ɩɪɨɬɨɤɨɥɚ 2. ȼɵɡɨɜ ɮɭɧɤɰɢɢ connect ():(Ɉɠɢɞɚɧɢɟ) ɨɩɟɪɚɰɢɨɧɧɚɹ ɫɢɫɬɟɦɚ ɧɚɡɧɚɱɚɟɬ ɫɨɤɟɬɭ ɩɨɪɬ, ɟɫɥɢ ɨɧ ɧɟ ɛɵɥ ɧɚɡɧɚɱɟɧ ɫ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɢ bind () 3. Ɉɬɩɪɚɜɤɚ ɫɨɨɛɳɟɧɢɹ, ɜ ɤɨɬɨɪɨɦ ɡɚɩɪɚɲɢɜɚɟɬɫɹ ɭɫɬɚɧɨɜɥɟɧɢɟ ɫɨɟɞɢɧɟɧɢɹ ɢ ɫɨɨɛɳɚɟɬɫɹ ɧɨɦɟɪ ɩɨɪɬɚ (Ɉɠɢɞɚɧɢɟ ɨɬɜɟɬɚ ɫɟɪɜɟɪɚ) 4. ɉɨɦɟɳɟɧɢɟ ɡɚɩɪɨɫɚ ɜ ɨɱɟɪɟɞɶ ɩɨɪɬɚ (Ɉɠɢɞɚɧɢɟ) 5. ɑɬɟɧɢɟ ɞɚɧɧɵɯ ɢɡ ɨɱɟɪɟɞɢ, ɩɪɢɟɦ ɡɚɩɪɨɫɚ ɢ ɫɨɡɞɚɧɢɟ ɭɧɢɤɚɥɶɧɨɝɨ ɤɚɧɚɥɚ ɞɥɹ ɫɨɤɟɬɚ (Ɉɠɢɞɚɧɢɟ) 6. ɋɨɡɞɚɧɢɟ (ɢɧɨɝɞɚ) ɭɧɢɤɚɥɶɧɨɝɨ ɡɚɞɚɧɢɹ ɢɥɢ ɩɨɬɨɤɚ ɞɥɹ ɜɡɚɢɦɨɞɟɣɫɬɜɢɹ ɫ ɩɪɨɝɪɚɦɦɨɣ (Ɉɠɢɞɚɧɢɟ) 7. Ɉɬɩɪɚɜɤɚ ɩɨɞɬɜɟɪɠɞɟɧɢɹ ɨ ɬɨɦ, ɱɬɨ ɫɨɟɞɢɧɟɧɢɟ ɭɫɬɚɧɨɜɥɟɧɨ. ɋɟɪɜɟɪ ɥɢɛɨ ɩɨɫɵɥɚɟɬ ɫɨɨɛɳɟɧɢɟ ɩɨ ɭɤɚɡɚɧɧɨɦɭ ɩɨɪɬɭ, ɥɢɛɨ ɨɠɢɞɚɟɬ ɡɚɩɪɨɫɚ ɨɬ ɩɪɨɝɪɚɦɦɵ. ɉɨɫɥɟ ɩɟɪɟɞɚɱɢ ɞɚɧɧɵɯ ɫɟɪɜɟɪ ɦɨɠɟɬ ɡɚɤɪɵɬɶ ɤɚɧɚɥ, ɟɫɥɢ ɨɧ ɜɵɞɚɟɬ ɬɨɥɶɤɨ ɨɞɧɨɫɬɨɪɨɧɧɢɟ ɫɨɨɛɳɟɧɢɹ (ɧɚɩɪɢɦɟɪ, ɫɨɨɛɳɚɟɬ
ɬɟɤɭɳɟɟ ɜɪɟɦɹ) 8. ɇɚɱɚɥɨ ɩɟɪɟɞɚɱɢ ɞɚɧɧɵɯ ɗɬɨɝɨ ɞɨɫɬɚɬɨɱɧɨ ɞɥɹ ɩɪɨɫɬɨɝɨ ɜɵɡɨɜɚ ɮɭɧɤɰɢɢ connect (). Ɉɩɢɫɚɧɧɵɣ ɩɪɨɰɟɫɫ ɦɨɠɟɬ ɛɵɬɶ ɝɨɪɚɡɞɨ ɛɨɥɟɟ ɫɥɨɠɧɵɦ, ɟɫɥɢ ɦɟɠɞɭ ɤɥɢɟɧɬɨɦ ɢ ɫɟɪɜɟɪɨɦ ɧɚɯɨɞɹɬɫɹ ɤɨɦɩɶɸɬɟɪɵ, ɜɵɩɨɥɧɹɸɳɢɟ ɦɚɪɲɪɭɬɢɡɚɰɢɸ (ɤɨɦɦɭɬɚɰɢɸ ɢ ɜɟɪɢɮɢɤɚɰɢɸ ɩɚɤɟɬɨɜ, ɮɪɚɝɦɟɧɬɚɰɢɸ ɢ ɞɟɮɪɚɝɦɟɧɬɚɰɢɸ, ɬɪɚɧɫɥɹɰɢɸ ɩɪɨɬɨɤɨɥɨɜ, ɬɭɧɧɟɥɢɪɨɜɚɧɢɟ ɢ ɬ.ɞ.). Ȼɢɛɥɢɨɬɟɤɚ Socket API ɡɧɚɱɢɬɟɥɶɧɨ ɭɩɪɨɳɚɟɬ ɫɟɬɟɜɨɟ ɜɡɚɢɦɨɞɟɣɫɬɜɢɟ. Ⱦɥɹ ɨɪɝɚɧɢɡɚɰɢɢ ɫɨɟɞɢɧɟɧɢɹ ɬɪɟɛɭɟɬɫɹ ɡɧɚɬɶ ɹɡɵɤ ɢ ɩɪɚɜɢɥɚ ɫɟɬɟɜɨɝɨ ɨɛɳɟɧɢɹ. ȼɫɟ ɧɚɱɢɧɚɟɬɫɹ ɫ ɮɭɧɤɰɢɢ socket(), ɤɨɬɨɪɚɹ ɫɨɡɞɚɟɬ ɚɧɚɥɨɝ ɬɟɥɟɮɨɧɧɨɣ ɬɪɭɛɤɢ. ɑɟɪɟɡ ɷɬɭ "ɬɪɭɛɤɭ" ɩɪɨɝɪɚɦɦɚ ɩɨɫɵɥɚɟɬ ɢ ɩɪɢɧɢɦɚɟɬ ɫɨɨɛɳɟɧɢɹ. ɑɬɟɧɢɟ ɢ ɡɚɩɢɫɶ ɞɚɧɧɵɯ ɨɫɭɳɟɫɬɜɥɹɸɬɫɹ ɫ ɩɨɦɨɳɶɸ ɬɟɯ ɠɟ ɫɚɦɵɯ ɮɭɧɤɰɢɣ read() ɢ write () ɤɨɬɨɪɵɟ ɩɪɢɦɟɧɹɸɬɫɹ ɩɪɢ ɪɚɛɨɬɟ ɫ ɮɚɣɥɚɦɢ. Ȼɨɥɟɟ ɫɥɨɠɧɵɟ ɫɢɫɬɟɦɵ ɫɬɪɨɹɬɫɹ ɜɨɤɪɭɝ ɮɭɧɤɰɢɢ recv().
П
е
я
е
Socket API
ȼ ɫɟɬɟɜɨɦ ɫɨɟɞɢɧɟɧɢɢ ɜɫɟɝɞɚ ɟɫɬɶ ɨɬɩɪɚɜɢɬɟɥɶ ɢ ɩɨɥɭɱɚɬɟɥɶ. ȼ ɨɛɳɟɦ ɫɥɭɱɚɟ ɨɬɩɪɚɜɢɬɟɥɟɦ ɹɜɥɹɟɬɫɹ ɤɥɢɟɧɬ, ɤɨɬɨɪɵɣ ɡɚɩɪɚɲɢɜɚɟɬ ɫɟɪɜɢɫ, ɩɪɟɞɨɫɬɚɜɥɹɟɦɵɣ ɫɟɬɟɜɵɦ ɤɨɦɩɶɸɬɟɪɨɦ. ȼ ɱɚɫɬɢ I, "ɋɨɡɞɚɧɢɟ ɫɟɬɟɜɵɯ ɤɥɢɟɧɬɫɤɢɯ ɩɪɢɥɨɠɟɧɢɣ", ɪɚɫɫɦɚɬɪɢɜɚɥɢɫɶ ɨɫɧɨɜɵ ɤɥɢɟɧɬɫɤɨɝɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ: ɤɚɤ ɩɨɞɤɥɸɱɢɬɶ ɤɥɢɟɧɬɚ ɤ ɫɟɪɜɟɪɭ, ɤɚɤ ɨɪɝɚɧɢɡɨɜɚɬɶ ɩɪɹɦɭɸ ɞɨɫɬɚɜɤɭ ɫɨɨɛɳɟɧɢɣ ɛɟɡ ɭɫɬɚɧɨɜɥɟɧɢɹ ɫɨɟɞɢɧɟɧɢɹ ɢ ɤɚɤ ɪɚɛɨɬɚɬɶ ɫ ɩɪɨɬɨɤɨɥɚɦɢ ɫɬɟɤɚ TCP/IP. ɋ ɷɬɨɣ ɝɥɚɜɵ ɧɚɱɢɧɚɟɬɫɹ ɡɧɚɤɨɦɫɬɜɨ ɫ ɞɪɭɝɨɣ ɫɬɨɪɨɧɨɣ ɫɨɟɞɢɧɟɧɢɹ — ɩɪɢɟɦɧɢɤɨɦ, ɢɥɢ ɫɟɪɜɟɪɨɦ. ɑɬɨɛɵ ɩɨɧɹɬɶ ɫɯɟɦɭ ɜɡɚɢɦɨɞɟɣɫɬɜɢɹ ɤɥɢɟɧɬɚ ɢ ɫɟɪɜɟɪɚ, ɩɪɟɞɫɬɚɜɶɬɟ, ɱɬɨ ɫɟɬɶ — ɷɬɨ ɬɟɥɟɮɨɧɧɚɹ ɫɢɫɬɟɦɚ ɛɨɥɶɲɨɣ ɤɨɦɩɚɧɢɢ, ɜ ɤɨɬɨɪɨɣ ɫɟɪɜɟɪ ɹɜɥɹɟɬɫɹ ɰɟɧɬɪɚɥɶɧɵɦ ɬɟɥɟɮɨɧɧɵɦ ɧɨɦɟɪɨɦ, ɧɚɩɪɚɜɥɹɸɳɢɦ ɡɜɨɧɤɢ ɤɨɧɤɪɟɬɧɵɦ ɫɥɭɠɚɳɢɦ. Ʉɥɢɟɧɬ ɫɜɹɡɵɜɚɟɬɫɹ ɫ ɬɪɟɛɭɟɦɵɦ ɫɥɭɠɚɳɢɦ, ɧɚɛɢɪɚɹ ɰɟɧɬɪɚɥɶɧɵɣ ɢ ɞɨɩɨɥɧɢɬɟɥɶɧɵɣ ɧɨɦɟɪɚ. Ɍɟɩɟɪɶ ɫɢɬɭɚɰɢɹ ɩɪɨɹɫɧɹɟɬɫɹ. ɐɟɧɬɪɚɥɶɧɵɣ ɧɨɦɟɪ ɹɜɥɹɟɬɫɹ ɚɞɪɟɫɨɦ ɫɟɬɟɜɨɝɨ ɭɡɥɚ, ɚ ɞɨɩɨɥɧɢɬɟɥɶɧɵɣ ɧɨɦɟɪ — ɷɬɨ ɩɨɪɬ ɤɨɧɤɪɟɬɧɨɝɨ ɫɟɪɜɢɫɚ. Ʉɥɢɟɧɬ ɞɨɥɠɟɧ ɡɧɚɬɶ ɧɨɦɟɪ ɩɨɪɬɚ, ɩɨ ɤɨɬɨɪɨɦɭ ɨɛɪɚɳɚɟɬɫɹ. ɗɬɨ ɩɨɯɨɠɟ ɧɚ ɬɟɥɟɮɨɧɧɵɣ ɧɨɦɟɪ, ɤɨɬɨɪɵɣ ɞɨɥɠɟɧ ɛɵɬɶ ɝɞɟ-ɬɨ ɨɩɭɛɥɢɤɨɜɚɧ: ɟɫɥɢ ɤɥɢɟɧɬ ɧɟ ɡɧɚɟɬ ɧɨɦɟɪ, ɨɧ ɧɟ ɫɦɨɠɟɬ ɩɨ ɧɟɦɭ ɩɨɡɜɨɧɢɬɶ. ɇɢɠɟ ɦɵ ɨɫɬɚɧɨɜɢɦɫɹ ɧɚ ɬɨɦ, ɤɚɤ ɩɪɟɞɨɫɬɚɜɥɹɬɶ ɫɟɪɜɢɫɵ ɢ ɲɚɝ ɡɚ ɲɚɝɨɦ ɪɚɫɫɦɚɬɪɢɦ ɩɪɨɰɟɫɫ ɫɨɡɞɚɧɢɹ ɫɟɪɜɟɪɚ.
: ɉɪɨɰɟɫɫ ɩɨɫɬɪɨɟɧɢɹ ɫɟɪɜɟɪɚ ɜɫɟɝɞɚ ɧɚɱɢɧɚɟɬɫɹ ɫ ɫɨɡɞɚɧɢɹ ɫɨɤɟɬɚ. ɉɨɞɨɛɧɨ ɬɨɦɭ ɤɚɤ ɜ ɤɥɢɟɧɬɫɤɨɣ ɩɪɨɝɪɚɦɦɟ ɬɪɟɛɭɟɬɫɹ ɨɩɪɟɞɟɥɟɧɧɚɹ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɫɢɫɬɟɦɧɵɯ ɜɵɡɨɜɨɜ, ɚɧɚɥɨɝɢɱɧɚɹ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɧɟɨɛɯɨɞɢɦɚ ɢ ɧɚ ɫɟɪɜɟɪɟ, ɬɨɥɶɤɨ ɡɞɟɫɶ ɨɧɚ ɞɥɢɧɧɟɟ. ȿɫɥɢ ɧɟɤɨɬɨɪɵɟ ɮɭɧɤɰɢɢ ɤɥɢɟɧɬɭ ɜɵɡɵɜɚɬɶ ɧɟ ɨɛɹɡɚɬɟɥɶɧɨ, ɬɨ ɞɥɹ ɫɟɪɜɟɪɧɨɝɨ ɩɪɢɥɨɠɟɧɢɹ ɜɫɟ ɨɧɢ ɧɭɠɧɵ (Ɋɢɫɭɧɨɤ 2). Ʉɥɢɟɧɬɫɤɚɹ ɩɪɨɝɪɚɦɦɚ, ɤɨɬɨɪɭɸ ɦɵ ɩɢɫɚɥɢ ɜ ɩɟɪɜɵɯ ɝɥɚɜɚɯ, ɜɵɡɵɜɚɥɚ ɮɭɧɤɰɢɢ ɜ ɬɚɤɨɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ: socket (), connect (), read(), write () ɢ close (). ɋɢɫɬɟɦɧɵɣ ɜɵɡɨɜ bind() ɛɵɥ ɧɟɨɛɹɡɚɬɟɥɶɧɵɦ, ɬɚɤ ɤɚɤ ɷɬɭ ɮɭɧɤɰɢɸ ɜɵɡɵɜɚɥɚ ɨɩɟɪɚɰɢɨɧɧɚɹ ɫɢɫɬɟɦɚ. ɇɨɦɟɪ ɩɨɪɬɚ ɧɟ ɬɪɟɛɨɜɚɥɫɹ, ɩɨɫɤɨɥɶɤɭ ɩɪɨɝɪɚɦɦɚ ɨɛɪɚɳɚɥɚɫɶ ɧɚɩɪɹɦɭɸ ɤ ɫɟɪɜɟɪɭ. Ʉɥɢɟɧɬ ɜɫɟɝɞɚ ɫɨɡɞɚɟɬ , ɩɨɬɨɦɭ ɱɬɨ ɨɧ ɩɨɫɬɨɹɧɧɨ ɟɝɨ ɡɚɧɢɦɚɟɬ. ɋ ɞɪɭɝɨɣ ɫɬɨɪɨɧɵ, ɫɟɪɜɟɪɧɵɟ ɩɪɨɝɪɚɦɦɵ ɞɨɥɠɧɵ ɩɪɟɞɨɫɬɚɜɥɹɬɶ ɫɜɨɢɦ ɤɥɢɟɧɬɚɦ ɧɟɢɡɦɟɧɧɵɟ, ɱɟɬɤɨ ɡɚɞɚɧɧɵɟ ɧɨɦɟɪɚ ɩɨɪɬɨɜ. Ȼɚɡɨɜɚɹ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɜɵɡɨɜɨɜ ɡɞɟɫɶ ɛɭɞɟɬ ɬɚɤɨɣ: socket(), bind(), listen(), accept() ɢ close(). ȼ ɬɨ ɜɪɟɦɹ ɤɚɤ ɤɥɢɟɧɬ ɫɨɡɞɚɟɬ ɚɤɬɢɜɧɨɟ ɫɨɟɞɢɧɟɧɢɟ, ɫɟɪɜɟɪɧɨɟ ɫɨɟɞɢɧɟɧɢɟ ɩɚɫɫɢɜɧɨ. Ɏɭɧɤɰɢɢ listen () ɢ accept () ɭɫɬɚɧɚɜɥɢɜɚɸɬ ɫɨɟɞɢɧɟɧɢɟ ɬɨɥɶɤɨ ɬɨɝɞɚ, ɤɨɝɞɚ ɩɪɢɯɨɞɢɬ ɡɚɩɪɨɫ ɨɬ ɤɥɢɟɧɬɚ. Ɂɧɚɤɨɦɫɬɜɨ ɫ ɮɭɧɤɰɢɟɣ bind() ɫɨɫɬɨɹɥɨɫɶ ɜɵɲɟ, ɫɟɣɱɚɫ ɨɧɚ ɛɭɞɟɬ ɨɩɢɫɚɧɚ ɛɨɥɟɟ ɮɨɪɦɚɥɶɧɨ. Ʉɪɨɦɟ ɬɨɝɨ, ɛɭɞɭɬ ɩɪɟɞɫɬɚɜɥɟɧɵ ɞɜɟ ɧɨɜɵɟ ɮɭɧɤɰɢɢ: listen () ɢ accept().
ñåðâåð
ê ëè åí ò
socket()
socket()
bind()
bind()
listen()
accept()
read()
connect()
write()
close()
read()
write()
close()
shutdown ()
2. А
,
ɉɪɟɠɞɟ ɱɟɦ ɩɟɪɟɣɬɢ ɤ ɪɚɫɫɦɨɬɪɟɧɢɸ ɫɢɫɬɟɦɧɵɯ ɮɭɧɤɰɢɣ, ɫɥɟɞɭɟɬ ɪɚɫɫɤɚɡɚɬɶ ɨ ɬɨɦ, ɤɚɤɨɝɨ ɪɨɞɚ ɫɟɪɜɟɪ ɦɵ ɛɭɞɟɦ ɫɨɡɞɚɜɚɬɶ. ȼ ɤɚɱɟɫɬɜɟ ɨɛɪɚɡɰɚ ɛɵɥ ɜɵɛɪɚɧ ɫɬɚɧɞɚɪɬɧɵɣ ɷɯɨ-ɫɟɪɜɟɪ. ɗɬɨ ɨɫɧɨɜɚ ɨɫɧɨɜ ɫɟɪɜɟɪɧɨɝɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɩɨɞɨɛɧɨ ɩɪɢɥɨɠɟɧɢɸ "Hello, World" ɜ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɢ ɧɚ ɹɡɵɤɟ ɋ. Ȼɨɥɶɲɢɧɫɬɜɨ ɫɨɟɞɢɧɟɧɢɣ ɦɨɠɧɨ ɩɪɨɜɟɪɢɬɶ, ɩɨɫɥɚɜ ɞɚɧɧɵɟ ɢ ɡɚɩɪɨɫɢɜ ɢɯ ɧɚɡɚɞ ɜ ɧɟɢɡɦɟɧɧɨɦ ɜɢɞɟ (ɷɯɨ). ɗɬɨ ɯɨɪɨɲɚɹ ɢɞɟɹ ɞɥɹ ɫɨɡɞɚɧɢɹ ɩɪɨɫɬɟɣɲɟɝɨ ɫɟɪɜɟɪɚ. Ⱥɧɚɥɨɝɢɱɧɵɦ ɨɛɪɚɡɨɦ ɩɢɲɭɬɫɹ ɢ ɨɬɥɚɠɢɜɚɸɬɫɹ ɞɚɠɟ ɫɚɦɵɟ ɫɥɨɠɧɵɟ ɩɪɢɥɨɠɟɧɢɹ.
П
я
. Э ,
.К
.П
(
-
) ,
ȼ ɨɛɳɟɦ ɫɥɭɱɚɟ ɜ ɫɟɪɜɟɪɧɨɣ ɩɪɨɝɪɚɦɦɟ ɬɪɟɛɭɟɬɫɹ ɜ ɨɩɪɟɞɟɥɟɧɧɨɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɜɵɡɜɚɬɶ ɪɹɞ ɫɢɫɬɟɦɧɵɯ ɮɭɧɤɰɢɣ. ɇɚ ɩɪɢɦɟɪɟ ɷɯɨ-ɫɟɪɜɟɪɚ ɦɨɠɧɨ ɧɚɝɥɹɞɧɨ ɭɜɢɞɟɬɶ ɷɬɭ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ, ɧɟ ɨɬɜɥɟɤɚɹɫɶ ɧɚ ɪɟɲɟɧɢɟ ɞɪɭɝɢɯ, ɛɨɥɟɟ ɫɩɟɰɢɮɢɱɟɫɤɢɯ ɡɚɞɚɱ. ɇɢɠɟ ɨɩɢɫɚɧ ɨɛɳɢɣ ɚɥɝɨɪɢɬɦ ɪɚɛɨɬɵ ɷɯɨɫɟɪɜɟɪɚ. 1.ɋɨɡɞɚɧɢɟ ɫɨɤɟɬɚ ɫ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɢ socket (). 2.ɉɪɢɜɹɡɤɚ ɤ ɩɨɪɬɭ ɫ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɢ bind(). 3.ɉɟɪɟɜɨɞ ɫɨɤɟɬɚ ɜ ɪɟɠɢɦ ɩɪɨɫɥɭɲɢɜɚɧɢɹ ɫ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɢ listen (). 4.ɉɪɨɜɟɪɤɚ ɩɨɞɤɥɸɱɟɧɢɹ ɫ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɢ accept (). 5.ɑɬɟɧɢɟ ɫɨɨɛɳɟɧɢɹ ɫ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɢ recv() ɢɥɢ read(). 6.ȼɨɡɜɪɚɬ ɫɨɨɛɳɟɧɢɹ ɤɥɢɟɧɬɭ ɫ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɢ send() ɢɥɢ write (). 7.ȿɫɥɢ ɩɨɥɭɱɟɧɧɨɟ ɫɨɨɛɳɟɧɢɟ ɧɟ ɹɜɥɹɟɬɫɹ ɫɬɪɨɤɨɣ "bye", ɜɨɡɜɪɚɬ ɤ ɩ. 5. 8.Ɋɚɡɪɵɜ ɫɨɟɞɢɧɟɧɢɹ ɫ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɢ close() ɢɥɢ shutdown(). 9.ȼɨɡɜɪɚɬ ɤ ɩ. 4. ȼ ɩɪɢɜɟɞɟɧɧɨɦ ɚɥɝɨɪɢɬɦɟ ɱɟɬɤɨ ɜɢɞɧɵ ɨɬɥɢɱɢɹ ɨɬ ɩɪɨɬɨɤɨɥɚ UDP ɢ ɞɪɭɝɢɯ ɩɪɨɬɨɤɨɥɨɜ, ɧɟ ɨɪɢɟɧɬɢɪɨɜɚɧɧɵɯ ɧɚ ɭɫɬɚɧɨɜɥɟɧɢɟ ɫɨɟɞɢɧɟɧɢɣ. Ɂɞɟɫɶ ɫɟɪɜɟɪ ɧɟ ɡɚɤɪɵɜɚɟɬ ɫɨɟɞɢɧɟɧɢɟ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɤɥɢɟɧɬ ɧɟ ɩɪɢɲɥɟɬ ɤɨɦɚɧɞɭ "bye". Ȼɥɚɝɨɞɚɪɹ ɚɥɝɨɪɢɬɦɭ ɫɬɚɧɨɜɢɬɫɹ ɩɨɧɹɬɧɨ, ɱɬɨ ɧɟɨɛɯɨɞɢɦɨ ɩɪɟɞɩɪɢɧɹɬɶ ɞɚɥɶɲɟ ɩɪɢ ɫɨɡɞɚɧɢɢ ɫɟɪɜɟɪɚ. ɉɟɪɜɵɣ ɨɱɟɜɢɞɧɵɣ ɲɚɝ (ɫɨɡɞɚɧɢɟ ɫɨɤɟɬɚ) ɪɚɫɫɦɚɬɪɢɜɚɥɫɹ ɜɵɲɟ. Ʉɚɤ ɭɠɟ ɭɩɨɦɢɧɚɥɨɫɶ, ɫ ɷɬɨɝɨ ɧɚɱɢɧɚɟɬɫɹ ɥɸɛɚɹ ɫɟɬɟɜɚɹ ɩɪɨɝɪɚɦɦɚ. ɋɥɟɞɭɸɳɢɣ ɲɚɝ — ɜɵɛɨɪ ɩɨɪɬɚ — ɨɛɹɡɚɬɟɥɟɧ ɞɥɹ ɫɟɪɜɟɪɚ.
Ɋɚɛɨɬɚ ɫ ɌɋɊ-ɫɨɤɟɬɚɦɢ ɧɚɱɢɧɚɟɬɫɹ ɫ ɜɵɡɨɜɚ ɮɭɧɤɰɢɢ socket (), ɤɨɬɨɪɨɣ ɩɟɪɟɞɚɟɬɫɹ ɤɨɧɫɬɚɧɬɚ SOCK_STREAM. ɇɨ ɬɟɩɟɪɶ ɬɪɟɛɭɟɬɫɹ ɡɚɞɚɬɶ ɬɚɤɠɟ ɧɨɦɟɪ ɩɨɪɬɚ, ɱɬɨɛɵ ɤɥɢɟɧɬ ɦɨɝ ɤ ɧɟɦɭ ɩɨɞɤɥɸɱɢɬɶɫɹ. Ɏɭɧɤɰɢɹ bind() ɫɩɪɚɲɢɜɚɟɬ ɭ ɨɩɟɪɚɰɢɨɧɧɨɣ ɫɢɫɬɟɦɵ, ɦɨɠɟɬ ɥɢ ɩɪɨɝɪɚɦɦɚ ɡɚɜɥɚɞɟɬɶ ɩɨɪɬɨɦ ɫ ɭɤɚɡɚɧɧɵɦ ɧɨɦɟɪɨɦ. ȿɫɥɢ ɫɟɪɜɟɪ ɧɟ ɭɤɚɡɵɜɚɟɬ ɩɨɪɬ, ɫɢɫɬɟɦɚ ɧɚɡɧɚɱɚɟɬ ɟɦɭ ɛɥɢɠɚɣɲɢɣ ɞɨɫɬɭɩɧɵɣ ɩɨɪɬ ɢɡ ɩɭɥɚ ɧɨɦɟɪɨɜ. ɗɬɨɬ ɧɨɦɟɪ ɦɨɠɟɬ ɛɵɬɶ ɪɚɡɧɵɦ ɩɪɢ ɤɚɠɞɨɦ ɫɥɟɞɭɸɳɟɦ ɡɚɩɭɫɤɟ ɩɪɨɝɪɚɦɦɵ. ȿɫɥɢ ɩɪɨɝɪɚɦɦɚ ɡɚɩɪɚɲɢɜɚɟɬ ɩɨɪɬ, ɧɨ ɧɟ ɩɨɥɭɱɚɟɬ ɟɝɨ, ɡɧɚɱɢɬ, ɫɟɪɜɟɪ ɭɠɟ ɜɵɩɨɥɧɹɟɬɫɹ. Ɉɩɟɪɚɰɢɨɧɧɚɹ ɫɢɫɬɟɦɚ ɫɜɹɡɵɜɚɟɬ ɩɨɪɬ ɬɨɥɶɤɨ ɫ ɨɞɧɢɦ ɩɪɨɰɟɫɫɨɦ. Ɉɛɴɹɜɥɟɧɢɟ ɮɭɧɤɰɢɢ bind() ɜɵɝɥɹɞɢɬ ɬɚɤ: #include #include int bind(int sd, struct sockaddr *addr, int addr size);
ɉɚɪɚɦɟɬɪ sd ɹɜɥɹɟɬɫɹ ɞɟɫɤɪɢɩɬɨɪɨɦ ɪɚɧɟɟ ɫɨɡɞɚɧɧɨɝɨ ɫɨɤɟɬɚ. ȼ ɩɚɪɚɦɟɬɪɟ addr ɩɟɪɟɞɚɟɬɫɹ ɫɬɪɭɤɬɭɪɚ ɫɟɦɟɣɫɬɜɚ sockaddr. ȼ ɧɟɣ ɭɤɚɡɵɜɚɟɬɫɹ ɫɟɦɟɣɫɬɜɨ ɩɪɨɬɨɤɨɥɨɜ, ɚɞɪɟɫ ɫɟɪɜɟɪɚ ɢ ɧɨɦɟɪ ɩɨɪɬɚ (ɫɦ. ɜɵɲɟ). ɉɨɫɥɟɞɧɢɣ ɩɚɪɚɦɟɬɪ ɫɨɞɟɪɠɢɬ ɪɚɡɦɟɪ ɫɬɪɭɤɬɭɪɵ sockaddr. ȿɝɨ ɧɟɨɛɯɨɞɢɦɨ ɡɚɞɚɜɚɬɶ, ɩɨɬɨɦɭ ɱɬɨ ɬɚɤɨɜɚ ɤɨɧɰɟɩɰɢɹ ɛɢɛɥɢɨɬɟɤɢ Socket API: ɨɞɢɧ ɢɧɬɟɪɮɟɣɫ, ɧɨ ɦɧɨɝɨ ɚɪɯɢɬɟɤɬɭɪ. Ɉɩɟɪɚɰɢɨɧɧɚɹ ɫɢɫɬɟɦɚ ɩɨɞɞɟɪɠɢɜɚɟɬ ɦɧɨɠɟɫɬɜɨ ɩɪɨɬɨɤɨɥɨɜ, ɭ ɤɚɠɞɨɝɨ ɢɡ ɤɨɬɨɪɵɯ ɫɜɨɹ ɚɞɪɟɫɧɚɹ ɫɬɪɭɤɬɭɪɚ. ɉɟɪɟɞ ɜɵɡɨɜɨɦ ɮɭɧɤɰɢɢ bind() ɧɟɨɛɯɨɞɢɦɨ ɡɚɩɨɥɧɢɬɶ ɩɨɥɹ ɫɬɪɭɤɬɭɪɵ sockaddr (Ʌɢɫɬɢɧɝ 3). 3. В
bind()
TCP-
/**************************************************************/ /*** : ***/ /*** sockaddr_in ***/ /**************************************************************/ struct sockaddr_in addr; /* */ bzero(&addr, sizeof(addr)); /* */ addr.sin_family = AF_INET; /* TCP/IP */ addr.sin_port = htons(MY_PORT); /* */ addr.sin_addr.s_addr = INADDR_ANY; /* IP*/ if ( bind(sd, &addr, sizeof(addr)) != 0 ) /* */ perror("Bind AF_INET");
ȼ ɫɥɟɞɭɸɳɟɦ ɮɪɚɝɦɟɧɬɟ ɩɪɨɝɪɚɦɦɵ (Ʌɢɫɬɢɧɝ 4) ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɢɧɢɰɢɚɥɢɡɚɰɢɹ ɢɦɟɧɨɜɚɧɧɨɝɨ ɫɨɤɟɬɚ (ɫɟɦɟɣɫɬɜɨ AF_UNIX ɢɥɢ AF_LOCAL). 4.В
bind()
****************************************************************/ /*** : ***/ /*** sockaddr_ux ***/ /****************************************************************/ #include struct sockaddr ux addr; /* */ bzero(&addr, sizeof(addr)); /* */ addr.sun family = AF LOCAL; /* */ strcpy(addr.sun_path, "/tmp/mysocket"); /* */ if ( bind(sd, saddr, sizeof(addr)) != 0 ) /* */ perror("Bind AF_LOCAL");
ȿɫɥɢ ɡɚɩɭɫɬɢɬɶ ɧɚ ɜɵɩɨɥɧɟɧɢɟ ɷɬɭ ɩɪɨɝɪɚɦɦɭ, ɬɨ ɩɨɫɥɟ ɟɟ ɡɚɜɟɪɲɟɧɢɹ ɜ ɤɚɬɚɥɨɝɟ /tmp ɩɨɹɜɢɬɫɹ ɮɚɣɥ mysocket. ɂɦɟɧɨɜɚɧɧɵɟ ɫɨɤɟɬɵ ɢɫɩɨɥɶɡɭɸɬɫɹ ɫɢɫɬɟɦɧɵɦ ɞɟɦɨɧɨɦ ɪɟɝɢɫɬɪɚɰɢɢ ɫɨɨɛɳɟɧɢɣ, syslogd, ɞɥɹ ɫɛɨɪɚ ɢɧɮɨɪɦɚɰɢɢ: ɫɢɫɬɟɦɧɵɟ ɩɪɨɰɟɫɫɵ ɭɫɬɚɧɚɜɥɢɜɚɸɬ ɫɨɟɞɢɧɟɧɢɟ ɫ ɫɨɤɟɬɨɦ ɞɟɦɨɧɚ ɢ ɩɨɫɵɥɚɸɬ ɜ ɧɟɝɨ ɫɨɨɛɳɟɧɢɹ. ȼ ɪɟɡɭɥɶɬɚɬɟ ɜɵɩɨɥɧɟɧɢɹ ɮɭɧɤɰɢɢ bind() ɦɨɝɭɬ ɜɨɡɧɢɤɧɭɬɶ ɩɟɪɟɱɢɫɥɟɧɧɵɟ ɧɢɠɟ ɨɲɢɛɤɢ.
•EBADF. ɍɤɚɡɚɧ ɧɟɜɟɪɧɵɣ ɞɟɫɤɪɢɩɬɨɪ ɫɨɤɟɬɚ. ɗɬɚ ɨɲɢɛɤɚ ɜɨɡɧɢɤɚɟɬ, ɟɫɥɢ ɜɵɡɨɜ ɮɭɧɤɰɢɢ socket () ɡɚɜɟɪɲɢɥɫɹ ɧɟɭɫɩɟɲɧɨ, ɚ ɩɪɨɝɪɚɦɦɚ ɧɟ ɩɪɨɜɟɪɢɥɚ ɤɨɞ ɟɟ ɡɚɜɟɪɲɟɧɢɹ.
•EACCES. Ɂɚɩɪɚɲɢɜɚɟɦɵɣ ɧɨɦɟɪ ɩɨɪɬɚ ɞɨɫɬɭɩɟɧ ɬɨɥɶɤɨ ɩɨɥɶɡɨɜɚɬɟɥɸ root. ɉɨɦɧɢɬɟ, ɱɬɨ ɞɥɹ ɞɨɫɬɭɩɚ ɤ ɩɨɪɬɚɦ ɫ ɧɨɦɟɪɚɦɢ 0—1023 ɩɪɨɝɪɚɦɦɚ ɞɨɥɠɧɚ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɢ ɩɨɥɶɡɨɜɚɬɟɥɹ root. ɉɨɞɪɨɛɧɟɟ ɨɛ ɷɬɨɦ ɪɚɫɫɤɚɡɵɜɚɥɨɫɶ ɜ ɝɥɚɜɟ 2, "Ɉɫɧɨɜɵ TCP/IP". •EINVAL. ɉɨɪɬ ɭɠɟ ɢɫɩɨɥɶɡɭɟɬɫɹ. ȼɨɡɦɨɠɧɨ, ɢɦ ɡɚɜɥɚɞɟɥɚ ɞɪɭɝɚɹ ɩɪɨɝɪɚɦɦɚ. ɗɬɚ ɨɲɢɛɤɚ ɦɨɠɟɬ ɬɚɤɠɟ ɜɨɡɧɢɤɧɭɬɶ, ɟɫɥɢ ɫɟɪɜɟɪ ɡɚɜɢɫ ɢ ɜɵ ɬɭɬ ɠɟ ɡɚɩɭɫɤɚɟɬɟ ɟɝɨ ɩɨɜɬɨɪɧɨ. Ⱦɥɹ ɨɩɟɪɚɰɢɨɧɧɨɣ ɫɢɫɬɟɦɵ ɬɪɟɛɭɟɬɫɹ ɜɪɟɦɹ, ɱɬɨɛɵ ɨɫɜɨɛɨɞɢɬɶ ɡɚɧɹɬɵɣ ɩɨɪɬ (ɞɨ ɩɹɬɢ ɦɢɧɭɬ!).
Ɏɭɧɤɰɢɹ bind() ɩɵɬɚɟɬɫɹ ɡɚɪɟɡɟɪɜɢɪɨɜɚɬɶ ɞɥɹ ɫɟɪɜɟɪɧɨɝɨ ɫɨɤɟɬɚ ɭɤɚɡɚɧɧɨɟ ɢɦɹ ɮɚɣɥɚ ɢɥɢ ɩɨɪɬ (ɫɩɢɫɨɤ ɞɨɫɬɭɩɧɵɯ ɢɥɢ ɫɬɚɧɞɚɪɬɧɵɯ ɩɨɪɬɨɜ ɫɨɞɟɪɠɢɬɫɹ ɜ ɮɚɣɥɟ /etc/services). Ʉɥɢɟɧɬɵ ɩɨɞɤɥɸɱɚɸɬɫɹ ɤ ɞɚɧɧɨɦɭ ɩɨɪɬɭ, ɩɨɫɵɥɚɹ ɢ ɩɪɢɧɢɦɚɹ ɱɟɪɟɡ ɧɟɝɨ ɞɚɧɧɵɟ.
ɋɨɤɟɬ ɨɛɟɫɩɟɱɢɜɚɟɬ ɢɧɬɟɪɮɟɣɫ, ɩɨɫɪɟɞɫɬɜɨɦ ɤɨɬɨɪɨɝɨ ɨɞɧɚ ɩɪɨɝɪɚɦɦɚ ɦɨɠɟɬ ɜɡɚɢɦɨɞɟɣɫɬɜɨɜɚɬɶ ɫ ɞɪɭɝɨɣ ɩɨ ɫɟɬɢ. ɋɨɟɞɢɧɟɧɢɟ ɹɜɥɹɟɬɫɹ ɷɤɫɤɥɸɡɢɜɧɵɦ: ɩɨɫɥɟ ɬɨɝɨ ɤɚɤ ɩɪɨɝɪɚɦɦɚ ɩɨɞɤɥɸɱɢɥɚɫɶ ɤ ɩɨɪɬɭ, ɧɢɤɚɤɚɹ ɞɪɭɝɚɹ ɩɪɨɝɪɚɦɦɚ ɧɟ ɦɨɠɟɬ ɤ ɧɟɦɭ ɨɛɪɚɬɢɬɶɫɹ. Ⱦɥɹ ɪɚɡɪɟɲɟɧɢɹ ɩɨɞɨɛɧɨɣ ɫɢɬɭɚɰɢɢ ɧɚ ɫɟɪɜɟɪɟ ɫɨɡɞɚɟɬɫɹ ɨɱɟɪɟɞɶ ɨɠɢɞɚɧɢɹ. Ɉɱɟɪɟɞɶ ɫɨɤɟɬɚ ɚɤɬɢɜɢɡɢɪɭɟɬɫɹ ɩɪɢ ɜɵɡɨɜɟ ɮɭɧɤɰɢɢ listen (). Ʉɨɝɞɚ ɫɟɪɜɟɪ ɜɵɡɵɜɚɟɬ ɷɬɭ ɮɭɧɤɰɢɸ, ɨɧ ɭɤɚɡɵɜɚɟɬ ɱɢɫɥɨ ɩɨɡɢɰɢɣ ɜ ɨɱɟɪɟɞɢ. Ʉɪɨɦɟ ɬɨɝɨ, ɫɨɤɟɬ ɩɟɪɟɜɨɞɢɬɫɹ ɜ ɪɟɠɢɦ "ɬɨɥɶɤɨ ɩɪɨɫɥɭɲɢɜɚɧɢɟ". ɗɬɨ ɨɱɟɧɶ ɜɚɠɧɨ, ɬɚɤ ɤɚɤ ɩɨɡɜɨɥɹɟɬ ɜɩɨɫɥɟɞɫɬɜɢɢ ɜɵɡɵɜɚɬɶ ɮɭɧɤɰɢɸ accept (). #include #include int listen(int sd, int numslots);
ɉɚɪɚɦɟɬɪ sd ɹɜɥɹɟɬɫɹ ɞɟɫɤɪɢɩɬɨɪɨɦ ɫɨɤɟɬɚ, ɩɨɥɭɱɟɧɧɵɦ ɜ ɪɟɡɭɥɶɬɚɬɟ ɜɵɡɨɜɚ ɮɭɧɤɰɢɢ socket (). ɉɚɪɚɦɟɬɪ numslots ɡɚɞɚɟɬ ɱɢɫɥɨ ɩɨɡɢɰɢɣ ɜ ɨɱɟɪɟɞɢ ɨɠɢɞɚɧɢɹ. ɉɪɢɜɟɞɟɦ ɩɪɢɦɟɪ (Ʌɢɫɬɢɧɝ 5). 5.
listen ()
/****************************************************************/ /*** listen(): ***/ /*** ***/ /****************************************************************/ int sd; sd = socket(PF_INET, SOCK_STREAM, 0); /*** ***/ if ( listen(sd, 20) != 0 ) /* */ perror("Listen"); /* 20*/
Ʉɚɤ ɩɪɚɜɢɥɨ, ɪɚɡɦɟɪ ɨɱɟɪɟɞɢ ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɪɚɜɧɵɦ ɨɬ 5 ɞɨ 20. Ȼɨɥɶɲɢɣ ɪɚɡɦɟɪ ɨɤɚɡɵɜɚɟɬɫɹ ɢɡɛɵɬɨɱɧɵɦ ɜ ɫɨɜɪɟɦɟɧɧɨɣ ɦɧɨɝɨɡɚɞɚɱɧɨɣ ɫɪɟɞɟ. ȿɫɥɢ ɦɧɨɝɨɡɚɞɚɱɧɵɣ ɪɟɠɢɦ ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ, ɦɨɠɟɬ ɩɨɬɪɟɛɨɜɚɬɶɫɹ ɭɜɟɥɢɱɢɬɶ ɪɚɡɦɟɪ ɨɱɟɪɟɞɢ ɞɨ ɜɟɥɢɱɢɧɵ ɩɟɪɢɨɞɚ ɬɚɣɦ-ɚɭɬɚ (ɧɚɩɪɢɦɟɪ, 60, ɟɫɥɢ ɬɚɣɦɚɭɬ ɫɨɫɬɚɜɥɹɟɬ 60 ɫɟɤɭɧɞ). Ɏɭɧɤɰɢɹ listen () ɦɨɠɟɬ ɝɟɧɟɪɢɪɨɜɚɬɶ ɫɥɟɞɭɸɳɢɟ ɨɲɢɛɤɢ. 1.EBADF. ɍɤɚɡɚɧ ɧɟɜɟɪɧɵɣ ɞɟɫɤɪɢɩɬɨɪ ɫɨɤɟɬɚ. 2.EOPNOTSUPP. ɉɪɨɬɨɤɨɥ ɫɨɤɟɬɚ ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬ ɮɭɧɤɰɢɸ listen (). ȼ TCP (SOCK_STREAM) ɨɱɟɪɟɞɶ ɨɠɢɞɚɧɢɹ ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ, ɚ ɜ ɩɪɨɬɨɤɨɥɟ UDP (SOCK_DGRAM) — . ɉɨɫɥɟ ɩɟɪɟɜɨɞɚ ɫɨɤɟɬɚ ɜ ɪɟɠɢɦ ɨɠɢɞɚɧɢɹ ɧɟɨɛɯɨɞɢɦɨ ɨɪɝɚɧɢɡɨɜɚɬɶ ɰɢɤɥ ɩɨɥɭɱɟɧɢɹ ɡɚɩɪɨɫɨɜ ɧɚ ɩɨɞɤɥɸɱɟɧɢɟ.
ɇɚ ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɩɪɨɝɪɚɦɦɚ ɫɨɡɞɚɥɚ ɫɨɤɟɬ, ɧɚɡɧɚɱɢɥɚ ɟɦɭ ɧɨɦɟɪ ɩɨɪɬɚ ɢ ɨɪɝɚɧɢɡɨɜɚɥɚ ɨɱɟɪɟɞɶ ɨɠɢɞɚɧɢɹ. Ɍɟɩɟɪɶ ɨɧɚ ɦɨɠɟɬ ɩɪɢɧɢɦɚɬɶ ɡɚɩɪɨɫɵ ɧɚ ɩɨɞɤɥɸɱɟɧɢɟ. Ɏɭɧɤɰɢɹ accept() ɞɟɥɚɟɬ ɭɤɚɡɚɧɧɵɣ ɫɨɤɟɬ ɞɢɫɩɟɬɱɟɪɨɦ ɫɨɟɞɢɧɟɧɢɣ. Ɂɞɟɫɶ ɩɪɢɜɵɱɧɵɣ ɯɨɞ ɫɨɛɵɬɢɣ ɧɚɪɭɲɚɟɬɫɹ. Ʉɨɝɞɚ ɫɨɤɟɬ ɩɟɪɟɜɨɞɢɬɫɹ ɜ ɪɟɠɢɦ ɩɪɨɫɥɭɲɢɜɚɧɢɹ, ɨɧ ɩɟɪɟɫɬɚɟɬ ɛɵɬɶ ɞɜɭɧɚɩɪɚɜɥɟɧɧɵɦ ɤɚɧɚɥɨɦ ɩɟɪɟɞɚɱɢ ɞɚɧɧɵɯ. ɉɪɨɮɚɦɦɚ ɧɟ ɦɨɠɟɬ ɞɚɠɟ ɱɢɬɚɬɶ ɞɚɧɧɵɟ ɢɡ ɧɟɝɨ. Ɉɧɚ ɦɨɠɟɬ ɬɨɥɶɤɨ ɩɪɢɧɢɦɚɬɶ ɡɚɩɪɨɫɵ ɧɚ ɩɨɞɤɥɸɱɟɧɢɟ. Ɏɭɧɤɰɢɹ accept () ɩɪɨɝɪɚɦɦɭ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɧɟ ɩɨɫɬɭɩɢɬ ɬɚɤɨɣ ɡɚɩɪɨɫ. Ʉɨɝɞɚ ɤɥɢɟɧɬ ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɫɨɟɞɢɧɟɧɢɟ ɫ ɫɟɪɜɟɪɨɦ, ɫɨɤɟɬ, ɧɚɯɨɞɹɳɢɣɫɹ ɜ ɪɟɠɢɦɟ ɩɪɨɫɥɭɲɢɜɚɧɢɹ, ɨɪɝɚɧɢɡɭɟɬ ɧɨɜɵɣ ɞɜɭɧɚɩɪɚɜɥɟɧɧɵɣ ɤɚɧɚɥ ɦɟɠɞɭ ɤɥɢɟɧɬɨɦ ɢ ɫɜɨɢɦ ɫɨɛɫɬɜɟɧɧɵɦ ɩɨɪɬɨɦ. Ɏɭɧɤɰɢɹ accept() ɧɟɹɜɧɨ ɫɨɡɞɚɟɬ ɜ ɩɪɨɝɪɚɦɦɟ ɧɨɜɵɣ ɞɟɫɤɪɢɩɬɨɪ ɫɨɤɟɬɚ. ɉɨ ɫɭɬɢ, ɩɪɢ ɤɚɠɞɨɦ ɧɨɜɨɦ ɩɨɞɤɥɸɱɟɧɢɢ ɫɨɡɞɚɟɬɫɹ ɜɵɞɟɥɟɧɧɵɣ ɤɚɧɚɥ ɦɟɠɞɭ ɤɥɢɟɧɬɨɦ ɢ ɫɟɪɜɟɪɨɦ. ɋ ɷɬɨɝɨ ɦɨɦɟɧɬɚ ɩɪɨɝɪɚɦɦɚ ɜɡɚɢɦɨɞɟɣɫɬɜɭɟɬ ɫ ɤɥɢɟɧɬɨɦ ɱɟɪɟɡ ɧɨɜɵɣ ɤɚɧɚɥ. Ɇɨɠɧɨ ɬɚɤɠɟ ɭɡɧɚɬɶ, ɤɬɨ ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɫɨɟɞɢɧɟɧɢɟ ɫ ɫɟɪɜɟɪɨɦ, ɩɨɫɤɨɥɶɤɭ ɜ ɮɭɧɤɰɢɸ accept() ɩɟɪɟɞɚɟɬɫɹ ɢɧɮɨɪɦɚɰɢɹ ɨ ɤɥɢɟɧɬɟ. Ⱥɧɚɥɨɝɢɱɧɵɣ ɩɪɨɰɟɫɫ ɪɚɫɫɦɚɬɪɢɜɚɥɫɹ ɜ ɝɥɚɜɟ 4, "ɉɟɪɟɞɚɱɚ ɫɨɨɛɳɟɧɢɣ ɦɟɠɞɭ ɨɞɧɨɪɚɧɝɨɜɵɦɢ ɤɨɦɩɶɸɬɟɪɚɦɢ", ɤɨɝɞɚ ɮɭɧɤɰɢɹ recvfrom() ɩɨɥɭɱɚɥɚ ɧɟ ɬɨɥɶɤɨ ɞɚɧɧɵɟ, ɧɨ ɢ ɭɤɚɡɚɬɟɥɶ ɧɚ ɚɞɪɟɫ ɨɬɩɪɚɜɢɬɟɥɹ. #include #include int accept(int sd, sockaddr *addr, int *addr size);
Ʉɚɤ ɜɫɟɝɞɚ, ɩɚɪɚɦɟɬɪ sd ɹɜɥɹɟɬɫɹ ɞɟɫɤɪɢɩɬɨɪɨɦ ɫɨɤɟɬɚ. ȼɨ ɜɬɨɪɨɦ ɩɚɪɚɦɟɬɪɟ ɜɨɡɜɪɚɳɚɟɬɫɹ ɚɞɪɟɫ ɤɥɢɟɧɬɚ ɢ ɧɨɦɟɪ ɩɨɪɬɚ, ɚ ɜ ɬɪɟɬɶɟɦ — ɪɚɡɦɟɪ ɫɬɪɭɤɬɭɪɵ sockaddr. ȼ ɨɬɥɢɱɢɟ ɨɬ ɮɭɧɤɰɢɢ recvfrom(), ɩɨ-
ɫɥɟɞɧɢɟ ɞɜɚ ɩɚɪɚɦɟɬɪɚ ɹɜɥɹɸɬɫɹ ɧɟɨɛɹɡɚɬɟɥɶɧɵɦɢ. ȿɫɥɢ ɜ ɩɪɨɝɪɚɦɦɟ ɧɟ ɬɪɟɛɭɟɬɫɹ ɡɧɚɬɶ ɚɞɪɟɫ ɤɥɢɟɧɬɚ, ɡɚɞɚɣɬɟ ɷɬɢ ɩɚɪɚɦɟɬɪɵ ɪɚɜɧɵɦɢ ɧɭɥɸ. ɇɟɨɛɯɨɞɢɦɨ ɭɛɟɞɢɬɶɫɹ, ɱɬɨ ɪɚɡɦɟɪ ɛɭɮɟɪɚ ɚɞɪɟɫɚ ɞɨɫɬɚɬɨɱɟɧ ɞɥɹ ɪɚɡɦɟɳɟɧɢɹ ɜ ɧɟɦ ɩɨɥɭɱɟɧɧɨɣ ɚɞɪɟɫɧɨɣ ɫɬɪɭɤɬɭɪɵ. Ȼɟɫɩɨɤɨɢɬɶɫɹ ɨ ɩɨɜɪɟɠɞɟɧɢɢ ɞɚɧɧɵɯ ɢɡ-ɡɚ ɩɟɪɟɩɨɥɧɟɧɢɹ ɛɭɮɟɪɚ ɧɟ ɫɬɨɢɬ: ɮɭɧɤɰɢɹ ɡɚɞɟɣɫɬɜɭɟɬ ɪɨɜɧɨ ɫɬɨɥɶɤɨ ɛɚɣɬɨɜ, ɫɤɨɥɶɤɨ ɭɤɚɡɚɧɨ ɜ ɬɪɟɬɶɟɦ ɩɚɪɚɦɟɬɪɟ. ɉɚɪɚɦɟɬɪ addr size ɩɟɪɟɞɚɟɬɫɹ ɩɨ ɫɫɵɥɤɟ, ɩɨɷɬɨɦɭ ɩɪɨɝɪɚɦɦɚ ɦɨɠɟɬ ɥɟɝɤɨ ɭɡɧɚɬɶ ɪɟɚɥɶɧɵɣ ɪɚɡɦɟɪ ɩɨɥɭɱɟɧɧɨɣ ɫɬɪɭɤɬɭɪɵ (ɥɢɫɬɢɧɝ 6.4). 6.
accept ().
/***************************************************************/ /*** accept(): ***/ /*** ***/ /***************************************************************/ int sd; struct sockaddr in addr; /*** , ***/ for (;;) /* */ { int clientsd; /* */ int size = sizeof(addr); /* */ clientsd = accept(sd, saddr, &size); /* if ( clientsd > 0 ) { /*** ***/ close(clientsd); /* } else perror ("Accept");
*/ */
/*
*/ /*
*/
В Ɉɛɪɚɬɢɬɟ ɜɧɢɦɚɧɢɟ ɧɚ ɬɨ, ɱɬɨ ɜ ɩɪɢɜɟɞɟɧɧɨɦ ɜɵɲɟ ɮɪɚɝɦɟɧɬɟ ɩɪɨɝɪɚɦɦɵ ɡɚɤɪɵɜɚɥɫɹ ɞɟɫɤɪɢɩɬɨɪ clientsd, ɤɨɬɨɪɵɣ ɨɬɥɢɱɚɟɬɫɹ ɨɬ ɨɫɧɨɜɧɨɝɨ ɞɟɫɤɪɢɩɬɨɪɚ ɫɨɤɟɬɚ. ɗɬɨ ɨɱɟɧɶ ɜɚɠɧɵɣ ɦɨɦɟɧɬ, ɩɨɫɤɨɥɶɤɭ ɞɥɹ ɤɚɠɞɨɝɨ ɫɨɟɞɢɧɟɧɢɹ ɫɨɡɞɚɟɬɫɹ ɨɬɞɟɥɶɧɵɣ ɞɟɫɤɪɢɩɬɨɪ. ȿɫɥɢ ɡɚɛɵɬɶ ɢɯ ɡɚɤɪɵɬɶ, ɥɢɦɢɬ ɞɟɫɤɪɢɩɬɨɪɨɜ ɦɨɠɟɬ ɫɨ ɜɪɟɦɟɧɟɦ ɢɫɱɟɪɩɚɬɶɫɹ. П accept () bind(). П .
, bind()
ɉɨɦɧɢɬɟ, ɱɬɨ ɛɨɥɶɲɢɧɫɬɜɨ ɩɨɥɟɣ ɫɬɪɭɤɬɭɪɵ ɢɦɟɟɬ ɫɟɬɟɜɨɣ ɩɨɪɹɞɨɤ ɫɥɟɞɨɜɚɧɢɹ ɛɚɣɬɨɜ. ɂɡɜɥɟɱɶ ɚɞɪɟɫ ɢ ɧɨɦɟɪ ɩɨɪɬɚ ɢɡ ɩɟɪɟɦɟɧɧɨɣ addr ɦɨɠɧɨ ɫ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɣ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ (Ʌɢɫɬɢɧɝ 7).
7.
accept ()
.
/****************************************************************/ /*** accept(): ***/ /*** ***/ /****************************************************************/ /*** ( ) ***/ client = accept(sd, &addr, &size); if ( client > 0 ) { if ( addr.sin_faaily == AF_INET) printf("Connection[%s]: %s:%d\n", /* */ ctime(tiine(0)), /* */ ntoa(addr.sin_addr), ntohs(addr.sin_port)); /*-----*/
ȿɫɥɢ ɜ ɩɪɨɰɟɫɫɟ ɜɵɩɨɥɧɟɧɢɹ ɮɭɧɤɰɢɢ accept () ɩɪɨɢɫɯɨɞɢɬ ɨɲɢɛɤɚ, ɮɭɧɤɰɢɹ ɜɨɡɜɪɚɳɚɟɬ ɨɬɪɢɰɚɬɟɥɶɧɨɟ ɡɧɚɱɟɧɢɟ. ȼ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ ɫɨɡɞɚɟɬɫɹ ɧɨɜɵɣ ɞɟɫɤɪɢɩɬɨɪ ɫɨɤɟɬɚ. ɇɢɠɟ ɩɟɪɟɱɢɫɥɟɧɵ ɤɨɞɵ ɜɨɡɦɨɠɧɵɯ ɨɲɢɛɨɤ. •EBADF. ɍɤɚɡɚɧ ɧɟɜɟɪɧɵɣ ɞɟɫɤɪɢɩɬɨɪ ɫɨɤɟɬɚ. •EOPNOTSOPP. ɉɪɢ ɜɵɡɨɜɟ ɮɭɧɤɰɢɢ accept () ɫɨɤɟɬ ɞɨɥɠɟɧ ɢɦɟɬɶ ɬɢɩ SOCK_STREAM. •EAGAIN. ɋɨɤɟɬ ɧɚɯɨɞɢɬɫɹ ɜ ɪɟɠɢɦɟ ɧɟɛɥɨɤɢɪɭɟɦɨɝɨ ɜɜɨɞɚ-ɜɵɜɨɞɚ, ɚ ɨɱɟɪɟɞɶ ɨɠɢɞɚɧɢɹ ɩɭɫɬɚ. Ɏɭɧɤɰɢɹ accept () ɛɥɨɤɢɪɭɟɬ ɪɚɛɨɬɭ ɩɪɨɝɪɚɦɦɵ, ɟɫɥɢ ɧɟ ɜɤɥɸɱɟɧ ɞɚɧɧɵɣ ɪɟɠɢɦ.
ɇɚɫɬɚɥɨ ɜɪɟɦɹ ɜɟɪɧɭɬɶɫɹ ɤ ɷɯɨ-ɫɟɪɜɟɪɭ, ɤɨɬɨɪɵɣ ɜɨɡɜɪɚɳɚɟɬ ɤɥɢɟɧɬɭ ɩɨɥɭɱɟɧɧɨɟ ɫɨɨɛɳɟɧɢɟ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɧɟ ɩɨɫɬɭɩɢɬ ɤɨɦɚɧɞɚ bye (Ʌɢɫɬɢɧɝ 8).
8.
-
.
/****************************************************************/ /*** : ***/ /*** , "bye" ***/ /****************************************************************/ /*** ( accept()) ***/ if ( client > 0 ) { char buffer[1024]; int nbytes; do { nbytes = recv(client, buffer, sizeof(buffer), 0); if ( nbytes > 0 ) /* , send(client, buffer, nbytes, 0); } while ( nbytes > 0 && strncmp("bye\r", buffer, 4) != 0); close(client);
*/
} Ɂɚɦɟɬɶɬɟ, ɱɬɨ ɩɪɢɡɧɚɤɨɦ ɨɤɨɧɱɚɧɢɹ ɫɟɚɧɫɚ ɹɜɥɹɟɬɫɹ ɫɬɪɨɤɚ "bye\r", ɚ ɧɟ "bye\n". ȼ ɨɛɳɟɦ ɫɥɭɱɚɟ ɷɬɨ ɡɚɜɢɫɢɬ ɨɬ ɬɨɝɨ, ɤɚɤ ɜɵɩɨɥɧɹɟɬɫɹ ɨɛɪɚɛɨɬɤɚ ɜɯɨɞɧɨɝɨ ɩɨɬɨɤɚ. ɂɡ ɫɨɨɛɪɚɠɟɧɢɣ ɧɚɞɟɠɧɨɫɬɢ ɫɥɟɞɭɟɬ ɩɪɨɜɟɪɹɬɶ ɨɛɚ ɫɥɭɱɚɹ. ɉɨɩɪɨɛɭɣɬɟ ɩɪɨɬɟɫɬɢɪɨɜɚɬɶ ɞɚɧɧɭɸ ɩɪɨɝɪɚɦɦɭ, ɢɫɩɨɥɶɡɨɜɚɜ ɜ ɤɚɱɟɫɬɜɟ ɤɥɢɟɧɬɚ ɭɬɢɥɢɬɭ Telnet.
С
е
1.Ɇɢɥɨɫɥɚɜɫɤɚɹ ɇ.Ƚ. ɢ ɞɪ., «ɂɧɬɪɚɫɟɬɢ: ɞɨɫɬɭɩ ɜ Internet, ɡɚɳɢɬɚ. ɍɱɟɛɧɨɟ ɩɨɫɨɛɢɟ ɞɥɹ ȼɍɁɨɜ»., ɘɇɂɌɂ 1999ɝ., ISBN: 5-238-00134-7 2.ɒɨɧ ɍɨɥɬɨɧ, "ɋɨɡɞɚɧɢɟ ɫɟɬɟɜɵɯ ɩɪɢɥɨɠɟɧɢɣ ɜ ɫɪɟɞɟ Ʌɢɧɭɤɫ", ɂɡɞɚɬɟɥɶɫɤɢɣ ɞɨɦ "ȼɢɥɶɹɦɫ" 2001, ISBN 5-8459-01930-6 3.Ɍɟɪɟɧɫ ɑɚɧ "ɋɢɫɬɟɦɧɨɟ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɟ ɧɚ C++ ɞɥɹ UNIX", BHV, ISBN-09130331562-2 4.Ȼɭɪɤ Ɋ. ɢ ɞɪ., «Unix ɞɥɹ Internet. ɗɧɰɢɤɥɨɩɟɞɢɹ ɩɨɥɶɡɨɜɚɬɟɥɹ + CD», Ⱦɢɚɋɨɮɬ 1999 ɝ., ISBN: 966-7393-34-8 5.Ɋɨɛɚɱɟɜɫɤɢɣ Ⱥ., «Ɉɩɟɪɚɰɢɨɧɧɚɹ ɫɢɫɬɟɦɚ UNIX», BHV-ɋɚɧɤɬ-ɉɟɬɟɪɛɭɪɝ 1999ɝ.