Example #1
0
        public static void ThrowServerException(SocketException e, ServerContext serverContext = null)
        {
            string ipAddress = serverContext != null ? serverContext.IPAddress.ToString() : "unknown";
            string port = serverContext != null ? serverContext.Port.ToString() : "-";

            if (ipAddress.Equals("0.0.0.0")) ipAddress = "any";

            if (e.Message.StartsWith("Only one usage of each socket address"))
                throw new ServerException(
                    string.Format(
                        "Can't bind to socket on port {0} for IP addresses ({1}) because it is already in use (is the server already running?)",
                        port, ipAddress), e);

            throw e;
        }
Example #2
0
        // caller should catch socket exceptions
        public void Start()
        {
            try
            {
                if (_isStopped)
                {
                    _serverContext = new ServerContext {IPAddress = IPAddress.Any, Port = PolicyServerPort};

                    _listener = new TcpListener(_serverContext.IPAddress, _serverContext.Port);
                    _listener.Start();

                    // This call returns immediately; waiting for a client to connect happens on a separate thread
                    ListenForNewConnection();

                    _isStopped = false;
                    Console.WriteLine("start success");
                }
            }
            catch (SocketException e)
            {
                ServerException.ThrowServerException(e, _serverContext);
            }
        }