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; } }
public void AcceptCallback(IAsyncResult ar) { Socket listener = null; Socket handler = null; try { listener = (Socket)ar.AsyncState; EndPoint endpoint = listener.LocalEndPoint; handler = listener.EndAccept(ar); handler.NoDelay = true; StateObject state = new StateObject(); state.workSocket = handler; if (endpoint.GetHashCode() == controlSocket.LocalEndPoint.GetHashCode()) { receiveControl = handler; receiveControl.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallbackControl), state); } if (endpoint.GetHashCode() == keyboardSocket.LocalEndPoint.GetHashCode()) { receiveKeyboard = handler; receiveKeyboard.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallbackKeyboard), state); } if (endpoint.GetHashCode() == clipboardSocket.LocalEndPoint.GetHashCode()) { receiveClipboard = handler; CSender = new ClipboardSender(receiveClipboard); } AsyncCallback aCallback = new AsyncCallback(AcceptCallback); if (endpoint.GetHashCode() == controlSocket.LocalEndPoint.GetHashCode()) { controlSocket.BeginAccept(aCallback, controlSocket); } if (endpoint.GetHashCode() == keyboardSocket.LocalEndPoint.GetHashCode()) { keyboardSocket.BeginAccept(aCallback, keyboardSocket); } if (endpoint.GetHashCode() == clipboardSocket.LocalEndPoint.GetHashCode()) { clipboardSocket.BeginAccept(aCallback, clipboardSocket); } } catch (ObjectDisposedException) { } }