zoukankan      html  css  js  c++  java
  • Socket.Poll()

    Socket.Poll()

    public bool Poll (

                        int microSeconds,

                        SelectMode mode

    )

    MSDN:

    Poll 方法将会检查 Socket 的状态。指定 selectMode 参数的 SelectMode.SelectRead,可确定 Socket 是否为可读。指定 SelectMode.SelectWrite,可确定 Socket 是否为可写。使用 SelectMode.SelectError 检测错误条件。Poll 将在指定的时段(以 microseconds 为单位)内阻止执行。如果希望无限期的等待响应,则将 microSeconds 设置为一个负整数。

    int  microSeconds,

    MSDN:

    等待响应的时间(以微秒为单位)。

    自己理解:

    是Poll程序中断运行时间。 如microseconds=1000;Poll阻塞1000微秒,microseconds<0将无限等待响应。

     

    SelectMode mode

     public enum SelectMode

     {

         SelectRead = 0,  //     读状态模式。

         SelectWrite = 1, //     写状态模式。

         SelectError = 2, //     错误状态模式。

     }

    MSDN:

    模式(SelectMode

    返回(return

    SelectRead

    1.  如果已调用Listen并且有挂起的连接,则为true。

    2.如果有数据可供读取,则为true。

    3.如果连接已关闭、重置或终止,则返回true。

    SelectWrite

    1.  如果正在处理Connect并且连接已成功,则为true。

    2.  如果可以发送数据,则返回true。

    SelectError

    1.  如果正在处理不阻止的Connect,并且连接已失败,则为true。

    2.  如果OutOfBandInline未设置,并且带外数据可用,则为true。

    自己理解:

    只对红色部分理解。

             2010.8.30

    MSDN例子:

    //Creates the Socket for sending data over TCP.

    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,

       ProtocolType.Tcp );

     

    // Connects to host using IPEndPoint.

    s.Connect(EPhost);

    if (!s.Connected)

    {

       strRetPage = "Unable to connect to host";

    }

    // Use the SelectWrite enumeration to obtain Socket status.

     if(s.Poll(-1, SelectMode.SelectWrite)){

          Console.WriteLine("This Socket is writable.");

     }

     else if (s.Poll(-1, SelectMode.SelectRead)){

            Console.WriteLine("This Socket is readable." );

     }

     else if (s.Poll(-1, SelectMode.SelectError)){

          Console.WriteLine("This Socket has an error.");

     }

     

    自己例子:

    protected override void ProcMessage()

            {

                int microSeconds = 50;

                EndPoint senderRemote = socket.RemoteEndPoint;

                int dataLen, msgLen = 0;

                try

                {

                    if (socket.Poll(microSeconds, SelectMode.SelectRead))

                    {

                        dataLen = ReceiveFrom(m_ReceiveBuf, m_ReceiveBuf.Length, socket);

                        if (EClass.Message.Message.Valid(m_ReceiveBuf))

                            MessageParse(m_ReceiveBuf, dataLen, (IPEndPoint)senderRemote);

                    }

                }

                catch (SocketException se)

                {

                    SocketError err = se.SocketErrorCode;

                }

            }

     原文地址:http://blog.sina.com.cn/s/blog_43eee55a0100l87i.html

  • 相关阅读:
    Neutron 理解 (1): Neutron 所实现的虚拟化网络 [How Netruon Virtualizes Network]
    openstack里面的Provider network 和 Tenant network 的区别
    Openstack网络相关概念比较复杂,经常使人混淆,本文进行相关说明。
    OpenStack 网络:Neutron 初探
    openstack 网络架构 nova-network + neutron
    Java 性能测试的四项原则
    微信公众号开发之微信买单
    微信公众号开发之如何使用JSSDK
    微信公众号开发之如何一键导出微信所有用户信息到Excel
    [内核驱动] 链表LIST_ENTRY的操作(转)
  • 原文地址:https://www.cnblogs.com/gusongbanyue/p/5774988.html
Copyright © 2011-2022 走看看