Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            LahoreSocketClient client = new LahoreSocketClient();

            client.RaiseTextReceivedEvent  += HandleTextReceived;
            client.RaiseServerDisconnected += HandleServerDisconnected;
            client.RaiseServerConnected    += HandleServerConnected;

            Console.WriteLine("*** Welcome to Socket Client Starter Example by Naeem Akram Malik ***");
            Console.WriteLine("Please Type a Valid Server IP Address or hostname formatted as <HOST> and Press Enter: ");

            string strIPAddress = Console.ReadLine();

            Console.WriteLine("Please Supply a Valid Port Number 0 - 65535 and Press Enter: ");
            string strPortInput = Console.ReadLine();

            if (strIPAddress.StartsWith("<HOST>"))
            {
                strIPAddress = strIPAddress.Replace("<HOST>", string.Empty);
                strIPAddress = Convert.ToString(LahoreSocketClient.ResolveHostNameToIPAddress(strIPAddress));
            }

            if (string.IsNullOrEmpty(strIPAddress))
            {
                Console.WriteLine("No IP Address Supplied...");
                Environment.Exit(0);
            }

            if (!client.SetServerIPAddress(strIPAddress) ||
                !client.SetPortNumber(strPortInput))
            {
                Console.WriteLine(
                    string.Format(
                        "Wrong IP Address or port number supplied - {0} - {1} - Press a key to exit",
                        strIPAddress,
                        strPortInput));
                Console.ReadKey();
                return;
            }

            client.ConnectToServer();

            string strInputUser = null;

            do
            {
                strInputUser = Console.ReadLine();

                if (strInputUser.Trim() != "<EXIT>")
                {
                    client.SendToServer(strInputUser);
                }
                else if (strInputUser.Equals("<EXIT>"))
                {
                    client.CloseAndDisconnect();
                }
            } while (strInputUser != "<EXIT>");
        }