Esempio n. 1
0
        static bool TryCreateTcpSocket(ConfigurationManager configurationManager, string[] socketParts, out Socket socket)
        {
            socket = null;
            ushort port;

            try {
                if (socketParts.Length > 1)
                {
                    if (!UInt16.TryParse(socketParts [socketParts.Length - 1], out port))
                    {
                        Logger.Write(LogLevel.Error, "Error parsing port number");
                        return(false);
                    }
                }
                else
                {
                    port = configurationManager.Port;
                }
            } catch (ApplicationException e) {
                Logger.Write(LogLevel.Error, e.Message);
                return(false);
            }

            IPAddress address = configurationManager.Address;

            if (socketParts.Length == 3)
            {
                string address_str = socketParts [1];

                if (address_str == null)
                {
                    address = IPAddress.Loopback;
                }
                else if (!IPAddress.TryParse(address_str, out address))
                {
                    Logger.Write(LogLevel.Error,
                                 "Error in argument \"address\". \"{0}\" cannot be converted to an IP address.",
                                 address_str);
                    return(false);
                }
            }

            try {
                socket = new TcpSocket(address, port);
            } catch (System.Net.Sockets.SocketException e) {
                Logger.Write(LogLevel.Error,
                             "Error creating the socket: {0}",
                             e.Message);
                return(false);
            }

            Logger.Write(LogLevel.Debug,
                         "Listening on port: {0}", port);
            Logger.Write(LogLevel.Debug,
                         "Listening on address: {0}", address);
            return(true);
        }
Esempio n. 2
0
File: main.cs Progetto: zofuthan/xsp
        static bool TryCreateTcpSocket(string ip, int port, out Socket socket)
        {
            socket = null;

            IPAddress address;

            if (!IPAddress.TryParse(ip, out address))
            {
                return(false);
            }

            socket = new TcpSocket(address, port);
            return(true);
        }
Esempio n. 3
0
        static bool TryCreateTcpSocket(string ip, int port, out Socket socket)
        {
            socket = null;

            IPAddress address;

            if (!IPAddress.TryParse(ip, out address))
            {
                Logger.Write(LogLevel.Error, "Error in argument \"address\". \"{0}\" cannot be converted to an IP address.", ip);
                return(false);
            }

            socket = new TcpSocket(address, port);
            return(true);
        }