- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 MSDN example使用异步套接字时,套接字中的接收数据是通过从 BeginReceive 调用的回调处理程序中重复调用异步 BeginReceive 来完成的:
private static void ReceiveCallback( IAsyncResult ar ) {
//...Skipped...
if (bytesRead > 0) {
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
new AsyncCallback(ReceiveCallback), state);
} else {
// ...Skipped...
}
http://msdn.microsoft.com/en-us/library/bbx2eya8(v=vs.110).aspx
是否有必要从已经在单独线程中执行的处理程序再次进行异步调用?可以简单地在此处理程序中循环使用 Receive 吗?像这样的东西:
while (bytesRead) {
bytesRead = client.Receive(state.buffer, 0, client.Available,
SocketFlags.None);
// Etc...
}
最佳答案
APM pattern的主要目标是为了避免在等待异步操作结果时阻塞调用线程,从而提高服务器应用程序的可扩展性。
如果在 AsyncCallback
中继续同步调用循环中的 Receive
,您仍然会阻塞初始 BeginReceive
所在的 IOCP 线程> 已完成。对于客户端 UI 应用程序来说,这可能没问题,您可能不关心 ThreadPool 饥饿问题,但这对于服务器端应用程序来说肯定不是一个好主意,否则阻塞的线程可能会服务其他传入的客户端请求。
请注意,对于 C# 5.0/.NET 4.5 及更高版本,APM 被视为遗留。您可以使用async/await
和 new Task-based Asynchronous Pattern (TAP)
模式,大大简化了异步代码开发,例如:
async Task<string> ReadAllAsync()
{
var sb = new StringBuffer();
using (var tcp = new TcpClient())
{
await tcp.ConnectAsync(IPAddress.Parse("localhost"), 8080).ConfigureAwait(false);
var buffer = new byte[1024];
using (var stream = tcp.GetStream())
{
var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
if (0 == bytesRead)
break;
sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));
}
}
return sb.ToString();
}
如果由于某种原因您不想使用 NetworkStream.ReadAsync
,您可以使用 Task.FromAsync
将 APM 样式套接字 API 包装为 TAP:
public static class SocketsExt
{
static public Task ReceiveDataAsync(
this TcpClient tcpClient,
byte[] buffer)
{
return Task.Factory.FromAsync(
(asyncCallback, state) =>
tcpClient.Client.BeginReceive(buffer, 0, buffer.Length,
SocketFlags.None, asyncCallback, state),
(asyncResult) =>
tcpClient.Client.EndReceive(asyncResult),
null);
}
static public async Task<Int32> ReceiveInt32Async(
this TcpClient tcpClient)
{
var data = new byte[sizeof(Int32)];
await tcpClient.ReceiveDataAsync(data).ConfigureAwait(false);
return BitConverter.ToInt32(data, 0);
}
}
关于C# 套接字 : synchronous calls within asynchronous ones,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24110473/
注意:这不是库存控制系统。我只是想绘制给哪个患者服用哪种药物的 map 。我没有考虑多少药包等。只是一次用药事件 我对数据库关系突然感到困惑,即使在与他们合作多年之后也是如此。以下是我的情况。 我有一
当用 PHP 发送群发邮件时,是向每个订阅者发送一封电子邮件(对所有电子邮件地址运行一个 for 循环)更好,还是仅将密件抄送中的所有内容添加到逗号分隔的列表中,并且因此只发送一封电子邮件? 谢谢。
我不确定我是否正确地为这种类型的关系建模,也许有人可以提供一些见解来判断这是否合理: 假设我们有一个典型的亲子类型关系,其中每个 parent 都可以有很多 child ,但我们需要跟踪 parent
我有模板和模板版本。一个模板可以有多个 template_version,但在任何给定时间只有一个事件的 template_version。我有以下两个模型: class Template 'Tem
如果我的代码是这样的: if($seconds < 60) $interval = "$seconds seconds ago"; else if($seconds < 3600) $
当我创建一对一关系迁移时,laravel 创建一对多关系。 PHP 7.1 和 MySQL 5.7 模型是:角色和用户。 角色: public function user() { return
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Java Strings: “String s = new String(”silly“);” 我正在浏览一
我正在创建一个社交网络,用户可以在其中上传和发布他们的图像和视频。现在,我不知道是否最好在我的数据库中创建一个表,将其命名为 media,并有一个 media_type 列或创建单独的图像和视频表。这
有n个线程可以访问的单例类。 每个线程加载此类的实例并在循环中调用此类的方法。 我必须控制执行流程,这样每个线程都可以调用第一个方法并暂停,只有在所有线程调用该方法一次之后,才必须恢复它们的工作。线程
存在参数数量未知(动态构建)的 MySQL 查询,其格式如下: SELECT * FROM actions WHERE user1 = ? AND user10 = ? AND user11 = ?
我检查了维基百科页面,但找不到它们之间的区别,两者似乎都将多类转换为多个线性分类器。 最佳答案 这是关于分割训练数据的策略。假设您有 N 个包含 C 类的数据样本。 一对一:在这里,您一次选择 2 个
我尝试在 sql 中插入多行。但它仅插入最后一行,并且在该行中仅存储每列的第一个字符。我通过 echo 打印查询,它只显示最后一行,但给出了每列的所有字符。另一件事是我通过单击提交按钮在两个表中插入值
我有两个实体:个人和公司。一家公司有一个或多个联系人(人)。公司至少有一个主要联系人(人)。实现这一点的最佳方法是什么? 实体如下: public class Person { public
我是 iOS 开发的新手,已经开始使用 Swift。我目前正在使用包含 3 个选项卡/导航的选项卡栏导航。我应该将 UIViewController 子类化并将其用于所有 3 个场景,还是每个场景都应
我的要求是,我需要打开两个窗口,但第二个窗口必须在第一个窗口打印并关闭后打开。可能吗? 但第二个窗口与第一个窗口同时打开。 HTML/JSP 代码打印 Java脚本函数打印(id){
经过几个小时的反复试验,我找到了这个 thread其中解释了如何建立具有相同两种类型的一对多关系和一对一关系。 但是,我无法让它与级联删除一起使用: Thrown: "Unable to determ
我想验证我的表单,如果任何输入字段为空,错误警告将显示在空白输入字段旁边。对于空白输入,错误信息必须一次全部输出,而不是一一显示。如何做到这一点? 下面是我的javascript代码: fun
我有一系列这样的字体值(命令分隔一行): Yeseva+One, Yrsa, ... 我正在寻找一个 SED(或其他 bash 工具表达式)来将每个值转换为这样的行语句: --font-yeseva-
我正在研究 中的核心音频转换服务 Learning Core Audio 我对他们 sample code 中的这个例子感到震惊: while(1) { // wrap the destina
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 9 年前。 Improve this qu
我是一名优秀的程序员,十分优秀!