c#endread怎么打印出来_NetworkStream.EndRead(IAsyncResult) 方法 (System.Net.Sockets) | Microsoft Docs...

处理异步读取的结束。Handles the end of an asynchronous read.

public:

override int EndRead(IAsyncResult ^ asyncResult);

public override int EndRead (IAsyncResult asyncResult);

override this.EndRead : IAsyncResult -> int

Public Overrides Function EndRead (asyncResult As IAsyncResult) As Integer

参数

An IAsyncResult that represents an asynchronous call.

返回

The number of bytes read from the NetworkStream.

例外

asyncResult 参数为 null。The asyncResult parameter is null.

已关闭基础 Socket。The underlying Socket is closed.

- 或 --or-

访问套接字时出错。An error occurred when accessing the socket.

示例

在下面的代码示例中, myReadCallback 将 BeginRead 作为回调方法提供给。In the following code example, myReadCallback is provided to BeginRead as the callback method. EndRead 在中实现, myReadCallback 以完成由启动的异步读取调用 BeginRead 。EndRead is implemented in myReadCallback to complete the asynchronous read call started by BeginRead.

// Example of EndRead, DataAvailable and BeginRead.

static void myReadCallBack( IAsyncResult^ ar )

{

NetworkStream^ myNetworkStream = safe_cast(ar->AsyncState);

array^myReadBuffer = gcnew array(1024);

String^ myCompleteMessage = "";

int numberOfBytesRead;

numberOfBytesRead = myNetworkStream->EndRead( ar );

myCompleteMessage = String::Concat( myCompleteMessage, Encoding::ASCII->GetString( myReadBuffer, 0, numberOfBytesRead ) );

// message received may be larger than buffer size so loop through until you have it all.

while ( myNetworkStream->DataAvailable )

{

AsyncCallback^ pasync = gcnew AsyncCallback( &myReadCallBack );

myNetworkStream->BeginRead( myReadBuffer, 0, myReadBuffer->Length, pasync, myNetworkStream );

}

// Print out the received message to the console.

Console::WriteLine( "You received the following message : {0}", myCompleteMessage );

}// Example of EndRead, DataAvailable and BeginRead.

public static void myReadCallBack(IAsyncResult ar ){

NetworkStream myNetworkStream = (NetworkStream)ar.AsyncState;

byte[] myReadBuffer = new byte[1024];

String myCompleteMessage = "";

int numberOfBytesRead;

numberOfBytesRead = myNetworkStream.EndRead(ar);

myCompleteMessage =

String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));

// message received may be larger than buffer size so loop through until you have it all.

while(myNetworkStream.DataAvailable){

myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length,

new AsyncCallback(NetworkStream_ASync_Send_Receive.myReadCallBack),

myNetworkStream);

}

// Print out the received message to the console.

Console.WriteLine("You received the following message : " +

myCompleteMessage);

}' Example of EndRead, DataAvailable and BeginRead.

Public Shared Sub myReadCallBack(ar As IAsyncResult)

Dim myNetworkStream As NetworkStream = CType(ar.AsyncState, NetworkStream)

Dim myReadBuffer(1024) As Byte

Dim myCompleteMessage As [String] = ""

Dim numberOfBytesRead As Integer

numberOfBytesRead = myNetworkStream.EndRead(ar)

myCompleteMessage = [String].Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))

' message received may be larger than buffer size so loop through until you have it all.

While myNetworkStream.DataAvailable

myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, New AsyncCallback(AddressOf NetworkStream_ASync_Send_Receive.myReadCallBack), myNetworkStream)

End While

' Print out the received message to the console.

Console.WriteLine(("You received the following message : " + myCompleteMessage))

End Sub

注解

EndRead方法完成方法中启动的异步读取操作 BeginRead 。The EndRead method completes the asynchronous read operation started in the BeginRead method.

在调用之前 BeginRead ,需要创建一个实现委托的回调方法 AsyncCallback 。Before calling BeginRead, you need to create a callback method that implements the AsyncCallback delegate. 此回调方法在单独的线程中执行,并在返回后由系统调用 BeginRead 。This callback method executes in a separate thread and is called by the system after BeginRead returns. The callback method must accept the IAsyncResult returned from the BeginRead method as a parameter.

Within the callback method, call the AsyncState property of the IAsyncResult to obtain the state object passed to the BeginRead method. Extract the receiving NetworkStream from this state object. 获取后 NetworkStream ,调用 EndRead 方法以成功完成读取操作并返回读取的字节数。After obtaining the NetworkStream, call the EndRead method to successfully complete the read operation and return the number of bytes read.

EndRead方法会一直阻止,直到有可用的数据。The EndRead method blocks until data is available. EndRead方法可读取尽可能多的数据,最多可达在方法的参数中指定的字节数 size BeginRead 。The EndRead method reads as much data as is available up to the number of bytes specified in the size parameter of the BeginRead method. 如果远程主机关闭 Socket 连接并且接收到所有可用数据,则该 EndRead 方法会立即完成并返回零字节。If the remote host shuts down the Socket connection and all available data has been received, the EndRead method completes immediately and returns zero bytes.

若要获取收到的数据,请调用 AsyncState 的属性 IAsyncResult ,然后提取所产生状态对象中包含的缓冲区。To obtain the received data, call the AsyncState property of the IAsyncResult, and extract the buffer contained in the resulting state object.

备注

If you receive an IOException, check the InnerException property to determine if it was caused by a SocketException. 如果是这样,请使用 ErrorCode 属性获取特定的错误代码,并参考 Windows 套接字版本 2 API 错误代码 文档以获取错误的详细说明。If so, use the ErrorCode property to obtain the specific error code and refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

适用于

另请参阅

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/505022.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

wp自定义帖子没标签_ofollow标签的作用有重大变化

nofollow标签的历史经典的nofollow标签作用和使用方法以前的帖子写过,详情读者可以参考以前帖子。nofollow标签(准确说是属性,不过约定俗成,还是叫标签吧)是Google和Yahoo等搜索引擎2005年推出的,目的是告诉…

android电视视频播放器,智能电视如何播放本地视频?当贝市场分享几款播放器...

原标题:智能电视如何播放本地视频?当贝市场分享几款播放器对视频清晰度要求更高的用户普遍会自己下载视频,然后通过本地播放的方式观影,那么,下面就给大家介绍几款智能电视的本地视频播放软件,包你好用。当…

c 复杂的前置后置面试题_你被哪些C语言面试题坑过?

最近在《深入理解计算机系统》上看到一道题,分享一下:假设我们在对有符号值使用补码运算的32位机器人运行代码。对于有符号值使用的是算术右移,而对于无符号值使用的是逻辑右移。变量的声明和初始化如下:int x foo(); //任意值in…

python 三引号_Python 简明教程 --- 4,Python 变量与基本数据类型

微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io任何一个人都会写出能够让机器理解的代码,只有好的程序员才能写出人类可以理解的代码。 —— Martin Fowler 1,什么是变量计算机的本质是处理数据,数…

鸿蒙系统手机9月11日,鸿蒙系统9月11日,将有望正式成为国际第三大手机操作生态系统...

原标题:鸿蒙系统9月11日,将有望正式成为国际第三大手机操作生态系统众所周知,当时华为鸿蒙系统还处于1.0版本的时候,这项技术就已经被运用到了荣耀智能屏上,目前该系统也已经过渡到了华为的手表上,经过这一…

lisp 所在图幅号计算_地图标准分幅与编号计算(二)新图幅号

新图幅号1991年制订了新的《国家基本比例尺地形图分幅和编号》(GB/T 13989-92 )的国家标准,并给出了不同标准比例尺地形图的编给规范及图式。新测和更新的地图,照此标准进行分幅和编号。我国基本比例尺的地形图包括1:5000、1:1万、1:2.5万、1:5万、1:10万…

高德地图画带箭头的线_现代汽车把艺术展览搬到线上,邀您逛全景获奖展

Hyundai Blue Prize 2019获奖展“游戏社会:狼、猞猁和蚁群”(Play societies:wolves, lynx and ants)线上展览正式上线。《游戏社会: 狼、猞猁和蚁群》,以 “信息高速公路上的荒原狼”、“猞猁安全岛”和“蚁群游戏厅” 三段隐喻文本展开展览…

python实训名片管理程序_python3学生名片管理v2.0版

python学生名片管理vv2.0是在1.0的基础上增加部分功能,实现将数据存入文件保存,以便于程序停止后还能再次取到数据。具体实现请看如下部分:card_main.pyimport sysfrom One.card_func2 import *def head():print(* * 30)print(学生信息管理系…

ssm 项目cannot resolve package_前端工程化之创建项目

前言在我们团队,刚开始创建项目,是直接使用框架的 cli 进行创建项目,并修改相关配置。随着项目的增多,沉淀了两套模板,平台端及移动端。后来,我们自己写了一个简单的 cli,并提供了 create 及 li…

android_secure写权限,android.permission.WRITE_SECURE_SETTINGS权限报错

在做Android的GPS这一块时,根据原生代码写的Widget,运行时总是会报错说需要android.permission.WRITE_SECURE_SETTINGS权限,于是便在Manifest.xml中添加该权限,但是保存时会报错提示该权限仅用于系统的app查看了很多资料都说需要将…

解析mysqlbinlog日志_mysqlbinlog日志分析 日志挖掘 违规操作

案例:客户数据库,某关键配置表被修改,需查找操作记录及和操作时间1:mysqlbinlog读取如果binlog formatstatement 可以直接通过mysqlbinlog查看如果binlog formatrow 需要添加 -vv --base64-outputdecode-rows/usr/local/mysql/bi…

python打印长方形_利用python打印出菱形、三角形以及矩形的方法实例

前言 本文主要给大家介绍了关于利用python打印出菱形、三角形以及矩形的相关内容,分享出来供大家参考学习,话不多说,来一起看看详细的介绍: 实例代码 #coding:utf-8 rows int(raw_input(输入列数: )) i j k 1 #声明…

百度seo排名规则_百度关键词seo优化排名如何上首页

无涯孤客百度关键词seo优化排名快速上首页,是通过使用多种百度算法优化,让网站在搜索引擎上排名更好,我们做百度关键词排名的话,要比市面上绝大公司做的要稳定,也希望各位可以相信我们,我们可以将百度关键词…

数学难题html5小游戏答案,小学数学难题讲解及答案

第一讲速算与巧算例1 计算9+99+999+9999+99999解:在涉及所有数字都是9的计算中,常使用凑整法.例如将999化成1000—1去计算.这是小学数学中常用的一种技巧.9+99+999+9999&…

华为做raid5步骤_华为RH2288V5服务器做RAID 0(官方推荐做法)

华为RH2288V5服务器采用的Avago SAS3408阵列卡,RAID做法跟RH2285 RH1285不一样,今天薇晓兰就带各位了解下如何给RH2288V5服务器做RAID!由于Avago SAS3408不支持Legacy配置,支持UEFI配置;支持Legacy和UEFI启动。所以开机…

distinct过滤掉重复记录并且显示所有字段_MySQL的所有姿势,我都帮你准备好了...

mysql登录远程登录方式本地登陆方式mysql:mysql -h 主机名 -P 端口号 -u 用户名 -p密码 mysql:mysql -uroot -p密码数据库操作命令创建数据库、删除数据库、展示所有数据库名。查看当前数据库名、查看所有表、查看其他数据库的表。查看数据库的版本、表结…

HTML5海报生成器源码,原生js小项目 - canvas海报生成器

1.背景之前做过一个营销类移动端h5项目-海报生成器,上传用户本地图片合成海报并支持下载,这次有时间了整理整理。2.几个重点上传本地图片并支持预览处理ios照片翻转使用canvas对图片等比拉伸缩放并居中裁剪使用canvas绘制图片以及文本输出base64并支持下…

pyqt5窗口之间传递信号_pyQT5 实现窗体之间传值的示例

准备一个MainWindow和一个WidgetForm,总代码如下# -*- coding: utf-8 -*-from PyQt5 import QtWidgetsfrom main_windows import Ui_MainWindowimport sysfrom wid_defs import my_widgetsfrom dlg_defs import my_Dialogclass MyWindow(QtWidgets.QMainWindow,Ui_M…

matlab length_MATLAB入门

一、操作界面1.功能区 2.当前文件夹 3.命令窗口4.工作区 5.程序编辑窗口 6.脚本文件(1)当前文件夹是MATLAB读取和存储文件的默认文件夹。 (2)当前文件夹可以修改或新建命令行窗口:用于输入命令(或语句&…

html微信支付功能代码,js微信支付实现代码

微信支付//调用微信JS api 支付function jsApiCall(){WeixinJSBridge.invoke("getBrandWCPayRequest",{$parms},//下面是支付完成后的回调,可以直接提示成功function(res) {if(res.err_msg "get_brand_wcpay_request:ok") {location.href &qu…