boost Asio网络编程简介

.markdown-preview:not([data-use-github-style]) { padding: 2em; font-size: 1.2em; color: rgb(171, 178, 191); background-color: rgb(40, 44, 52); overflow: auto }
.markdown-preview:not([data-use-github-style])>:first-child { margin-top: 0px }
.markdown-preview:not([data-use-github-style]) h1,.markdown-preview:not([data-use-github-style]) h2,.markdown-preview:not([data-use-github-style]) h3,.markdown-preview:not([data-use-github-style]) h4,.markdown-preview:not([data-use-github-style]) h5,.markdown-preview:not([data-use-github-style]) h6 { line-height: 1.2; margin-top: 1.5em; margin-bottom: 0.5em; color: rgb(255, 255, 255) }
.markdown-preview:not([data-use-github-style]) h1 { font-size: 2.4em; font-weight: 300 }
.markdown-preview:not([data-use-github-style]) h2 { font-size: 1.8em; font-weight: 400 }
.markdown-preview:not([data-use-github-style]) h3 { font-size: 1.5em; font-weight: 500 }
.markdown-preview:not([data-use-github-style]) h4 { font-size: 1.2em; font-weight: 600 }
.markdown-preview:not([data-use-github-style]) h5 { font-size: 1.1em; font-weight: 600 }
.markdown-preview:not([data-use-github-style]) h6 { font-size: 1em; font-weight: 600 }
.markdown-preview:not([data-use-github-style]) strong { color: rgb(255, 255, 255) }
.markdown-preview:not([data-use-github-style]) del { color: rgb(124, 135, 156) }
.markdown-preview:not([data-use-github-style]) a,.markdown-preview:not([data-use-github-style]) a code { color: rgb(82, 139, 255) }
.markdown-preview:not([data-use-github-style]) img { max-width: 100% }
.markdown-preview:not([data-use-github-style])>p { margin-top: 0px; margin-bottom: 1.5em }
.markdown-preview:not([data-use-github-style])>ul,.markdown-preview:not([data-use-github-style])>ol { margin-bottom: 1.5em }
.markdown-preview:not([data-use-github-style]) blockquote { margin: 1.5em 0px; font-size: inherit; color: rgb(124, 135, 156); border-color: rgb(75, 83, 98); border-width: 4px }
.markdown-preview:not([data-use-github-style]) hr { margin: 3em 0px; border-top: 2px dashed rgb(75, 83, 98); background: none }
.markdown-preview:not([data-use-github-style]) table { margin: 1.5em 0px }
.markdown-preview:not([data-use-github-style]) th { color: rgb(255, 255, 255) }
.markdown-preview:not([data-use-github-style]) th,.markdown-preview:not([data-use-github-style]) td { padding: 0.66em 1em; border: 1px solid rgb(75, 83, 98) }
.markdown-preview:not([data-use-github-style]) code { color: rgb(255, 255, 255); background-color: rgb(58, 63, 75) }
.markdown-preview:not([data-use-github-style]) pre.editor-colors { margin: 1.5em 0px; padding: 1em; font-size: 0.92em; background-color: rgb(49, 54, 63) }
.markdown-preview:not([data-use-github-style]) kbd { color: rgb(255, 255, 255); border-width: 1px 1px 2px; border-style: solid; border-color: rgb(75, 83, 98) rgb(75, 83, 98) rgb(62, 68, 81); background-color: rgb(58, 63, 75) }
.markdown-preview[data-use-github-style] { font-family: "Helvetica Neue", Helvetica, "Segoe UI", Arial, freesans, sans-serif; line-height: 1.6; padding: 30px; font-size: 16px; color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); overflow: scroll }
.markdown-preview[data-use-github-style]>:first-child { margin-top: 0px !important }
.markdown-preview[data-use-github-style]>:last-child { margin-bottom: 0px !important }
.markdown-preview[data-use-github-style] a:not([href]) { color: inherit; text-decoration: none }
.markdown-preview[data-use-github-style] .absent { color: rgb(204, 0, 0) }
.markdown-preview[data-use-github-style] .anchor { position: absolute; top: 0px; left: 0px; display: block; padding-right: 6px; padding-left: 30px; margin-left: -30px }
.markdown-preview[data-use-github-style] .anchor:focus { outline: none }
.markdown-preview[data-use-github-style] h1,.markdown-preview[data-use-github-style] h2,.markdown-preview[data-use-github-style] h3,.markdown-preview[data-use-github-style] h4,.markdown-preview[data-use-github-style] h5,.markdown-preview[data-use-github-style] h6 { position: relative; margin-top: 1em; margin-bottom: 16px; font-weight: bold; line-height: 1.4 }
.markdown-preview[data-use-github-style] h1 .octicon-link,.markdown-preview[data-use-github-style] h2 .octicon-link,.markdown-preview[data-use-github-style] h3 .octicon-link,.markdown-preview[data-use-github-style] h4 .octicon-link,.markdown-preview[data-use-github-style] h5 .octicon-link,.markdown-preview[data-use-github-style] h6 .octicon-link { display: none; color: rgb(0, 0, 0); vertical-align: middle }
.markdown-preview[data-use-github-style] h1:hover .anchor,.markdown-preview[data-use-github-style] h2:hover .anchor,.markdown-preview[data-use-github-style] h3:hover .anchor,.markdown-preview[data-use-github-style] h4:hover .anchor,.markdown-preview[data-use-github-style] h5:hover .anchor,.markdown-preview[data-use-github-style] h6:hover .anchor { padding-left: 8px; margin-left: -30px; text-decoration: none }
.markdown-preview[data-use-github-style] h1:hover .anchor .octicon-link,.markdown-preview[data-use-github-style] h2:hover .anchor .octicon-link,.markdown-preview[data-use-github-style] h3:hover .anchor .octicon-link,.markdown-preview[data-use-github-style] h4:hover .anchor .octicon-link,.markdown-preview[data-use-github-style] h5:hover .anchor .octicon-link,.markdown-preview[data-use-github-style] h6:hover .anchor .octicon-link { display: inline-block }
.markdown-preview[data-use-github-style] h1 tt,.markdown-preview[data-use-github-style] h2 tt,.markdown-preview[data-use-github-style] h3 tt,.markdown-preview[data-use-github-style] h4 tt,.markdown-preview[data-use-github-style] h5 tt,.markdown-preview[data-use-github-style] h6 tt,.markdown-preview[data-use-github-style] h1 code,.markdown-preview[data-use-github-style] h2 code,.markdown-preview[data-use-github-style] h3 code,.markdown-preview[data-use-github-style] h4 code,.markdown-preview[data-use-github-style] h5 code,.markdown-preview[data-use-github-style] h6 code { font-size: inherit }
.markdown-preview[data-use-github-style] h1 { padding-bottom: 0.3em; font-size: 2.25em; line-height: 1.2; border-bottom: 1px solid rgb(238, 238, 238) }
.markdown-preview[data-use-github-style] h1 .anchor { line-height: 1 }
.markdown-preview[data-use-github-style] h2 { padding-bottom: 0.3em; font-size: 1.75em; line-height: 1.225; border-bottom: 1px solid rgb(238, 238, 238) }
.markdown-preview[data-use-github-style] h2 .anchor { line-height: 1 }
.markdown-preview[data-use-github-style] h3 { font-size: 1.5em; line-height: 1.43 }
.markdown-preview[data-use-github-style] h3 .anchor { line-height: 1.2 }
.markdown-preview[data-use-github-style] h4 { font-size: 1.25em }
.markdown-preview[data-use-github-style] h4 .anchor { line-height: 1.2 }
.markdown-preview[data-use-github-style] h5 { font-size: 1em }
.markdown-preview[data-use-github-style] h5 .anchor { line-height: 1.1 }
.markdown-preview[data-use-github-style] h6 { font-size: 1em; color: rgb(119, 119, 119) }
.markdown-preview[data-use-github-style] h6 .anchor { line-height: 1.1 }
.markdown-preview[data-use-github-style] p,.markdown-preview[data-use-github-style] blockquote,.markdown-preview[data-use-github-style] ul,.markdown-preview[data-use-github-style] ol,.markdown-preview[data-use-github-style] dl,.markdown-preview[data-use-github-style] table,.markdown-preview[data-use-github-style] pre { margin-top: 0px; margin-bottom: 16px }
.markdown-preview[data-use-github-style] hr { height: 4px; padding: 0px; margin: 16px 0px; background-color: rgb(231, 231, 231); border: 0px none }
.markdown-preview[data-use-github-style] ul,.markdown-preview[data-use-github-style] ol { padding-left: 2em }
.markdown-preview[data-use-github-style] ul.no-list,.markdown-preview[data-use-github-style] ol.no-list { padding: 0px; list-style-type: none }
.markdown-preview[data-use-github-style] ul ul,.markdown-preview[data-use-github-style] ul ol,.markdown-preview[data-use-github-style] ol ol,.markdown-preview[data-use-github-style] ol ul { margin-top: 0px; margin-bottom: 0px }
.markdown-preview[data-use-github-style] li>p { margin-top: 16px }
.markdown-preview[data-use-github-style] dl { padding: 0px }
.markdown-preview[data-use-github-style] dl dt { padding: 0px; margin-top: 16px; font-size: 1em; font-style: italic; font-weight: bold }
.markdown-preview[data-use-github-style] dl dd { padding: 0px 16px; margin-bottom: 16px }
.markdown-preview[data-use-github-style] blockquote { padding: 0px 15px; color: rgb(119, 119, 119); border-left: 4px solid rgb(221, 221, 221) }
.markdown-preview[data-use-github-style] blockquote>:first-child { margin-top: 0px }
.markdown-preview[data-use-github-style] blockquote>:last-child { margin-bottom: 0px }
.markdown-preview[data-use-github-style] table { display: block; width: 100%; overflow: auto }
.markdown-preview[data-use-github-style] table th { font-weight: bold }
.markdown-preview[data-use-github-style] table th,.markdown-preview[data-use-github-style] table td { padding: 6px 13px; border: 1px solid rgb(221, 221, 221) }
.markdown-preview[data-use-github-style] table tr { background-color: rgb(255, 255, 255); border-top: 1px solid rgb(204, 204, 204) }
.markdown-preview[data-use-github-style] table tr:nth-child(2n) { background-color: rgb(248, 248, 248) }
.markdown-preview[data-use-github-style] img { max-width: 100% }
.markdown-preview[data-use-github-style] .emoji { max-width: none }
.markdown-preview[data-use-github-style] span.frame { display: block; overflow: hidden }
.markdown-preview[data-use-github-style] span.frame>span { display: block; float: left; width: auto; padding: 7px; margin: 13px 0px 0px; overflow: hidden; border: 1px solid rgb(221, 221, 221) }
.markdown-preview[data-use-github-style] span.frame span img { display: block; float: left }
.markdown-preview[data-use-github-style] span.frame span span { display: block; padding: 5px 0px 0px; clear: both; color: rgb(51, 51, 51) }
.markdown-preview[data-use-github-style] span.align-center { display: block; overflow: hidden; clear: both }
.markdown-preview[data-use-github-style] span.align-center>span { display: block; margin: 13px auto 0px; overflow: hidden; text-align: center }
.markdown-preview[data-use-github-style] span.align-center span img { margin: 0px auto; text-align: center }
.markdown-preview[data-use-github-style] span.align-right { display: block; overflow: hidden; clear: both }
.markdown-preview[data-use-github-style] span.align-right>span { display: block; margin: 13px 0px 0px; overflow: hidden; text-align: right }
.markdown-preview[data-use-github-style] span.align-right span img { margin: 0px; text-align: right }
.markdown-preview[data-use-github-style] span.float-left { display: block; float: left; margin-right: 13px; overflow: hidden }
.markdown-preview[data-use-github-style] span.float-left span { margin: 13px 0px 0px }
.markdown-preview[data-use-github-style] span.float-right { display: block; float: right; margin-left: 13px; overflow: hidden }
.markdown-preview[data-use-github-style] span.float-right>span { display: block; margin: 13px auto 0px; overflow: hidden; text-align: right }
.markdown-preview[data-use-github-style] code,.markdown-preview[data-use-github-style] tt { padding: 0.2em 0px; margin: 0px; font-size: 85%; background-color: rgba(0, 0, 0, 0.0392157) }
.markdown-preview[data-use-github-style] code::before,.markdown-preview[data-use-github-style] tt::before,.markdown-preview[data-use-github-style] code::after,.markdown-preview[data-use-github-style] tt::after { letter-spacing: -0.2em; content: "?" }
.markdown-preview[data-use-github-style] code br,.markdown-preview[data-use-github-style] tt br { display: none }
.markdown-preview[data-use-github-style] del code { text-decoration: inherit }
.markdown-preview[data-use-github-style] pre>code { padding: 0px; margin: 0px; font-size: 100%; white-space: pre; background: transparent; border: 0px }
.markdown-preview[data-use-github-style] .highlight { margin-bottom: 16px }
.markdown-preview[data-use-github-style] .highlight pre,.markdown-preview[data-use-github-style] pre { padding: 16px; overflow: auto; font-size: 85%; line-height: 1.45; background-color: rgb(247, 247, 247) }
.markdown-preview[data-use-github-style] .highlight pre { margin-bottom: 0px }
.markdown-preview[data-use-github-style] pre { }
.markdown-preview[data-use-github-style] pre code,.markdown-preview[data-use-github-style] pre tt { display: inline; max-width: initial; padding: 0px; margin: 0px; overflow: initial; line-height: inherit; background-color: transparent; border: 0px }
.markdown-preview[data-use-github-style] pre code::before,.markdown-preview[data-use-github-style] pre tt::before,.markdown-preview[data-use-github-style] pre code::after,.markdown-preview[data-use-github-style] pre tt::after { content: normal }
.markdown-preview[data-use-github-style] kbd { display: inline-block; padding: 3px 5px; font-size: 11px; line-height: 10px; color: rgb(85, 85, 85); vertical-align: middle; background-color: rgb(252, 252, 252); border-width: 1px; border-style: solid; border-color: rgb(204, 204, 204) rgb(204, 204, 204) rgb(187, 187, 187) }
.markdown-preview[data-use-github-style] a { color: rgb(51, 122, 183) }
.markdown-preview[data-use-github-style] code { color: inherit }
.markdown-preview[data-use-github-style] pre.editor-colors { padding: 0.8em 1em; margin-bottom: 1em; font-size: 0.85em; overflow: auto }
.scrollbars-visible-always .markdown-preview pre.editor-colors .vertical-scrollbar,.scrollbars-visible-always .markdown-preview pre.editor-colors .horizontal-scrollbar { visibility: hidden }
.scrollbars-visible-always .markdown-preview pre.editor-colors:hover .vertical-scrollbar,.scrollbars-visible-always .markdown-preview pre.editor-colors:hover .horizontal-scrollbar { visibility: visible }
.markdown-preview .task-list-item-checkbox { position: absolute; margin: 0.25em 0px 0px -1.4em }
.bracket-matcher .region { border-bottom: 1px dotted lime; position: absolute }
.spell-check-misspelling .region { border-bottom: 2px dotted rgba(255, 51, 51, 0.75) }
.spell-check-corrections { width: 25em !important }
pre.editor-colors { background-color: #282c34; color: #abb2bf }
pre.editor-colors .line.cursor-line { background-color: rgba(153, 187, 255, 0.04) }
pre.editor-colors .invisible { color: #abb2bf }
pre.editor-colors .cursor { border-left: 2px solid #528bff }
pre.editor-colors .selection .region { background-color: #3e4451 }
pre.editor-colors .bracket-matcher .region { border-bottom: 1px solid #528bff }
pre.editor-colors .invisible-character { color: rgba(171, 178, 191, 0.15) }
pre.editor-colors .indent-guide { color: rgba(171, 178, 191, 0.15) }
pre.editor-colors .wrap-guide { background-color: rgba(171, 178, 191, 0.15) }
pre.editor-colors .find-result .region.region.region,pre.editor-colors .current-result .region.region.region { background-color: rgba(82, 139, 255, 0.24) }
pre.editor-colors .find-result .region.region.region { border: 2px solid transparent }
pre.editor-colors .current-result .region.region.region { border: 2px solid #528bff }
pre.editor-colors .gutter .line-number { color: #636d83 }
pre.editor-colors .gutter .line-number.cursor-line { color: #abb2bf; background-color: #2c313a }
pre.editor-colors .gutter .line-number.cursor-line-no-selection { background-color: transparent }
pre.editor-colors .gutter .line-number .icon-right { color: #abb2bf }
pre.editor-colors .gutter:not(.git-diff-icon) .line-number.git-line-removed.git-line-removed::before { bottom: -3px }
pre.editor-colors .gutter:not(.git-diff-icon) .line-number.git-line-removed::after { content: ""; position: absolute; left: 0px; bottom: 0px; width: 25px; border-bottom: 1px dotted rgba(224, 82, 82, 0.5) }
pre.editor-colors .gutter .line-number.folded,pre.editor-colors .gutter .line-number::after,pre.editor-colors .fold-marker::after { color: #abb2bf }
.syntax--comment { color: #5c6370; font-style: italic }
.syntax--comment .syntax--markup.syntax--link { color: #5c6370 }
.syntax--entity.syntax--name.syntax--type { color: #e5c07b }
.syntax--entity.syntax--other.syntax--inherited-class { color: #98c379 }
.syntax--keyword { color: #c678dd }
.syntax--keyword.syntax--control { color: #c678dd }
.syntax--keyword.syntax--operator { color: #abb2bf }
.syntax--keyword.syntax--other.syntax--special-method { color: #61afef }
.syntax--keyword.syntax--other.syntax--unit { color: #d19a66 }
.syntax--storage { color: #c678dd }
.syntax--storage.syntax--type.syntax--annotation,.syntax--storage.syntax--type.syntax--primitive { color: #c678dd }
.syntax--storage.syntax--modifier.syntax--package,.syntax--storage.syntax--modifier.syntax--import { color: #abb2bf }
.syntax--constant { color: #d19a66 }
.syntax--constant.syntax--variable { color: #d19a66 }
.syntax--constant.syntax--character.syntax--escape { color: #56b6c2 }
.syntax--constant.syntax--numeric { color: #d19a66 }
.syntax--constant.syntax--other.syntax--color { color: #56b6c2 }
.syntax--constant.syntax--other.syntax--symbol { color: #56b6c2 }
.syntax--variable { color: #e06c75 }
.syntax--variable.syntax--interpolation { color: #be5046 }
.syntax--variable.syntax--parameter { color: #abb2bf }
.syntax--string { color: #98c379 }
.syntax--string.syntax--regexp { color: #56b6c2 }
.syntax--string.syntax--regexp .syntax--source.syntax--ruby.syntax--embedded { color: #e5c07b }
.syntax--string.syntax--other.syntax--link { color: #e06c75 }
.syntax--punctuation.syntax--definition.syntax--comment { color: #5c6370 }
.syntax--punctuation.syntax--definition.syntax--method-parameters,.syntax--punctuation.syntax--definition.syntax--function-parameters,.syntax--punctuation.syntax--definition.syntax--parameters,.syntax--punctuation.syntax--definition.syntax--separator,.syntax--punctuation.syntax--definition.syntax--seperator,.syntax--punctuation.syntax--definition.syntax--array { color: #abb2bf }
.syntax--punctuation.syntax--definition.syntax--heading,.syntax--punctuation.syntax--definition.syntax--identity { color: #61afef }
.syntax--punctuation.syntax--definition.syntax--bold { color: #e5c07b; font-weight: bold }
.syntax--punctuation.syntax--definition.syntax--italic { color: #c678dd; font-style: italic }
.syntax--punctuation.syntax--section.syntax--embedded { color: #be5046 }
.syntax--punctuation.syntax--section.syntax--method,.syntax--punctuation.syntax--section.syntax--class,.syntax--punctuation.syntax--section.syntax--inner-class { color: #abb2bf }
.syntax--support.syntax--class { color: #e5c07b }
.syntax--support.syntax--type { color: #56b6c2 }
.syntax--support.syntax--function { color: #56b6c2 }
.syntax--support.syntax--function.syntax--any-method { color: #61afef }
.syntax--entity.syntax--name.syntax--function { color: #61afef }
.syntax--entity.syntax--name.syntax--class,.syntax--entity.syntax--name.syntax--type.syntax--class { color: #e5c07b }
.syntax--entity.syntax--name.syntax--section { color: #61afef }
.syntax--entity.syntax--name.syntax--tag { color: #e06c75 }
.syntax--entity.syntax--other.syntax--attribute-name { color: #d19a66 }
.syntax--entity.syntax--other.syntax--attribute-name.syntax--id { color: #61afef }
.syntax--meta.syntax--class { color: #e5c07b }
.syntax--meta.syntax--class.syntax--body { color: #abb2bf }
.syntax--meta.syntax--method-call,.syntax--meta.syntax--method { color: #abb2bf }
.syntax--meta.syntax--definition.syntax--variable { color: #e06c75 }
.syntax--meta.syntax--link { color: #d19a66 }
.syntax--meta.syntax--require { color: #61afef }
.syntax--meta.syntax--selector { color: #c678dd }
.syntax--meta.syntax--separator { background-color: #373b41; color: #abb2bf }
.syntax--meta.syntax--tag { color: #abb2bf }
.syntax--underline { text-decoration: underline }
.syntax--none { color: #abb2bf }
.syntax--invalid.syntax--deprecated { color: #523d14 !important; background-color: #e0c285 !important }
.syntax--invalid.syntax--illegal { color: white !important; background-color: #e05252 !important }
.syntax--markup.syntax--bold { color: #d19a66; font-weight: bold }
.syntax--markup.syntax--changed { color: #c678dd }
.syntax--markup.syntax--deleted { color: #e06c75 }
.syntax--markup.syntax--italic { color: #c678dd; font-style: italic }
.syntax--markup.syntax--heading { color: #e06c75 }
.syntax--markup.syntax--heading .syntax--punctuation.syntax--definition.syntax--heading { color: #61afef }
.syntax--markup.syntax--link { color: #56b6c2 }
.syntax--markup.syntax--inserted { color: #98c379 }
.syntax--markup.syntax--quote { color: #d19a66 }
.syntax--markup.syntax--raw { color: #98c379 }
.syntax--source.syntax--c .syntax--keyword.syntax--operator { color: #c678dd }
.syntax--source.syntax--cpp .syntax--keyword.syntax--operator { color: #c678dd }
.syntax--source.syntax--cs .syntax--keyword.syntax--operator { color: #c678dd }
.syntax--source.syntax--css .syntax--property-name,.syntax--source.syntax--css .syntax--property-value { color: #828997 }
.syntax--source.syntax--css .syntax--property-name.syntax--support,.syntax--source.syntax--css .syntax--property-value.syntax--support { color: #abb2bf }
.syntax--source.syntax--elixir .syntax--source.syntax--embedded.syntax--source { color: #abb2bf }
.syntax--source.syntax--elixir .syntax--constant.syntax--language,.syntax--source.syntax--elixir .syntax--constant.syntax--numeric,.syntax--source.syntax--elixir .syntax--constant.syntax--definition { color: #61afef }
.syntax--source.syntax--elixir .syntax--variable.syntax--definition,.syntax--source.syntax--elixir .syntax--variable.syntax--anonymous { color: #c678dd }
.syntax--source.syntax--elixir .syntax--quoted { color: #98c379 }
.syntax--source.syntax--elixir .syntax--keyword.syntax--special-method,.syntax--source.syntax--elixir .syntax--embedded.syntax--section,.syntax--source.syntax--elixir .syntax--embedded.syntax--source.syntax--empty { color: #e06c75 }
.syntax--source.syntax--elixir .syntax--readwrite.syntax--module .syntax--punctuation { color: #e06c75 }
.syntax--source.syntax--elixir .syntax--regexp.syntax--section,.syntax--source.syntax--elixir .syntax--regexp.syntax--string { color: #be5046 }
.syntax--source.syntax--elixir .syntax--separator,.syntax--source.syntax--elixir .syntax--keyword.syntax--operator { color: #d19a66 }
.syntax--source.syntax--elixir .syntax--variable.syntax--constant { color: #e5c07b }
.syntax--source.syntax--elixir .syntax--array,.syntax--source.syntax--elixir .syntax--scope,.syntax--source.syntax--elixir .syntax--section { color: #828997 }
.syntax--source.syntax--gfm .syntax--markup { }
.syntax--source.syntax--gfm .syntax--link .syntax--entity { color: #61afef }
.syntax--source.syntax--go .syntax--storage.syntax--type.syntax--string { color: #c678dd }
.syntax--source.syntax--ini .syntax--keyword.syntax--other.syntax--definition.syntax--ini { color: #e06c75 }
.syntax--source.syntax--java .syntax--storage.syntax--modifier.syntax--import { color: #e5c07b }
.syntax--source.syntax--java .syntax--storage.syntax--type { color: #e5c07b }
.syntax--source.syntax--java .syntax--keyword.syntax--operator.syntax--instanceof { color: #c678dd }
.syntax--source.syntax--java-properties .syntax--meta.syntax--key-pair { color: #e06c75 }
.syntax--source.syntax--java-properties .syntax--meta.syntax--key-pair>.syntax--punctuation { color: #abb2bf }
.syntax--source.syntax--js .syntax--keyword.syntax--operator { color: #56b6c2 }
.syntax--source.syntax--js .syntax--keyword.syntax--operator.syntax--delete,.syntax--source.syntax--js .syntax--keyword.syntax--operator.syntax--in,.syntax--source.syntax--js .syntax--keyword.syntax--operator.syntax--of,.syntax--source.syntax--js .syntax--keyword.syntax--operator.syntax--instanceof,.syntax--source.syntax--js .syntax--keyword.syntax--operator.syntax--new,.syntax--source.syntax--js .syntax--keyword.syntax--operator.syntax--typeof,.syntax--source.syntax--js .syntax--keyword.syntax--operator.syntax--void { color: #c678dd }
.syntax--source.syntax--json .syntax--meta.syntax--structure.syntax--dictionary.syntax--json>.syntax--string.syntax--quoted.syntax--json { color: #e06c75 }
.syntax--source.syntax--json .syntax--meta.syntax--structure.syntax--dictionary.syntax--json>.syntax--string.syntax--quoted.syntax--json>.syntax--punctuation.syntax--string { color: #e06c75 }
.syntax--source.syntax--json .syntax--meta.syntax--structure.syntax--dictionary.syntax--json>.syntax--value.syntax--json>.syntax--string.syntax--quoted.syntax--json,.syntax--source.syntax--json .syntax--meta.syntax--structure.syntax--array.syntax--json>.syntax--value.syntax--json>.syntax--string.syntax--quoted.syntax--json,.syntax--source.syntax--json .syntax--meta.syntax--structure.syntax--dictionary.syntax--json>.syntax--value.syntax--json>.syntax--string.syntax--quoted.syntax--json>.syntax--punctuation,.syntax--source.syntax--json .syntax--meta.syntax--structure.syntax--array.syntax--json>.syntax--value.syntax--json>.syntax--string.syntax--quoted.syntax--json>.syntax--punctuation { color: #98c379 }
.syntax--source.syntax--json .syntax--meta.syntax--structure.syntax--dictionary.syntax--json>.syntax--constant.syntax--language.syntax--json,.syntax--source.syntax--json .syntax--meta.syntax--structure.syntax--array.syntax--json>.syntax--constant.syntax--language.syntax--json { color: #56b6c2 }
.syntax--source.syntax--ruby .syntax--constant.syntax--other.syntax--symbol>.syntax--punctuation { color: inherit }
.syntax--source.syntax--python .syntax--keyword.syntax--operator.syntax--logical.syntax--python { color: #c678dd }
.syntax--source.syntax--python .syntax--variable.syntax--parameter { color: #d19a66 }

1 Asio介绍

在进行网络编程之前,我们先对Asio进行简单的介绍。首先Asio这个名字就说明了它的核心作用——异步输入/输出(Asynchronous input/output).也是就是说这个库设计的目的是让C++异步地处理数据且独立于平台,并不只是针对网络编程而已,只是它主要被应用于网络编程。除了网络编程以外他还包含了其他的IO功能。
异步数据处理就是指任务触发后不需要等待它们完成。相反,Boost.Asio会在任务完成时触发一个应用。异步任务的主要优点在于,在等待任务完成时不需要阻塞应用程序,可以去执行其它任务。异步任务的典型例子是网络应用。即在数据发送完毕后我们需要知道数据是否发送成功,异步通信可以让我们不必等待发送数据返回的结果。Boost.Asio将这个过程分成两个单独的步骤:第一步是作为一个异步任务开始传输数据。一旦数据传输完成,无论成功与否,应用程序会在第二步中国得到相关的结果通知。

2 I/O服务与I/O对象

使用Boost.Asio 进行异步数据处理的应用程序基于两个概念:I/O服务和IO/对象。I/O服务抽象了操作系统的接口,运行第一时间进行异步数据处理,而I/O对象则用于初始化特定的操作。鉴于Boost.Asio值提供了一个名为 boost::asio::io_service 的类作为I/O服务,它针对所支持的每一个操作系统分别实现了优化的类,另外库中还包含了针对不同I/O对象的几个类。其中,类 boost::asio::ip::tcp::socket 用于通过网络发送和接收数据,而类 boost::asio::deadline_timer 则提供了一个计时器,用于测量某个固定时间点到来或是一段指定的时长过去了。下面给出了一个计时器的例子,因为相较于其他I/O对象而言,它不需要任何有关网络编程的知识。

#include <boost/asio.hpp>

#include <iostream>

void Handler(const boost::system::error_code &ec)

{

std::cout<<"3 s."<<std::endl;

}

int main()

{

boost::asio::io_service ioService;

boost::asio::deadline_timer timer(ioService,boost::posix_time::seconds(3));

timer.async_wait(Handler);

std::cout<<"waiting..."<<std::endl;

ioService.run();

system("pause");        //程序暂停

return 0;

}

在main()中首先定义了一个I/O服务 io_service ,用于初始化I/O对象timer.就像本例中 deadline_timer 一样,所有I/O对象都需要一个I/O服务作为他们的构造函数的第一个参数。timer的第二个参数是时间用于指定定时的时长。这里通过调用 async_wait 来实现异步通信,其参数为回调函数的句柄值。这里的程序并不会阻塞在这里等待定时结束,而是立刻往下执行(这里打印waiting).如果我们使用的是 wait 函数则会阻塞在这里。最后程序又调用了I/O服务的 run() 方法,这是必须的,因为控制权必须被操作系统接管,才能在五秒之后调用 hander() 函数。 注意run()方法是阻塞的 ,在程序运行到这里时会"停止执行"。这一点挺有讽刺意味的,即在许多操作系统中都是通过阻塞来支持异步操作。在本例中如果run方法不是阻塞的,那可能在异步通信回调函数调用之前程序就运行完毕结束了。在调用完run后程序会继续向下运行,为了防止程序结束这里调用了一个暂停函数,方便观察结果。
这里我们可能会提出一些问题,比如如果异步程序是通过run方法的阻塞来完成的,那么多个异步程序怎么执行?如果不希望线程阻塞应该怎么做?为了深刻的了解run的工作原理,我们首先看一下下面这个例子:

#include <boost/asio.hpp>

#include <iostream>

void Handler1(const boost::system::error_code &ec)

{

std::cout<<"2 s."<<std::endl;

}

void Handler2(const boost::system::error_code &ec)

{

std::cout<<"4 s."<<std::endl;

}

int main()

{

boost::asio::io_service ioService;

boost::asio::deadline_timer timer1(ioService,boost::posix_time::seconds(2));

timer1.async_wait(Handler1);

//timer1.wait();        //阻塞等待

boost::asio::deadline_timer timer2(ioService,boost::posix_time::seconds(4));

timer2.async_wait(Handler2);

std::cout<<"waiting..."<<std::endl;

ioService.run();

system("pause");        //程序暂停

return 0;

}

这这里例子中可以看到多个异步通信可以基于同一个I/O服务。当然我们也可以为每一个异步通信都建立一个I/O服务,不过之前说过ioService.run是会阻塞程序的,我们必须要让回调函数先执行的I/O服务的run方法先执行(这里等待3秒异步通信)。即:

boost::asio::io_service ioService1;

boost::asio::io_service ioService2;

boost::asio::deadline_timer timer1(ioService1,boost::posix_time::seconds(2));

timer1.async_wait(Handler1);

//timer1.wait();        //阻塞等待

boost::asio::deadline_timer timer2(ioService2,boost::posix_time::seconds(4));

timer2.async_wait(Handler2);

std::cout<<"waiting..."<<std::endl;

ioService1.run();

ioService2.run();

绝大多数情况下这个方法是很危险的,更一般的情况是让每一个I/O服务运行于一个单独的线程。这样它就只会阻塞它所在的线程对其他线程没有任何影响。这也解决了我们上面提出的第二个问题——防止run方法的阻塞。
一旦特定的I/O服务中所有异步操作都完成了,控制权就会返回 run() 方法,然后它就会返回。上面的例子中当两个闹铃都响过之后,程序就会向下运行。

3 多线程与异步操作

现如今的PC即通常都具有多核处理器,所以使用多线程的应用可以进一步提高应用程序的执行效率。但是创建了比处理器个数更多的线程并不会在性能上有所提升,boost库中提供了获取逻辑处理器个数的函数 boost::thread::hardware_concurrency() ,逻辑处理器个数和内核数不一样,一个内核可以通过超线程技术超线程为双核,也称伪双核,和真双核的区别是它们是共享缓存。比如我的电脑就是使用了超线程技术变为双核四线程的。
前面我们提到过使用多线程的方式能够防止run方法的阻塞,进而可以使多个异步操作同时运行。下面就来具体讨论多线程和异步操作的联合使用。

3.1 基于一个I/O服务的多异步任务处理

关于多线程和异步操作的联合使用主要有两种方式,首先我们先介绍一个I/O服务和多个异步任务的处理方式。 io_service::run() 方法是线程安全的,因此我们可用在多个线程中调用同一个run方法,下面是一个使用示例:

#include <boost/asio.hpp>

#include <boost/thread.hpp>

#include <iostream>

void Handler1(const boost::system::error_code &ec)

{

std::cout<<"2 s."<<std::endl;

}

void Handler2(const boost::system::error_code &ec)

{

std::cout<<"2 s."<<std::endl;

}

boost::asio::io_service IoService;

void Run()

{

IoService.run();

}

int main()

{

boost::asio::deadline_timer timer1(IoService,boost::posix_time::seconds(2));

timer1.async_wait(Handler1);

boost::asio::deadline_timer timer2(IoService,boost::posix_time::seconds(2));

timer2.async_wait(Handler2);

std::cout<<"waiting..."<<std::endl;

boost::thread thread1(Run);

boost::thread thread2(Run);

thread1.join();

thread2.join();

system("pause");        //程序暂停

return 0;

}

在上面这个示例中,我们分别在两个线程中调用了IoService的run方法。这样就可以使两个异步操作并行的执行,如果我们在执行的第二个异步操作的回调函数时,第一个回调函数正在运行,则第二个回调函数会自动在第二个线程中运行。如果在执行第二个回调函数时,第一个已经运行完毕了,那么第二个回调函数就会在第一个线程中运行。当然我们也可以在两个线程中运行4个异步操作,如果我们确定没有两个以上异步操作会同时运行,那么就不会产生等待(如两个线程在2s后运行,两个在4秒后运行)。如果有两个以上异步回调函数同时运行,就会有一个异步操作必须等待(如三个在2s后运行,一个在4s后运行)。
上面的程序有一个bug,即这两个异步操作可能在两个线程中同时访问临界资源 std::cout 的buffer缓冲区,这样有可能会使终端打印的信息混合。解决的办法是使用互斥锁。这部分内容属于多线程编程的知识点,这里不做过多介绍,可以参考 boost多线程的使用
对于上面的代码由于临界资源的关系并不能让两个线程并行运行,所有这时使用多线程并不能提供多少的好处。关于异步这里在多说一点,异步操作不同于一般的C++风格。异步操作使得原本在逻辑上是顺序执行的关系在物理上分割开来了,令代码的阅读更加困难。这就使得我们在进行异步通信的编程时要更加的注意代码的组织形式。

3.2 多个I/O服务对应多个异步任务

另外一种I/O服务和异步任务的对应关系是——多个I/O服务对应多个异步任务。这个概念理解起来比较简单,就是将每一个异步任务都绑定到唯一的一个I/O服务上,如下例所示:

#include <boost/asio.hpp>

#include <boost/thread.hpp>

#include <iostream>

void Handler1(const boost::system::error_code &ec)

{

std::cout<<"2 s."<<std::endl;

}

void Handler2(const boost::system::error_code &ec)

{

std::cout<<"2 s."<<std::endl;

}

const int ThreadNum = 2;

boost::asio::io_service IoService[ThreadNum];

void Run(int i)

{

IoService[i].run();

}

int main()

{

boost::asio::deadline_timer timer1(IoService[0],boost::posix_time::seconds(2));

timer1.async_wait(Handler1);

boost::asio::deadline_timer timer2(IoService[1],boost::posix_time::seconds(2));

timer2.async_wait(Handler2);

std::cout<<"waiting..."<<std::endl;

boost::thread thread1(boost::bind(Run,0));

boost::thread thread2(boost::bind(Run,1));

thread1.join();

thread2.join();

system("pause");        //程序暂停

return 0;

}

这里使用的 bind 将Run绑定为一个无参返回类型为void的函数对象,因为thread的构造函数只支持void的无参函数或函数对象。关于bind的使用可以参考 bind的使用 .这个应用程序的功能与前一个相同。只是这里的每一个异步操作都运行在独立的线程之中相当于小的自主应用,它们拥有独立的告诉缓存、内存页。由于在确定优化策略之前需要对底层硬件、操作系统、编译器以及潜在的瓶颈有专门的了解,所以应该仅在清除这些好处的情况下使用多个I/O服务。
当然我们也可以将上面两种方式进行结合,即某些线程可能处理多个异步操作,而另外一些线程则只处理单个的异步操作。

4 网络编程介绍

前面我们提到过虽然Boost.Asio是一个可以异步处理任何种类数据的库,但是它主要被用于网络编程。事实上,Boost.Asio在加入其它对象之前就已经支持网络功能了。网络编程主要分为客户端开发和服务器端开发,简单的来说客户端就是主动连接的一端,而服务器端是等待被连接的一端。下面是作为客户端访问 www.highscore.de 的一个例子:

#include <boost/asio.hpp>

#include <boost/array.hpp>

#include <iostream>

#include <string>

boost::asio::io_service IoService;

boost::asio::ip::tcp::resolver Resolver(IoService);

boost::asio::ip::tcp::socket Socket(IoService);

boost::array<char,4096> Buffer;

void ReadHandler(const boost::system::error_code& ec,std::size_t transferredNum)

{

if(!ec)

{

std::cout<<std::string(Buffer.data(),transferredNum)<<std::endl;

Socket.async_read_some(boost::asio::buffer(Buffer),ReadHandler);

}

}

void ConnectHandler(const boost::system::error_code &ec)

{

if(!ec)

{

boost::asio::write(Socket,boost::asio::buffer("GET / HTTP 1.1\r\nHost: highscore.de\r\n\r\n"));

Socket.async_read_some(boost::asio::buffer(Buffer),ReadHandler);

}

else

std::cout<<"连接失败"<<std::endl;

}

void ResolverHandler(const boost::system::error_code& ec,boost::asio::ip::tcp::resolver::iterator it)

{

if(!ec)

{

Socket.async_connect(*it,ConnectHandler);

std::cout<<(*it).service_name()<<std::endl;

}

else

std::cout<<"解析失败"<<std::endl;

}

int main()

{

boost::asio::ip::tcp::resolver::query query("www.highscore.de","80");

Resolver.async_resolve(query,ResolverHandler);

IoService.run();

system("pause");        //程序暂停

return 0;

}



这里分别使用了三个回调函数来处理解析、连接和数据接收的异步操作。由于这三个异步操作在时间上有先后顺序并不会同时产生,所以这里使用了单线程的模式。互联网使用了IP地址来标识每台PC。IP地址实际上只是一串数字,难以记住。为了方便对互联网上的主机进行记忆,我们通常会给主机的地址起一个比较容易记住的名称,如这里的 www.highscore.de .通过域名解析的过程可以将这个域名翻译成相应的IP地址,而这个翻译就是对应的这里的 boost::asio::ip::tcp::resolver。域名解析过程并不是在本地完成的,而是由互联网上专门的DNS服务器完成。因此这个解析过程通常也被实现为一个异步操作。一旦解析完成,无论是否成功,这里的 ResolverHandler() 都会被调用。
在域名解析完毕之后,我们就需要和我们需要通讯的服务器建立连接。建立连接的过程这里也用异步通信进行实现。建立连接需要正确的服务器解析,解析后的地址由迭代器 it 保存。在连接建立完毕后,就可以开始通讯了。这里先发送一个请求,然后进行异步的数据读取。在数据读取的回调函数 ReadHandler 中,我们先打印服务器返回的数据,然后再次的进行异步的读取。这个操作是必须的,因为不能抱枕一次异步操作就能将整个页面传输完毕。当整个页面传输完毕后,ReadHandler 会给出一个错误进而防止进一步的页面输出。
在上面这个示例中,输入输出缓冲区使用了 boost::array 容器,位于 boost/array.hpp 中,它提供了类似C语言中数组的概念比vector的效益要高一些。
下面是一个服务器端的例子:

#include <boost/asio.hpp>

#include <string>

#include <iostream>

boost::asio::io_service IoService;

boost::asio::ip::tcp::endpoint Endpoint(boost::asio::ip::tcp::v4(),80);

boost::asio::ip::tcp::socket Socket(IoService);

boost::asio::ip::tcp::acceptor Acceptor(IoService,Endpoint);

std::string Data = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello,world!";

void WriteHandler(const boost::system::error_code &ec,std::size_t transferredNum)

{

}

void AcceptHandler(const boost::system::error_code &ec)

{

if(!ec)

{

boost::asio::async_write(Socket,boost::asio::buffer(Data),WriteHandler);

}

else

std::cout<<"连接失败"<<std::endl;

}

int main()

{

Acceptor.listen();

Acceptor.async_accept(Socket,AcceptHandler);

IoService.run();

system("pause");        //程序暂停

return 0;

}

上面这个是服务器端的例子,它以本机作为服务器等待客户端进行连接(本机回环地址为127.0.0.1),当客户端和其建立连接后它会给客户端发送 Data 中的数据。我们可以将上上个客户端示例中的查询部分主机地址改为 boost::asio::ip::tcp::resolver::query query("127.0.0.1","80"); 。然后我们先运行服务器端的应用程序然后再运行客户端的应用程序。在客户端就可以接收到主机发送到的数据了。
main() 首先调用了 listen() 方法将接收器置于接收状态,然后调用 async_accept() 方法等待一个客户端的连接。当连接建立后,就会调用 AcceptHandler() 方法。我们可以在这里调用 boost::asio::async_write()Data 中的数据进行发送,在将缓冲区中的数据全部发送完毕后就会调用指定的 WriteHandler 函数。
下面执行过程:

这里只是对网络编程的一个简单的介绍,后续我们会更详细的介绍网络编程的概念。

参考文章:

中文教程:
Boost Asio C++ 网络编程
官方教程:
Boost.Asio
The Boost C++ Libraries(中文版) 第7章

时间: 2024-07-30 23:39:31

boost Asio网络编程简介的相关文章

Boost Asio 网络编程 基本用法

Boost Asio 网络编程 基本用法 flyfish 2015-2-9 IP地址 boost::asio::ip::address表示IP地址,同时支持ipv4和ipv6. boost::asio::ip::address addr; addr = addr.from_string("127.0.0.1"); assert(addr.is_v4()); OutputDebugStringA(addr.to_string().c_str()); addr = addr.from_st

boost::asio网络传输错误码的一些实验结果(recv error_code)

错误码很重要,可以由此判断网络连接到底发生了神马事情,从而驱动高层逻辑的行为.只有笼统的错误码判断的网络层是不够规范的,鄙人觉得有些错误码还是需要在网络层就区分开的,特此记录一些当前实验的错误码以及发生原因. 以下是一部分在async_receive()的handler处捕获到的比较有用的错误码 错误码(十进制) 枚举 发现原因 10009 boost::asio::error::bad_descriptor 在一个已经关闭了的套接字上执行async_receive() 995 boost::a

boost asio 学习(九) boost::asio 网络封装

http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=10 9. A boost::asio network wrapper (TCP) 现在我们了解asio和TCP网络方面的知识,我们可以尝试下封装网络底层.通过使用这个封装,我们可以重用代码并且将精力集中于业务逻 辑方面而不在网络通讯方面花费太多精力. 重要提示:本代码仅仅用于教学目的.不要在商业系统中使用该代码,

C/C++利用Boost::Asio网络库建立自己的Socket服务器

引言 寸光阴,当下我们或许更需要利用现有的知识,应用现有的技术.网络是当前互联网的根本,了解网络便开始显得极其重要.今天我们利用Boost库中Asio部分,浅尝网络服务器.此处不做过于深入的开展,为达成学习目的,只做简单的异步并发服务器. 注意:本篇代码没有直接引用boost等命名空间,为的是新入门Boost的同学能够更好的了解每个参数在boost的具体命名空间位置,有助于更好的理解boost的布局. 版权所有:_OE_,转载请注明出处:http://blog.csdn.net/csnd_ayo

boost asio 网络聊天 代码修改学习

简化asio的聊天代码 去除ROOM的设计 所有连接客户端均在同一个ROOM下 chat message 使用boost自带示例的头文件 #pragma once #include <cstdio> #include <cstdlib> #include <cstring> class chat_message { public: enum { header_length = 4 }; enum { max_body_length = 512 }; chat_messa

Boost C++: 网络编程1

1 #include <iostream> 2 #include <boost/asio.hpp> 3 #include <boost/config/compiler/visualc.hpp> 4 #include <boost/property_tree/ptree.hpp> 5 #include <boost/property_tree/json_parser.hpp> 6 #include <boost/foreach.hpp>

UNIX网络编程--简介(一)【转】

本文转载自:http://blog.csdn.net/yusiguyuan/article/details/11760187 一.概述 a) 在编写与计算机通信的程序时,首先要确定的就是和计算机通信的协议,从高层次来确定通信由哪个程序发起以及响应在合适产生.大多数网络应用按照划分成客户和服务器来组织.在设计网络应用时,确定总是由客户发起请求往往能够简化协议和程序本身.当然一个较为复杂的网络应用还需要异步回调通信,也就是由服务器向客户发起请求消息. i. 在本书原著上多次提到客户(client)和

Java基础学习(六) 网络编程简介

基础知识:1.osi分层模型和TCP/IP分层模型对应关系 2.七层模型与协议的对应关系 网络层 ------------ IP(网络之间的互联协议) 传输层 ------------ TCP(传输控制协议).UDP(用户数据报协议) 应用层 ------------ Telnet(Internet远程登录服务的标准协议和主要方式).FTP(文本传输协议).HTTP(超文本传送协议) 3.IP地址和端口号1.ip地址用于唯一标示网络中的一个通信实体,这个通信实体可以是一台主机,可以是一台打印机,

Boost.Asio c++ 网络编程翻译(14)

保持活动 假如,你需要做下面的操作: io_service service; ip::tcp::socket sock(service); char buff[512]; ... read(sock, buffer(buff)); 在这个例子中,sock和buff的存在时间都必须比read()调用的时间要长.也就是说,在调用read()返回之前,它们都必须有效.这就是你期望的:你传给一个方法的所有参数在参数内部都必须有效.当我们采用异步方式时,事情会变得越复杂. io_service servi