示例#1
0
        private void CreateSocket()
        {
            try
            {
                sock = _socketFactory.CreateSocket(endpoint.IpAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp, _enableDualMode);
            }
            catch (SocketCreateException ex)
            {
                if (_enableDualMode && endpoint.IpAddress.Equals(IpAddressInfo.IPv6Any) &&
                    (string.Equals(ex.ErrorCode, "AddressFamilyNotSupported", StringComparison.OrdinalIgnoreCase) ||
                     // mono on bsd is throwing this
                     string.Equals(ex.ErrorCode, "ProtocolNotSupported", StringComparison.OrdinalIgnoreCase)))
                {
                    endpoint        = new IpEndPointInfo(IpAddressInfo.Any, endpoint.Port);
                    _enableDualMode = false;
                    sock            = _socketFactory.CreateSocket(endpoint.IpAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp, _enableDualMode);
                }
                else
                {
                    throw;
                }
            }

            sock.Bind(endpoint);

            // This is the number TcpListener uses.
            sock.Listen(2147483647);

            sock.StartAccept(ProcessAccept, () => _closed);
            _closed = false;
        }
示例#2
0
        //********************************************************************
        /// <summary> Function to start the SocketServer </summary>
        public void Start()
        {
            if (fIPAddress == null || fPort == 0)
            {
                // TODO:
                throw new Exception("Unable to start Socket server with specified values");
            }
            // Is the Socket already listening?
            if (!fIsRunning)
            {
                fIsShuttingDown = false;
                // Init the array of AdkSocketConnection references
                fSocketConnections = new ArrayList(fMaxClientConnections);
                IPEndPoint    ipEndpoint = new IPEndPoint(fIPAddress, fPort);
                AsyncCallback handler    = new AsyncCallback(AcceptNewConnection);

                // Call the derived class to begin accepting connections
                fAcceptSocket.Bind(ipEndpoint);
                fAcceptSocket.BeginAccept(handler, null);
                Debug("AdkSocketServer Started. Listening on port : {0}", new object[] { fPort });
                fIsRunning = true;
            }
        }