如何异步套接字句柄超时? [英] How to handle timeout in Async Socket?

查看:135
本文介绍了如何异步套接字句柄超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用异步套接字的发送信息至客户端,并期待从中响应code。如果客户在指定的内部就会没有回答认为超时。有些文章中互联网的建议使用WaitOne的,但是这将阻止该线程并按照使用I / O完成的目的。

什么是处理超时的异步接口的最佳方法是什么?

 子OnSend(BYVAL AR作为的IAsyncResult)
       昏暗的插座作为插座= CTYPE(ar.AsyncState,插座)
       socket.EndSend(AR)

       socket.BeginReceive(Me.ReceiveBuffer,0,Me.ReceiveBuffer.Length,SocketFlags.None,新的AsyncCallback(AddressOf的onReceive),插座)

 结束小组
 

解决方案

您不能超时或取消异步插槽操作。

所有你能做的就是开始自己的定时器这将关闭插槽 -The回调将被立即调用和 EndX 函数会回来与的ObjectDisposedException 如果你调用它。这里有一个例子:

 使用系统;
使用的System.Threading;
使用的System.Net.Sockets;

类AsyncClass
{
     插座袜子;
     定时器定时;
     byte []的缓冲区;
     INT timeoutflag;

     公共AsyncClass()
     {
          袜子=新的Socket(AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp);

          缓冲区=新的字节[256];
     }

     公共无效StartReceive()
     {
          IAsyncResult的解析度= sock.BeginReceive(缓冲,0,buffer.Length,
                SocketFlags.None,的onReceive,NULL);

          如果(!res.IsCompleted)
          {
                定时器=新的Timer(计时器触发,空,1000,Timeout.Infinite);
          }
     }

     无效的onReceive(IAsyncResult的RES)
     {
          如果(Interlocked.CompareExchange(参考timeoutflag,1,0)!= 0)
          {
                //该标志设置在其他地方,所以立即返回。
                返回;
          }

          //我们设置标志位为1,表明它已完成。

          如果(定时器!= NULL)
          {
                //从射击停止计时器。
                timer.Dispose();
          }

          //处理读取。

          INT LEN = sock.EndReceive(RES);
     }

     无效的OnTimer(obj对象)
     {
          如果(Interlocked.CompareExchange(参考timeoutflag 2,0)!= 0)
          {
                //该标志设置在其他地方,所以立即返回。
                返回;
          }

          //我们设置标志2,表明超时被击中。

          timer.Dispose();
          sock.Close(); //关闭套接字取消异步操作。
     }
}
 

I have a code that using async socket to send message to client and expecting response from it. If the client did not reply in a specified internal it will considers timeout. Some of the article in Internet suggest to use WaitOne, but this will blocks the thread and defers the purpose of using I/O completion.

What is the best way to handle timeout in async socket?

 Sub OnSend(ByVal ar As IAsyncResult)
       Dim socket As Socket = CType(ar.AsyncState ,Socket)
       socket.EndSend(ar)

       socket.BeginReceive(Me.ReceiveBuffer, 0, Me.ReceiveBuffer.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), socket)

 End Sub

解决方案

You can’t timeout or cancel asynchronous Socket operations.

All you can do is start your own Timer which closes the Socket—the callback will then be immediately called and the EndX function will come back with an ObjectDisposedException if you call it. Here's an example:

using System;
using System.Threading;
using System.Net.Sockets;

class AsyncClass
{
     Socket sock;
     Timer timer;
     byte[] buffer;
     int timeoutflag;

     public AsyncClass()
     {
          sock = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp);

          buffer = new byte[256];
     }

     public void StartReceive()
     {
          IAsyncResult res = sock.BeginReceive(buffer, 0, buffer.Length,
                SocketFlags.None, OnReceive, null);

          if(!res.IsCompleted)
          {
                timer = new Timer(OnTimer, null, 1000, Timeout.Infinite);
          }
     }

     void OnReceive(IAsyncResult res)
     {
          if(Interlocked.CompareExchange(ref timeoutflag, 1, 0) != 0)
          {
                // the flag was set elsewhere, so return immediately.
                return;
          }

          // we set the flag to 1, indicating it was completed.

          if(timer != null)
          {
                // stop the timer from firing.
                timer.Dispose();
          }

          // process the read.

          int len = sock.EndReceive(res);
     }

     void OnTimer(object obj)
     {
          if(Interlocked.CompareExchange(ref timeoutflag, 2, 0) != 0)
          {
                // the flag was set elsewhere, so return immediately.
                return;
          }

          // we set the flag to 2, indicating a timeout was hit.

          timer.Dispose();
          sock.Close(); // closing the Socket cancels the async operation.
     }
}

这篇关于如何异步套接字句柄超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆