//public void CheckNetState() //{ // if (socket == null) // { // return; // } // //DateTime start = DateTime.Now; // UnityEngine.NetworkReachability state = UnityEngine.Application.internetReachability; // if (state == UnityEngine.NetworkReachability.NotReachable) // { // IsNetStateChange = true; // UnityEngine.Debug.Log("IsNetStateChange = true" + state.ToString()); // } // else if (NetState != state)//处理3G 2G的情况 // { // UnityEngine.Debug.Log("IsNetStateChange = true" + state.ToString()); // IsNetStateChange = true; // } // //UnityEngine.Debug.Log("CheckTime" + DateTime.Now.Subtract(start).TotalMilliseconds ); //} /// <summary> /// 打开连接 /// </summary> public void Open() { UnityEngine.NetworkReachability state = UnityEngine.Application.internetReachability; if (state != UnityEngine.NetworkReachability.NotReachable) { IPAddress[] address = Dns.GetHostEntry(_host).AddressList; IPAddress add_ip = address[0]; if (IsApplePlatform) { foreach (var addr in address) { if (addr.AddressFamily == AddressFamily.InterNetworkV6) { add_ip = addr; break; } } } if (add_ip.AddressFamily == AddressFamily.InterNetworkV6) { _socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); } else { _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } _socket.SendBufferSize = 8192; _socket.ReceiveBufferSize = 8192; //_socket.BeginConnect(add_ip, _port, new AsyncCallback(connect_callback), _socket);//异步connect _socket.Connect(add_ip, _port); Debug.Log("Socket connected " + add_ip + _port); //if (_heartbeatThread == null) //{ // _heartbeatThread = new Timer(SendHeartbeatPackage, null, HearInterval, HearInterval); // ReBuildHearbeat(); //} _thread = new Thread(new ThreadStart(CheckReceive)); _thread.Start(); } }
public void ConnectedServer() { UnityEngine.NetworkReachability state = UnityEngine.Application.internetReachability; if (state != UnityEngine.NetworkReachability.NotReachable) { NetWriter.SetUrl(serverIp + ":" + port); //if (mSocket == null) { string strUrl = NetWriter.GetUrl(); string[] arr = strUrl.Split(new char[] { ':' }); int nPort = int.Parse(arr[1]); mSocket = new SocketConnect(arr[0], nPort, HeadFormater, timeOut, hearInterval); } mSocket.EnsureConnected(); } else { QueueEvent(EventDef.ServerConnectFailure, "当前网络没有连接"); } }
public static Socket Connect(string host, int port) { timeoutEvent.Reset(); isConnectionSuccessful = false; UnityEngine.NetworkReachability state = UnityEngine.Application.internetReachability; if (state == UnityEngine.NetworkReachability.NotReachable) { return(null); } IPAddress address; if (System.Net.IPAddress.TryParse(host, out address) == false) { return(null); } Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ie = new IPEndPoint(address, port); socket.BeginConnect(ie, new AsyncCallback(CallbackConnect), socket); if (timeoutEvent.WaitOne(TimeoutMSec, false)) { if (isConnectionSuccessful) { return(socket); } else { return(null); } } else { socket.Close(); return(null); } }
/// <summary> /// 打开连接 /// </summary> public void Open() { UnityEngine.NetworkReachability state = UnityEngine.Application.internetReachability; if (state != UnityEngine.NetworkReachability.NotReachable) { _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { _socket.Connect(_host, _port); } catch { //socket.Dispose(); _socket = null; throw; } _netview = CreateThread(); _thread = new Thread(new ThreadStart(CheckReceive)); _thread.Start(); } }
public void startNet() { UnityEngine.NetworkReachability state = UnityEngine.Application.internetReachability; if (state != UnityEngine.NetworkReachability.NotReachable) { _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { _socket.Connect(_ip, _port); } catch (System.Exception) { _socket = null; throw; } _thread = new Thread(new ThreadStart(_threadEntry)); _thread.Start(); } //测试调用到lua //pushNetTask(onNetRecvError); //NetErrorCallback.Invoke(); }