Ejemplo n.º 1
0
        private void Connect()
        {
            InitControlSocket(workingPort);

            string passwordMessage = workingPassword + "<PasswordCheck>";

            byte[] passwordToByte = Encoding.Unicode.GetBytes(passwordMessage);
            int    bytesSend      = controlSocket.Send(passwordToByte);

            int pwdAccepted = ReceivePasswordCheck();

            if (pwdAccepted == 1)
            {
                InitKeyboardSocket(workingPort + 10);
                InitMouseSocket(workingPort + 20);
                InitClipboardSocket(workingPort + 30);
                CSender = new ClipboardSender(clipboardSocket);
                ListenFromServer();

                tbConnectionStatus.Text     = "Client connected to " + controlSocket.RemoteEndPoint.ToString();
                Connect_Button.IsEnabled    = false;
                Disconnect_Button.IsEnabled = true;
                InitHooks();

                Thread tt = new Thread(() =>
                {
                    while (clipboardSocket.Connected)
                    {
                        Thread t = new Thread(() => CSender.ReceiveClipboard());
                        t.SetApartmentState(ApartmentState.STA);
                        t.Start();
                        t.Join();
                    }
                });
                tt.Start();
                connected = true;
            }
            else if (pwdAccepted == 0)
            {
                controlSocket.Close();
                Disconnect_Button.IsEnabled = false;
                Connect_Button.IsEnabled    = true;
                MessageBox.Show("Wrong Password");
            }
            else
            {
                MessageBox.Show("Server could not check password, try changing port");
                Disconnect_Button.IsEnabled = false;
                Connect_Button.IsEnabled    = true;
            }
        }
Ejemplo n.º 2
0
        public void ReceiveCallbackControl(IAsyncResult ar)
        {
            try
            {
                string content = string.Empty;

                if (receiveControl.Connected)
                {
                    StateObject state   = (StateObject)ar.AsyncState;
                    Socket      handler = state.workSocket;

                    int bytesRead = handler.EndReceive(ar);

                    if (bytesRead > 0)
                    {
                        content += Encoding.Unicode.GetString(state.buffer, 0, bytesRead);
                        if (content.IndexOf("<PasswordCheck>") > -1)
                        {
                            string str = content.Substring(0, content.LastIndexOf("<PasswordCheck>"));
                            if (CheckPassword(str, handler))
                            {
                                keyboardSocket.Listen(1);
                                AsyncCallback aCallback2 = new AsyncCallback(AcceptCallback);
                                keyboardSocket.BeginAccept(aCallback2, keyboardSocket);
                                clipboardSocket.Listen(1);
                                AsyncCallback aCallback3 = new AsyncCallback(AcceptCallback);
                                clipboardSocket.BeginAccept(aCallback3, clipboardSocket);

                                InitMouseSocket();
                                StartListenMouse();

                                ni.Icon = iconConnected;



                                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallbackControl), state);

                                Thread tt = new Thread(() =>
                                {
                                    while (receiveClipboard.Connected)
                                    {
                                        Thread t = new Thread(() => CSender.ReceiveClipboard());
                                        t.SetApartmentState(ApartmentState.STA);
                                        t.Start();
                                        t.Join();
                                    }
                                });
                                tt.Start();
                                connected = true;
                            }
                        }
                        else if (content.IndexOf("<Disconnect>") > -1)
                        {
                            ni.Icon           = iconDisconnected;
                            ni.BalloonTipText = "Client is disconnected";
                            ni.ShowBalloonTip(3000);

                            receiveControl.Shutdown(SocketShutdown.Both);
                            receiveControl.Close();
                            receiveControl.Dispose();

                            receiveKeyboard.Shutdown(SocketShutdown.Both);
                            receiveKeyboard.Close();
                            receiveKeyboard.Dispose();

                            receiveClipboard.Shutdown(SocketShutdown.Both);
                            receiveClipboard.Close();
                            receiveClipboard.Dispose();

                            connected = false;

                            mouseSocket.Shutdown(SocketShutdown.Both);
                            mouseSocket.Close();
                        }
                        else if (content.IndexOf("<Stop>") > -1)
                        {
                            ni.Icon           = iconPause;
                            ni.BalloonTipText = "Client stopped control";
                            ni.ShowBalloonTip(3000);
                            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallbackControl), state);
                        }
                        else if (content.IndexOf("<Restart>") > -1)
                        {
                            ni.Icon           = iconConnected;
                            ni.BalloonTipText = "Client restarted control";
                            ni.ShowBalloonTip(3000);
                            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallbackControl), state);
                        }
                        else
                        {
                            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallbackControl), state);
                        }
                    }
                }
            }
            catch (ObjectDisposedException) { } //Need this when the socket are closed, exception not managed since we don't care if we loose some data
            catch (SocketException) { }
        }