public static void connectToServer() { try { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(new IPEndPoint(MainWindow.serverIP, MainWindow.connectRemotePort)); byte[] bufferToSend = Encoding.Unicode.GetBytes(passwdDigest); Utility.SendBytes(socket, bufferToSend, bufferToSend.Length, SocketFlags.None); byte[] response = new byte[4]; if (!Utility.ReceiveBytes(socket, response, response.Length, SocketFlags.None)) { throw new Exception(); } if (Encoding.Unicode.GetString(response).Equals("OK")) { MessageBox.Show("Connected"); InputEventTransfer.Target = new IPEndPoint(MainWindow.serverIP, MainWindow.inputRemotePort); ClipboardNetworkChannel.StartService(MainWindow.clipboardLocalPort); ClipboardTrasfer.Target = new IPEndPoint(MainWindow.serverIP, MainWindow.clipboardRemotePort); OnClientConnected(); } else { MessageBox.Show("Wrong password"); ConnectWindow.worker.ReportProgress(70); Utility.ShutdownSocket(socket); } } catch (Exception e) { MessageBox.Show( e.Message); ConnectWindow.worker.ReportProgress(50); Utility.ShutdownSocket(socket); ClipboardTrasfer.StopService(); ClipboardNetworkChannel.StopService(); InputEventTransfer.StopService(); } }
public static void disconnectFromServer() { try { byte[] bufferBye = Encoding.Unicode.GetBytes("BYE"); Utility.SendBytes(socket, bufferBye, bufferBye.Length, SocketFlags.None); } catch (Exception) { Utility.ShutdownSocket(socket); } finally { MainWindow.keep.DeadClient = null; OnClientDisconnected(); InputEventTransfer.StopService(); ClipboardNetworkChannel.StopService(); ClipboardTrasfer.StopService(); Utility.ShutdownSocket(socket); MessageBox.Show("Disconnected"); } }