Example #1
0
        public void sendfile()
        {
            try
            {
                if (server != null)
                {
                    if (Clipboard.ContainsText(TextDataFormat.Text))
                    {
                        if (clipb != null)
                        {
                            if (server != null && server.GetConnected())
                            {
                                clipb.textselected = (string)Clipboard.GetDataObject().GetData(DataFormats.Text);
                                clipb.valore       = 0;
                                if (clipbthread != null && clipbthread.IsAlive)
                                {
                                    MessageBox.Show("Attenzione, trasferimento clipboard gia in atto, interrompere prima il trasferimento corrente");
                                }
                                else
                                {
                                    clipbthread              = new Thread(new ThreadStart(clipb.sendData));
                                    clipbthread.Name         = "clip thread";
                                    clipbthread.IsBackground = true;
                                    clipbthread.Start();
                                    return;
                                }
                                // Console.WriteLine("sto svegliando la senddata");
                                // autoEvent.Set();
                            }
                            else
                            {
                                MessageBox.Show("non è possibile inviare la clipboard");
                                return;
                            }
                        }
                    }
                    if (Clipboard.ContainsFileDropList())
                    {
                        if (clipb != null)
                        {
                            if (server != null && server.GetConnected())
                            {
                                clipb.filename.Clear();
                                clipb.filetotallen = 0;

                                for (int i = 0; i < Clipboard.GetFileDropList().Count; i++)
                                {
                                    clipb.valore = 1;
                                    clipb.setparam(Clipboard.GetFileDropList()[i].ToString());
                                    //}
                                }

                                clipb.SetServer(server);

                                clipb.form1 = this;


                                clipbthread              = new Thread(new ThreadStart(clipb.sendData));
                                clipbthread.Name         = "clip thread";
                                clipbthread.IsBackground = true;
                                clipbthread.Start();
                                return;
                                //clipbthread.Join();
                            }
                            else
                            {
                                MessageBox.Show("non è possibile inviare la clipboard");
                                return;
                            }
                            //}
                        }
                    }
                    if (Clipboard.ContainsImage())
                    {
                        if (clipb != null)
                        {
                            if (server.GetConnected())
                            {
                                Image m = Clipboard.GetImage();
                                clipb.SetImmage(m);
                                clipb.valore = 2;
                                clipb.SetServer(server);
                                clipb.form1              = this;
                                clipbthread              = new Thread(new ThreadStart(clipb.sendData));
                                clipbthread.Name         = "clip thread";
                                clipbthread.IsBackground = true;
                                clipbthread.Start();
                                return;
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("non hai ancora effettuato la connessione con questo server");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                MessageBox.Show("Impossibile mandare attualmente la clipboard.");
            }
        }
        public void StartListening()
        {
            mouse = null;
            keybd = null;
            byte[] bytes = new Byte[1024];
            Console.WriteLine("sono bindato all'indirizzo " + resolve() + "porta " + port);
            form.DoSomething("sono in ascolto all'indirizzo " + resolve());
            ipAddress = IPAddress.Parse(resolve());
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);

            listener = new Socket(AddressFamily.InterNetwork,
                                  SocketType.Stream, ProtocolType.Tcp);

            try
            {
                mousethread    = null;
                keybdthread    = null;
                invioClipboard = null;
                avvioServInvio = null;

                listener.Bind(localEndPoint);
                listener.Listen(1);
                terminateGracefully = false;
                while (!terminateGracefully)
                {
                    Console.WriteLine("Waiting for a connection...");
                    Console.WriteLine("la password è " + password);
                    // Program is suspended while waiting for an incoming connection.
                    handler = listener.Accept();
                    SetKeepAlive(handler, 90, 100);
                    //  handler.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, true);
                    /***************************** CREAZIONE DEI THREAD. ****************************************/
                    terminatecurrentsocketPrinc = false;
                    StateObject state = new StateObject();
                    state.workSocket = handler;
                    if (mousethread == null)
                    {
                        mouse = new MouseUDP();
                        mouse.getServer(this);
                        // mouse.setevent)(autoMouse);
                        mousethread = new Thread(new ThreadStart(mouse.mouserun));
                        mousethread.IsBackground = true; // ricorda che il thread in background viene terminato immediatamente.
                        // al contrario di un thread in primo piano che deve attendere il completamento dell'operazione.
                        // mettendo il thread in background quando si chiude la finestra viene termianto tutto automaticamente.

                        mousethread.Name = "mouse thread";
                        mousethread.Start();
                    }
                    if (keybdthread == null)
                    {
                        keybd = new KeybdTCP();
                        keybd.SetServer(this);
                        keybdthread              = new Thread(new ThreadStart(keybd.keybdrun));
                        keybdthread.Name         = "keybd thread";
                        keybdthread.IsBackground = true;
                        keybdthread.Start();
                        flag = false;
                    }
                    if (avvioServInvio == null)
                    {
                        clp.SetServer(this);
                        clp.form1                   = form;
                        avvioServInvio              = new Thread(new ThreadStart(clp.setnewserver));
                        avvioServInvio.Name         = "Server per invio.";
                        avvioServInvio.IsBackground = true;
                        avvioServInvio.Start();
                    }
                    if (invioClipboard == null)
                    {
                        myclp = new MyClipBoard();
                        myclp.setServer(this);
                        myclp.setForm(form);
                        invioClipboard              = new Thread(new ThreadStart(myclp.receiveDataClipBoard));
                        invioClipboard.Name         = "server ricezione myclipboard";
                        invioClipboard.IsBackground = true;
                        invioClipboard.Start();
                    }
                    /**********************************************************/

                    connected = false;

                    /*  autoMouse.Reset();
                     * autoRecv.Reset();
                     * autoTast.Reset();*/
                    bool flagerrore = false;
                    while (!terminatecurrentsocketPrinc)
                    {
                        Console.WriteLine("sono connesso ");
                        // An incoming connection needs to be processed.
                        PrincreceiveDone.Reset();
                        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
                        PrincreceiveDone.WaitOne();
                    }
                    if (handler != null)
                    {
                        handler.Close();
                    }

                    form.DoSomething("Online in attesa di Host");

                    // chiudo i socket per far tornare bene le callback
                }
                if (listener != null)
                {
                    listener.Close(); listener = null;
                }
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                if (handler != null)
                {
                    handler.Close(); handler = null;
                }
                if (listener != null)
                {
                    listener.Close(); listener = null;
                }
                Console.WriteLine("errore nel socket uccido tutti i thread e ricomincio.");
                if (mousethread != null)
                {
                    mouse.closeSocket();
                }
                if (keybdthread != null)
                {
                    keybd.closeSocket();
                }
                if (avvioServInvio != null)
                {
                    clp.closeSocket();
                }
                if (invioClipboard != null)
                {
                    myclp.closeSocket();
                }
                mousethread    = null;
                keybdthread    = null;
                invioClipboard = null;
                avvioServInvio = null;
                if (form != null)
                {
                    form.DoSomething2("Offline");
                }
            }
        }