public ServerInboxWindow(ServerForm parent, TcpClient clientSocket, Button btnInbox)
 {
     InitializeComponent();
     this.parent       = parent;
     this.clientSocket = clientSocket;
     this.btnInbox     = btnInbox;
 }
        private void btnServer_Click(object sender, EventArgs e)
        {
            ServerForm server = new ServerForm(parent);

            server.Show();
            this.Hide();
        }
Exemplo n.º 3
0
        // Thread
        private void ServerThread(ServerForm main)
        {
            try
            {
                main.serverSocket.Start();
            } catch
            {
                MessageBox.Show("The port is invalid or already in use.", "Error");
                serverSocket = null;
                main.Invoke(new MethodInvoker(delegate()
                {
                    main.btnClose.PerformClick();
                }));
                return;
            }
            try
            {
                main.clientSocket = serverSocket.AcceptTcpClient();
            } catch (SocketException e)
            {
                return;
            }

            NetworkStream stream = new NetworkStream(clientSocket.Client, false);

            byte[] buffer     = new byte[1024];
            int    bytesCount = stream.Read(buffer, 0, buffer.Length);

            stream.Close();
            string ip = Encoding.UTF8.GetString(buffer, 0, bytesCount);

            if (Passed(main, clientSocket, ip) == false)
            {
                return;
            }

            main.serverShellWindow = new ServerShellWindow(main, btnShell);
            main.serverInboxWindow = new ServerInboxWindow(main, clientSocket, btnInbox);
            main.Invoke(new MethodInvoker(delegate()
            {
                main.Text = $"{ip} connected.";
                main.serverShellWindow.Show();
                main.serverInboxWindow.Show();
            }));
            main.terminal = new Terminal(this);
            main.terminal.Start();
            main.listenThread = new Thread(o => ListenThread(main));
            listenThread.Start();

            ConnectionChanged(true);

            // Bắt đầu viết log vì client đã được server cho phép truy cập
            log = new Log(parent.pathLogFolder, ip);
        }
        private void SendInbox(ServerForm parent, ServerInboxWindow main, TcpClient clientSocket)
        {
            string data = main.serverInboxInput.Text;

            Storage.RichTextBoxAppend(main.serverInbox, $"You: {data}");
            parent.serverInbox = $"You: {data}";

            NetworkStream stream = new NetworkStream(clientSocket.Client, false);

            byte[] buffer = Encoding.UTF8.GetBytes($"i{data}");
            stream.Write(buffer, 0, buffer.Length);
            stream.Close();

            Storage.TextBoxClear(main.serverInboxInput);
        }
Exemplo n.º 5
0
        private bool Passed(ServerForm main, TcpClient clientSocket, string ip)
        {
            int mode = 0;

            filter.Invoke(new MethodInvoker(delegate()
            {
                mode = filter.SelectedIndex;
            }));

            if (mode == 0)
            {
                return(true);
            }
            bool inList;

            try
            {
                inList = ips.Contains(IPAddress.Parse(ip));
            }
            catch
            {
                DenyConnection(clientSocket);
                (new Thread(o => ServerThread(main))).Start();
                MessageBox.Show("Unknown IP tried to connect.", "Deny");
                return(false);
            }

            if (mode == 1)
            {
                if (inList)
                {
                    return(true);
                }
                DenyConnection(clientSocket);
                (new Thread(o => ServerThread(main))).Start();
                MessageBox.Show($"{ip} tried to connect.", "Deny");
                return(false);
            }

            if (!inList)
            {
                return(true);
            }
            DenyConnection(clientSocket);
            (new Thread(o => ServerThread(main))).Start();
            MessageBox.Show($"{ip} tried to connect.", "Deny");
            return(false);
        }
Exemplo n.º 6
0
        private void ShellReceiveThread(ServerForm main, string data)
        {
            data = data.Substring(1);
            if (data == "CTRL+C")
            {
                main.TerminalBreak(main);
                return;
            }
            if (data == "cls" || data == "clear")
            {
                main.serverShellWindow.ClearShell();
                return;
            }

            // ghi vao log lenh duoc thuc thi
            log.updateLog("Input: " + data + '\n');
            log.updateLog("=> Output:\n");


            main.terminal.executeCommand(data);
        }
Exemplo n.º 7
0
        private void ListenThread(ServerForm main)
        {
            Storage.BtnEnabledInvoke(main.btnShell, true);
            Storage.BtnEnabledInvoke(main.btnInbox, true);
            NetworkStream stream = new NetworkStream(main.clientSocket.Client, false);

            byte[] buffer = new byte[1024];
            while (Thread.CurrentThread.IsAlive)
            {
                int bytesCount = 0;
                try
                {
                    bytesCount = stream.Read(buffer, 0, buffer.Length);
                } catch { }
                if (bytesCount == 0)
                {
                    break;
                }
                string data = Encoding.UTF8.GetString(buffer, 0, bytesCount);

                if (data[0] == 'i')
                {
                    (new Thread(o => InboxReceiveThread(main, data))).Start();//serverInboxWindow, ref serverInbox, data))).Start();
                }
                else if (data[0] == 's')
                {
                    (new Thread(o => ShellReceiveThread(main, data))).Start();//clientSocket, serverShellWindow, ref serverShell, data))).Start();
                }
                else if (data == @"!@#$%^&*()_+EXIT!@#$%^&*()_+")
                {
                    (new Thread(PartnerLeft)).Start();
                    break;
                }
            }
            stream.Close();
            Storage.BtnEnabledInvoke(main.btnShell, false);
            Storage.BtnEnabledInvoke(main.btnInbox, false);
        }
 public Terminal(ServerForm parent)
 {
     this.parent = parent;
     __constructor();
 }
Exemplo n.º 9
0
 private void InboxReceiveThread(ServerForm main, string data)//ServerInboxWindow serverInboxWindow, ref string serverInbox, string data)
 {
     data              = $"Partner: {data.Substring(1)}";
     main.serverInbox += data;
     main.serverInboxWindow.UpdateInbox(data);
 }
Exemplo n.º 10
0
 public void TerminalBreak(ServerForm main)
 {
     main.terminal.__destructor();
     main.terminal = new Terminal(main);
     main.terminal.Start();
 }
Exemplo n.º 11
0
 public ServerShellWindow(ServerForm parent, Button btnShell)
 {
     InitializeComponent();
     this.parent   = parent;
     this.btnShell = btnShell;
 }