private void StartListen() { try { _socket.BeginAccept(asyncResult => { try { Socket newSocket = _socket.EndAccept(asyncResult); //马上进行下一轮监听,增加吞吐量 if (_isListen) { StartListen(); } SocketConnection newConnection = new SocketConnection(newSocket, this) { HandleRecMsg = HandleRecMsg == null ? null : new Action <byte[], SocketConnection, SocketServer>(HandleRecMsg), HandleClientClose = HandleClientClose == null ? null : new Action <SocketConnection, SocketServer>(HandleClientClose), HandleSendMsg = HandleSendMsg == null ? null : new Action <byte[], SocketConnection, SocketServer>(HandleSendMsg), HandleException = HandleException == null ? null : new Action <Exception>(HandleException) }; newConnection.StartRecMsg(); AddConnection(newConnection); HandleNewClientConnected?.BeginInvoke(this, newConnection, null, null); } catch (Exception ex) { HandleException?.BeginInvoke(ex, null, null); } }, null); } catch (Exception ex) { HandleException?.BeginInvoke(ex, null, null); } }
/// <summary> /// 开始服务,监听客户端 /// </summary> public void StartServer() { try { //实例化套接字(ip4寻址协议,流式传输,TCP协议) _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建ip对象 IPAddress address = IPAddress.Parse(_ip); //创建网络节点对象包含ip和port IPEndPoint endpoint = new IPEndPoint(address, _port); //将 监听套接字绑定到 对应的IP和端口 _socket.Bind(endpoint); //设置监听队列长度为Int32最大值(同时能够处理连接请求数量) _socket.Listen(int.MaxValue); //开始监听客户端 StartListen(); HandleServerStarted?.BeginInvoke(this, null, null); } catch (Exception ex) { HandleException?.BeginInvoke(ex, null, null); } }
/// <summary> /// 发送数据 /// </summary> /// <param name="bytes">数据</param> /// <param name="iPEndPoint">目标地址</param> public void Send(byte[] bytes, IPEndPoint iPEndPoint) { try { _udpClient.BeginSend(bytes, bytes.Length, iPEndPoint, asyncCallback => { try { int length = _udpClient.EndSend(asyncCallback); HandleSendMsg?.BeginInvoke(this, iPEndPoint, bytes, null, null); } catch (Exception ex) { HandleException?.BeginInvoke(ex, null, null); } }, null); } catch (Exception ex) { HandleException?.BeginInvoke(ex, null, null); } }
/// <summary> /// 关闭与服务器的连接 /// </summary> public void Close() { try { _isRec = false; _socket.BeginDisconnect(false, asyncCallback => { try { _socket.EndDisconnect(asyncCallback); } catch (Exception ex) { HandleException?.BeginInvoke(ex, null, null); } finally { _socket.Dispose(); } }, null); } catch (Exception ex) { HandleException?.BeginInvoke(ex, null, null); } finally { try { HandleClientClose?.Invoke(this); } catch (Exception ex) { HandleException?.Invoke(ex); } } }
/// <summary> /// 关闭当前连接 /// </summary> public void Close() { if (_isClosed) { return; } try { _isClosed = true; _server.RemoveConnection(this); _isRec = false; _socket.BeginDisconnect(false, (asyncCallback) => { try { _socket.EndDisconnect(asyncCallback); } catch (Exception ex) { HandleException?.BeginInvoke(ex, null, null); } finally { _socket.Dispose(); } }, null); } catch (Exception ex) { HandleException?.BeginInvoke(ex, null, null); } finally { HandleClientClose?.BeginInvoke(_server, this, null, null); } }