示例#1
0
        public void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                StateObject state = (StateObject)ar.AsyncState;

                // Read data from the remote device.
                int bytesRead = state.workSocket.EndReceive(ar);



                if (bytesRead == 0)
                {
                    if (sockClipBoard != null)
                    {
                        sockClipBoard.Close();
                    }
                    Console.WriteLine("il client ha chiuso la connessione");

                    server.terminatecurrentsocket = true;
                    server.receiveDone.Set();
                    return;
                }
                if (bytesRead > 0)
                {
                    if (server.GetConnected())
                    {
                        data = Encoding.ASCII.GetString(state.buffer, 0, bytesRead);
                        formClip.DoStatus(false);
                        switch (data)
                        {
                        case "F":

                            if (recvFile(receivedPath) < 0)
                            {
                                receivedPath = @"C:\local\";
                                if (Directory.Exists(receivedPath))
                                {
                                    Directory.Delete(receivedPath, true);
                                }
                                listFileName.Clear();
                                Directory.CreateDirectory(receivedPath);
                            }
                            break;

                        case "I":
                            Bitmap bmp = ReceiveVarData(sockClipBoard);
                            formClip.setImageToClip(bmp);
                            break;

                        case "T":
                            //formClip.DoStatus(false);
                            byteRead = sockClipBoard.Receive(clientData);
                            string text = Encoding.Default.GetString(clientData, 0, byteRead);
                            Console.WriteLine("!!!!!!!!!!!!!!!!!!" + text);
                            formClip.retriveClipBoard(text);
                            //formClip.DoStatus(true);
                            break;

                        case "DIR":
                            if (recvDirectory(receivedPath) < 0)
                            {
                                receivedPath = @"C:\local\";
                                listFileName.Clear();
                            }
                            break;

                        case "ENDDIR":
                            DirectoryInfo parentDir = Directory.GetParent(Path.GetDirectoryName(receivedPath));
                            string        parent    = parentDir.FullName;
                            receivedPath = parent + @"\";
                            sockClipBoard.Send(msg2);
                            break;

                        case "END":
                            Console.WriteLine("Ho ricevuto END");
                            formClip.addToClipBoard(listFileName);
                            receivedPath = @"C:\local\";
                            listFileName.Clear();
                            // formClip.DoStatus(true);
                            break;
                        }
                        formClip.DoStatus(true);
                    }
                }
            }

            catch (SocketException ex)
            {
                if (sockClipBoard != null)
                {
                    sockClipBoard.Close();
                }
                Console.WriteLine("sono il socket che riceve in eccezione ");
                server.terminatecurrentsocket = true;
                server.receiveDone.Set();
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine("sono il socket che riceve in eccezione: " + ex.ToString());
                server.terminatecurrentsocket = true;
                server.receiveDone.Set();
                return;
            }
            server.receiveDone.Set();
        }
示例#2
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.");
            }
        }
示例#3
0
        private void MessageReceiveCallback(IAsyncResult result)
        {
            EndPoint remote = new IPEndPoint(0, 0);
            string   pos    = "";

            // autoEvent.WaitOne();
            try
            {
                if (server.GetConnected())
                {
                    // get received message.
                    pos = Encoding.UTF8.GetString(recBuffer);

                    // clicked?
                    if (pos.StartsWith("click"))
                    {
                        this.LeftClick();

                        // long click = double click
                    }
                    else if (pos.StartsWith("d.click"))
                    {
                        this.LeftClick();
                        this.LeftClick();
                    }
                    else if (pos.StartsWith("r.click"))
                    {
                        this.RightClick();
                    }
                    else if (pos.StartsWith("trascina"))
                    {
                        this.trascina();
                    }
                    else if (pos.StartsWith("SU"))
                    {
                        this.SU(Convert.ToInt32(pos.Substring(2, 2)) + 30);
                        //  Console.WriteLine(Convert.ToInt32(pos.Substring(2, 2)));
                    }
                    else if (pos.StartsWith("GIU"))
                    {
                        this.GIU(Convert.ToInt32(pos.Substring(3, 3)) - 30);
                        //Console.WriteLine(Convert.ToInt32(pos.Substring(3, 3)));
                    }
                    else
                    {
                        string x = pos.Substring(0, pos.IndexOf(","));
                        // calculate delta
                        int deltaX;
                        int deltaY;
                        int.TryParse(x, out deltaX);
                        int.TryParse(pos.Substring(pos.IndexOf(",") + 1,
                                                   pos.IndexOf("\0") + -pos.IndexOf(",")), out deltaY);
                        // Console.WriteLine(deltaY+"//////////////////");
                        int scaleY;
                        int.TryParse(server.coordinateCLient.Substring(server.coordinateCLient.IndexOf(",") + 1, (server.coordinateCLient.IndexOf("\0") - server.coordinateCLient.IndexOf(","))), out scaleY);
                        int scaleX;
                        int.TryParse(server.coordinateCLient.Substring(0, server.coordinateCLient.IndexOf(",")), out scaleX);
                        int coordX = (Screen.PrimaryScreen.Bounds.Width * deltaX) / scaleX;
                        int coordY = (Screen.PrimaryScreen.Bounds.Height * deltaY) / scaleY;
                        //  Console.WriteLine("nuove coordinate" + coordX + " " + coordY + " " + (Screen.PrimaryScreen.Bounds.Height * deltaY) + " " + scaleY);
                        // set new point
                        System.Drawing.Point pt = System.Windows.Forms.Cursor.Position;
                        System.Windows.Forms.Cursor.Position = new System.Drawing.Point(coordX, coordY);
                    }
                }
                // End and "begin" for next package
                this.receiveSocket.EndReceiveFrom(result, ref remote);
            }
            catch (SocketException e)
            {
                Console.Write("i close the socket");
                // chiudo il socket per la ricezione.
                if (receiveSocket != null)
                {
                    receiveSocket.Close(); receiveSocket = null;
                }

                server.terminatecurrentsocketMouse = true;
            }
            catch (Exception)
            {
                Console.Write(pos);
                // chiudo il socket per la ricezione.
                if (receiveSocket != null)
                {
                    receiveSocket.Close(); receiveSocket = null;
                }
                server.terminatecurrentsocketMouse = true;
            }
            server.MouseUDPreceiveDone.Set();
        }