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;
}
}
}
}
网友评论