/******************************************************************************** * 文件名:Socket.cs * 全路径: \NetWork\SocketAPI\Socket.cs * 创建人: 王华 * 创建时间:2013-11-29 * * 功能说明: Socket类 * * 修改记录: *********************************************************************************/ #if UNITY_WP8 using UnityPortSocket; #else using System.Net.Sockets; #endif using System.Net; using SPacket.Socket_API; namespace SPacket.SocketInstance { public class SocketInstance { private string m_host; private int m_port; private Socket m_SocketID; public bool IsValid { get { return m_SocketID != null; } } public bool IsConnected { get { return m_SocketID != null && m_SocketID.Connected; } } public uint Send(byte[] buf, int nLen, SocketFlags flags = SocketFlags.None) { return SocketAPI.Send(m_SocketID, buf, (uint) nLen, flags); } public uint receive(byte[] buf, int nLen, uint flags = 0) { return SocketAPI.Recv(m_SocketID, buf, (uint) nLen, flags); } public void close() { if (IsValid) { SocketAPI.Close(m_SocketID); m_SocketID = null; } } public EndPoint GetLocalEndPoint() { return m_SocketID == null ? null : m_SocketID.LocalEndPoint; } public uint available() { return SocketAPI.available(m_SocketID); } public string connect(string IP, int port) { m_host = IP; m_port = port; var results = ""; m_SocketID = SocketAPI.Connect(m_host, m_port, ref results); return results; } public bool IsCanSend() { if (m_SocketID != null) if (m_SocketID.Poll(0, SelectMode.SelectWrite)) return true; return false; } public bool IsCanReceive() { if (m_SocketID != null) if (m_SocketID.Poll(0, SelectMode.SelectRead)) return true; return false; } } }