Search code examples
c#winformstcpclient

Send Mouse Coordinates Over Tcp Issues


I am working on program in which I want to move cursor of remote pc from my pc but here is a little problem receiving mouse coordinates on remote pc the data receive by tcp server on remote machine due to delay not pushing immediately on some places here is the code and output coordinates which i saved in a file for both client send and server receive.

Client Code to Send Coordinates.

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (isconnected)
    {
        try
        {
            NetworkStream serverStream = clientSocket.GetStream();
            this.Cursor = new Cursor(Cursor.Current.Handle);
            int pX = Cursor.Position.X;
            int pY = Cursor.Position.Y;
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes($"{pX}#{pY}");
            System.IO.File.AppendAllText(@"F:\DOWNLOAD\client.txt", $"{pX}#{pY}" + Environment.NewLine);
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
        }
        catch (Exception ex)
        {
        }
    }
}

Server Code to Receive Coordinates

while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
    try
    {
        // Translate data bytes to a ASCII string.
        // Receive mouse coordinates here
        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

        string Coordinates = data.ToString();
        string X = Coordinates.Substring(0, Coordinates.IndexOf('#'));
        string Y = Coordinates.Substring(Coordinates.IndexOf('#') + 1);
        coordvalue.Invoke((MethodInvoker)(() => coordvalue.Text = ""));

        coordvalue.Invoke((MethodInvoker)(() => coordvalue.Text = $"{X} and {Y}"));
        coordvalue.Invoke((MethodInvoker)(() => coordvalue.Update()));

        // Change coordinates
        System.Windows.Forms.Cursor.Position = new Point(Convert.ToInt32(X), Convert.ToInt32(Y));
        Cursor.Clip = new Rectangle(this.Location, this.Size);
        System.IO.File.AppendAllText(@"F:\DOWNLOAD\server.txt", $"{X} and {Y}" + Environment.NewLine);

    }
    catch (Exception E)
    {
        //  MessageBox.Show(E.ToString());
    }
}

Send Coordinates from client

653#492 659#490 669#489 677#486 684#483 693#476 699#470 709#460 715#453 720#444 724#437

Receive Coordinates on Server 653 and 492 659 and 490 669 and 489 677 and 486 684 and 483 693 and 476 699 and 470 709 and 460715#453720#444 724 and 437

Here problem is with709 and 460715#453720#444 because old coordinates were not pushed forward and appending with new which is not suitable for mouse position Please help.


Solution

  • I think for this situation using binary format is better.

    Like this:

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (isconnected)
        {
            try
            {
                NetworkStream serverStream = clientSocket.GetStream();
    
                var BW = new BinaryWriter(serverStream);
    
                this.Cursor = new Cursor(Cursor.Current.Handle);
                int pX = Cursor.Position.X;
                int pY = Cursor.Position.Y;
                BW.Write(pX);
                BW.Write(pY);
            }
            catch (Exception ex)
            {
    
    
            }
        }
    
    }
    
    private void RecieveLoop()
    {
        if (clientSocket.Available > 0)
        {
            NetworkStream serverStream = clientSocket.GetStream();
            var BR = new BinaryReader(serverStream);
    
            try
            {
                int X = BR.ReadInt32();
                int Y = BR.ReadInt32();
    
    
                coordvalue.Invoke((MethodInvoker)(() => coordvalue.Text = ""));
    
                coordvalue.Invoke((MethodInvoker)(() => coordvalue.Text = $"{X} and {Y}"));
                coordvalue.Invoke((MethodInvoker)(() => coordvalue.Update()));
                ////change coordinates
                System.Windows.Forms.Cursor.Position = new Point(Convert.ToInt32(X), Convert.ToInt32(Y));
                Cursor.Clip = new Rectangle(this.Location, this.Size);
                System.IO.File.AppendAllText(@"F:\DOWNLOAD\server.txt", $"{X} and {Y}" + Environment.NewLine);
    
            }
            catch (Exception E)
            {
    
                //  MessageBox.Show(E.ToString());
            }
        }
    }