void kh_KeyIntercepted(KeyboardHook.KeyboardHookEventArgs e) { try { /** Invia l' evento tastiera soltanto se form2 è in primo piano ***/ if (this.IsActive(this.Handle) == false) { Program.kh.AllowKey = true; return; } // Console.WriteLine(e.KeyName); //ds.Draw(e.KeyName); if (e.WParam == (IntPtr)WM_KEYUP || e.WParam == (IntPtr)WM_SYSKEYUP) { byte[] msg = new byte[2]; msg[0] = (byte)e.KeyCode; int bytesSent; msg[1] = (byte)'U'; bytesSent = client.getCurrentKeyboardSocket().Send(msg); } if (e.WParam == (IntPtr)WM_KEYDOWN || e.WParam == (IntPtr)WM_SYSKEYDOWN) { byte[] msg = new byte[2]; msg[0] = (byte)e.KeyCode; msg[1] = (byte)'D'; // Send the data through the socket. client.getCurrentKeyboardSocket().Send(msg); // Console.WriteLine("Testo: " + e.KeyData); } } catch (SocketException ex) { MessageBox.Show("Il server ha terminato la connessione sock ex"); client.closeAllSocket(); form.changeStatusListView(" ", true); this.Close(); return; } catch (Exception ex) { MessageBox.Show("Il server ha terminato la connessione general ex"); form.changeStatusListView(" ", true); client.closeAllSocket(); this.Close(); return; } }
public int recvFile(string path) { string fileName; try { //ricevo il nome del file client.getClipboardReciveSocket().Receive(clientData); int fileNameLen = BitConverter.ToInt32(clientData, 0); fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen); long dim = BitConverter.ToInt64(clientData, fileNameLen + 4); // byte[] msg2 = Encoding.ASCII.GetBytes("+OK" + fileName); client.getClipboardReciveSocket().Send(msg); BinaryWriter bWrite = new BinaryWriter(File.Open(path + fileName, FileMode.Append)); // Console.WriteLine("Nome File: " + fileName); // Console.WriteLine("Dimensione File: " + dim); bool errorFile = false; int ricevuti = 0; long dimBar = dim; ProgressBar bar = new ProgressBar(); // Thread backgroundThread = new Thread(new ThreadStart(() => { bar.SetBar((int)dim); // Iterate from 0 - 99 // On each iteration, pause the thread for .05 seconds, then update the dialog's progress bar try { while (dim > 0) { if (client.getClipboardReciveSocket().Poll(3000000, SelectMode.SelectRead)) { byteRead = client.getClipboardReciveSocket().Receive(clientData); ricevuti = ricevuti + byteRead; string response = Encoding.ASCII.GetString(clientData, 0, byteRead); if (response.Equals("-ERR\r\n"))//il server mi manda -ERR quando durante un trasferimento file { bWrite.Close(); // Close the dialog if it hasn't been already if (bar.InvokeRequired) { bar.BeginInvoke(new Action(() => { bar.showdialogset(true); bar.Close(); bar.showdialogset(false); })); } MessageBox.Show("-ERR, connessione interrotta"); errorFile = true; return; } // bar.UpdateProgress(byteRead, (int)dimBar, fileName); bWrite.Write(clientData, 0, byteRead); dim = dim - byteRead; } else { bWrite.Close(); // Close the dialog if it hasn't been already if (bar.InvokeRequired) { bar.BeginInvoke(new Action(() => { bar.showdialogset(true); bar.Close(); bar.showdialogset(false); })); } MessageBox.Show("Errore nel trasferimento, connessione interrotta"); errorFile = true; return; } } } catch (Exception ex) { bWrite.Close(); // Close the dialog if it hasn't been already if (bar.InvokeRequired) { bar.BeginInvoke(new Action(() => { bar.showdialogset(true); bar.Close(); bar.showdialogset(false); })); } MessageBox.Show("Errore nel trasferimento, connessione interrotta"); errorFile = true; return; } bWrite.Close(); client.getClipboardReciveSocket().Send(msg); if (bar.InvokeRequired) { bar.BeginInvoke(new Action(() => { bar.showdialogset(true); bar.Close(); bar.showdialogset(false); })); } } )); // Start the background process thread backgroundThread.Start(); //****Il caso in cui il client chiede la finestra del trasferimento****// if (bar.ShowDialog() == DialogResult.OK) { Console.WriteLine("Ho interrotto il trasferimento"); bWrite.Close(); client.currentSocket.Send(Encoding.ASCII.GetBytes("-ERR\r\n<EOF>")); backgroundThread.Abort(); return(-1); } backgroundThread.Join(); if (errorFile) { errorFile = false; return(-1); } if (path == @"C:\local\") { listFileName.Add(path + fileName); } return(1); } catch (SocketException ex) { MessageBox.Show("Il server ha terminato la connessione"); client.closeAllSocket(); return(-1); } catch (Exception ex) { MessageBox.Show("Il server ha terminato la connessione"); client.closeAllSocket(); return(-1); } }