美文网首页
2018-10-26

2018-10-26

作者: 昵称违法 | 来源:发表于2018-10-26 21:29 被阅读0次

using System.Collections;

using System.Collections.Generic;

using System.Net.Sockets;

using UnityEngine;

/// <summary>

/// socket状态侦测

/// </summary>

public class DetectSocket {

    /// <summary>

    /// 检测socket连接状态

    /// </summary>

    /// <param name="s"></param>

    /// <returns></returns>

    public static bool SocketConnected(Socket s)

    {

        // Exit if socket is null

        if (s == null)

            return false;

        bool part1 = s.Poll(1000, SelectMode.SelectRead);

        bool part2 = (s.Available == 0);

        if (part1 && part2)

            return false;

        else

        {

            try

            {

                int sentBytesCount = s.Send(new byte[1], 1, 0);//todo 改成空包

                return sentBytesCount == 1;

            }

            catch

            {

                return false;

            }

        }

    }

}

相关文章

网友评论

      本文标题:2018-10-26

      本文链接:https://www.haomeiwen.com/subject/vbaltqtx.html