public void Connect(string ip, int port) { Debuger.Log("连接服务端 IP {0} Port {1} ", ip, port); IP = ip; remoteIp = ip; remotePort = port; remotEndPoint = IPUtility.GetHostEndPoint(ip, port); currentSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); currentSocket.Bind(new IPEndPoint(IPAddress.Any, Port)); kcp = new KCP(0, HandleKcpSend); kcp.NoDelay(1, 10, 2, 1); kcp.WndSize(128, 128); Connected = true; threadRecv = new Thread(ThreadRecv) { IsBackground = true }; threadRecv.Start(); }
public void Connect(string ip, int port) { IP = ip; remoteIp = ip; remotePort = port; Connected = true; remotEndPoint = IPUtility.GetHostEndPoint(ip, port); try { currentSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); currentSocket.Bind(new IPEndPoint(IPAddress.Any, Port)); currentSocket.Connect(ip, port); currentSocket.BeginReceive(cacheBuffer, 0, cacheBuffer.Length, SocketFlags.None, ReceiveCallBack, cacheBuffer); } catch (Exception e) { Debuger.LogError(e); } }
public bool Connect(string ipAddress, int listenPort) { if (currentSocket != null) { Debuger.LogError("无法建立连接,需要先关闭上一次连接!"); return(false); } ip = ipAddress; port = listenPort; lastRecvTimestamp = (uint)TimeUtility.GetTotalMillisecondsSince1970(); try { remoteEndPoint = IPUtility.GetHostEndPoint(ip, port); if (remoteEndPoint == null) { Debuger.LogError("无法将Host解析为IP!"); Close(); return(false); } currentSocket = new Socket(remoteEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp); currentSocket.Bind(IPUtility.GetIPEndPointAny(AddressFamily.InterNetwork, 0)); isRunning = true; threadRecv = new Thread(ThreadRecv) { IsBackground = true }; threadRecv.Start(); } catch (Exception e) { Debuger.LogError(e.Message + e.StackTrace); Close(); return(false); } return(true); }
private void SendMessage(int dstID, byte[] bytes, int len) { int dstPort = IPCConfig.GetIPCInfo(dstID).Port; IPEndPoint ep = IPUtility.GetHostEndPoint("127.0.0.1", dstPort); currentSocket.SendTo(bytes, 0, len, SocketFlags.None, ep); }