コード例 #1
0
        /// <summary>
        /// 开始服务,监听客户端
        /// </summary>
        public void StartServer()
        {
            try
            {
                //实例化套接字(ip4寻址协议,流式传输,TCP协议)
                if (ProtocolType == ProtocolType.Tcp)
                {
                    _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType);
                }
                else if (ProtocolType == ProtocolType.Udp)
                {
                    _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType);
                }

                //创建ip对象
                IPAddress address = IPAddress.Parse(_ip);
                //创建网络节点对象包含ip和port
                IPEndPoint endpoint = new IPEndPoint(address, _port);
                //将 监听套接字绑定到 对应的IP和端口
                _socket.Bind(endpoint);
                //设置监听队列长度为Int32最大值(同时能够处理连接请求数量)
                if (ProtocolType == ProtocolType.Tcp)
                {
                    _socket.Listen(int.MaxValue);
                }

                //开始监听客户端
                StartListen();
                HandleServerStarted?.BeginInvoke(this, null, null);
            }
            catch (Exception ex)
            {
                HandleException?.BeginInvoke(ex, null, null);
            }
        }
コード例 #2
0
        private void InitServer()
        {
            _tcpSocketServer.HandleServerStarted = theServer =>
            {
                HandleServerStarted?.Invoke(this);
            };

            _tcpSocketServer.HandleException = ex =>
            {
                HandleException?.Invoke(ex);
            };

            _tcpSocketServer.HandleClientClose = (theServer, theCon) =>
            {
                var webCon = GetConnection(theCon.ConnectionId);
                CloseConnection(webCon);
                HandleClientClose?.Invoke(this, webCon);
            };

            _tcpSocketServer.HandleNewClientConnected = (theServer, theCon) =>
            {
                WebSocketConnection newCon = new WebSocketConnection(this, theCon)
                {
                    HandleClientClose = HandleClientClose == null ? null : new Action <WebSocketServer, WebSocketConnection>(HandleClientClose),
                    HandleSendMsg     = HandleSendMsg == null ? null : new Action <WebSocketServer, WebSocketConnection, string>(HandleSendMsg)
                };

                AddConnection(newCon);

                HandleNewClientConnected?.Invoke(this, newCon);
            };

            _tcpSocketServer.HandleRecMsg = (thServer, theCon, bytes) =>
            {
                string recStr = bytes.ToString(Encoding.UTF8);
                if (IsHandshake(recStr))
                {
                    string res = GetWebSocketResponse(recStr);

                    theCon.Send(res);
                }
                else
                {
                    int opcode = new string(bytes[0].ToBinString().Copy(4, 4).ToArray()).ToInt_FromBinString();

                    //为关闭连接
                    if (opcode == 8)
                    {
                        GetConnection(theCon.ConnectionId).Close();
                    }
                    else
                    {
                        string recData = AnalyticData(bytes);
                        HandleRecMsg?.Invoke(this, GetConnection(theCon.ConnectionId), recData);
                    }
                }
            };
        }
コード例 #3
0
        /// <summary>
        /// 关闭指定客户端连接
        /// </summary>
        /// <param name="theConnection">指定的客户端连接</param>
        #endregion


        #region 外部接口
        /// <summary>
        /// 开始服务,监听客户端
        /// </summary>
        public void StartServer()
        {
            try
            {
                //实例化套接字(ip4寻址协议,流式传输,TCP协议)
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                //开始侦听
                Listening();
                HandleServerStarted?.BeginInvoke(this, null, null);
            }
            catch (Exception ex)
            {
                HandleException?.BeginInvoke(ex, null, null);
            }
        }
コード例 #4
0
ファイル: SocketServer.cs プロジェクト: Jason-wang16/Test123
 public void StartServer()
 {
     try
     {
         _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         IPAddress  address  = IPAddress.Parse(_ip);
         IPEndPoint endpoint = new IPEndPoint(address, _port);
         _socket.Bind(endpoint);
         _socket.Listen(int.MaxValue);
         StartListen();
         HandleServerStarted?.Invoke(this);
     }
     catch (Exception ex)
     {
         HandleException?.Invoke(ex);
     }
 }