C# - 远程控制电脑没有端口forwaring(像的TeamViewer)端口、远程控制、电脑、TeamViewer

2023-09-04 02:32:42 作者:说爱太烫嘴

修改 我想通了什么问题,但现在我面临着其他。我想从其他电脑,这部分作品遥控器1台PC,但你​​必须端口转发端口。是否可以连接如的TeamViewer?于是,我绕过防火墙而不必端口转发?如果是的话,怎么样?如果有人可以帮助我走出这将是pretty的真棒:)

EDIT I figured out what the problem was, but now I faced an other. I want to remote control 1 PC from an other PC, that part works, but you have to port forward the port. Is it possible to connect like TeamViewer? So I bypass the firewall and don't have to port forward? If yes, how? If someone could help me out it would be pretty awesome :)

布拉姆

原帖

我做了一个code,所以你可以从其他计算机控制计算机。唯一的问题是,它并没有电脑你自己的网络以外的工作(也许是我接错IP?我试过的家伙IPv4和IP从的 WhatIsMyIP )。我怎样才能使这项工作对我的外网?

I made a code so you can control a computer from an other computer. The only problem is that it doesn't work for computers outside of your own network (Maybe I connected the wrong IP? I tried the IPv4 of the guy and the IP from WhatIsMyIP ). How can I make this work for outside my network?

下面是服务器级的(我删除了大量的code,因为它没有任何意义)

Here is the server class(I removed a lot of code, because it didn't make sense)

    public partial class Form1 : Form
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

        private const int MOUSE_LEFTDOWN = 0x02;
        private const int MOUSE_LEFTUP = 0x04;
        private const int MOUSE_RIGTDOWN = 0x08;
        private const int MOUSE_RIGHTUP = 0x10;

        private TcpListener listener;
        private Socket mainSocket;
        private int port;
        private Stream s;
        private Thread eventWatcher;
        private int imageDelay;
        string connString;
        string connToString;
        string db;
        MySqlCommand command;
        MySqlCommand command2;
        MySqlCommand command3;
        MySqlCommand command4;

        MySqlConnection connection = null;

        public Form1()
        {
            InitializeComponent();
            port = 1338;
            imageDelay = 1000;
        }
        public Form1(int p)
        {
            port = p;
            imageDelay = 1000;
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show(getIP());
            startConnection();

            command = connection.CreateCommand();
            command2 = connection.CreateCommand();
            command3 = connection.CreateCommand();
            command4 = connection.CreateCommand();
            MessageBox.Show(connString);
            MessageBox.Show(db);

            this.Hide();
            port = 1338;
            while (true)
            {
                try
                {
                    startListening();
                }
                catch (Exception)
                {
                }
            }
        }

        public void startListening()
        {
            try
            {
                listener = new TcpListener(port);
                listener.Start();
                mainSocket = listener.AcceptSocket();
                s = new NetworkStream(mainSocket);
                eventWatcher = new Thread(new ThreadStart(waitForKeys));
                eventWatcher.Start();
                while (true)
                {
                    Bitmap screeny = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
                    Graphics theShot = Graphics.FromImage(screeny);
                    theShot.ScaleTransform(.25F, .25F);
                    theShot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
                    BinaryFormatter bFormat = new BinaryFormatter();
                    bFormat.Serialize(s, screeny);
                    Thread.Sleep(imageDelay);
                    theShot.Dispose();
                    screeny.Dispose();
                }
            }
            catch (Exception)
            {
                if (mainSocket.IsBound)
                    mainSocket.Close();
                if (listener != null)
                    listener.Stop();
            }
        }

        private static void trigger(IAsyncResult i) { }

和客户端code(同样,去掉了很多):

And the client code(Again, removed a lot):

    public class StateObject
    {
        public Socket workSocket = null;
        public const int BufferSize = 256;
        public byte[] buffer = new byte[BufferSize];
        public StringBuilder sb = new StringBuilder();
    }

    public partial class Form2 : Form
    {
        private static string response;

        private Stream stream;
        private StreamWriter eventSender;
        private Thread theThread;
        private TcpClient client;
        private Form1 mForm;
        private int resolutionX;
        private int resolutionY;
        //private int sendDelay = 250;
        //private Thread delayThread;
        public bool sendKeysAndMouse = false;

        private static ManualResetEvent sendDone = new ManualResetEvent(false);
        private static ManualResetEvent receiveDone = new ManualResetEvent(false);

        public Form2()
        {
            InitializeComponent();
        }
        public Form2(TcpClient s, Form1 callingForm)
        {
            client = s;
            mForm = callingForm;
            InitializeComponent();
            theThread = new Thread(new ThreadStart(startRead));
            theThread.Start();
        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (theThread.IsAlive)
                theThread.Abort();
            mForm.form2Closed();
        }
        private void startRead()
        {
            try
            {
                stream = client.GetStream();
                eventSender = new StreamWriter(stream);
                while (true)
                {
                    BinaryFormatter bFormat = new BinaryFormatter();
                    Bitmap inImage = bFormat.Deserialize(stream) as Bitmap;
                    resolutionX = inImage.Width;
                    resolutionY = inImage.Height;
                    theImage.Image = (Image)inImage;
                }
            }
            catch (Exception) { }
            /*try
            {
                Image theDesktop;
                stream = client.GetStream();
                theDesktop.Save(stream,new ImageFormat(
                while (true)
                {
                    while (stream.Read(buffer, 0, 1024) > 0)
                        theDesktop.Read(buffer, 0, 1024);
                    if(theDesktop != null) {
                        theDesktop.
                        theImage.Image = temp;
                }
            }
            catch (Exception gay) { }*/
        }

        private void Form2_ResizeEnd(object sender, EventArgs e)
        {

        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }


        private void Form2_MouseMove(object sender, MouseEventArgs e)
        {
        }


        private void theImage_MouseClick(object sender, MouseEventArgs e)
        {
            if (!sendKeysAndMouse)
                return;
            eventSender.Write("LCLICK\n");
            eventSender.Flush();
        }

        private void theImage_MouseDown(object sender, MouseEventArgs e)
        {
            if (!sendKeysAndMouse)
                return;
            eventSender.Write("LDOWN\n");
            eventSender.Flush();
        }

        private void theImage_MouseUp(object sender, MouseEventArgs e)
        {
            if (!sendKeysAndMouse)
                return;
            eventSender.Write("LUP\n");
            eventSender.Flush();
        }

        private void Receive(Socket client)
        {
            try
            {
                StateObject state = new StateObject();
                state.workSocket = client;

                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        private void Send(Socket client, String data)
        {
            byte[] byteData = Encoding.ASCII.GetBytes(data);

            client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);
        }

        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                StateObject state = (StateObject)ar.AsyncState;
                Socket client = state.workSocket;

                int bytesRead = client.EndReceive(ar);

                if (bytesRead > 0)
                {
                    state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
                }
                else
                {
                    if (state.sb.Length > 1)
                    {
                        response = state.sb.ToString();
                    }
                    receiveDone.Set();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        private void SendCallback(IAsyncResult ar)
        {
            try
            {
                Socket client = (Socket)ar.AsyncState;

                int bytesSent = client.EndSend(ar);
                Console.WriteLine("Sent {0} bytes to server.", bytesSent);

                sendDone.Set();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

我希望有人能帮助我,或者给我解释一下什么我做错了,我怎么能够使它发挥作用。

I hope someone can help me, or explain me what I'm doing wrong and how I can make it work.

推荐答案

的方式小组查看器,这是有一个在交互的第三个计算机。团队浏览器有,你和这个人既要连接到聊到互联网上的公共服务器和所有它的作用是充当桥梁将传入的消息从一端到另一端。现在,两端都出站连接,不需要端口转发。

The way Team Viewer does this is there is a 3rd computer in the interaction. Team Viewer has a public server on the internet that both you and the person you are connecting to talk to and all it does is act as a bridge forwarding incoming messages from one end to the other end. Now both ends have outbound connections and don't need port forwarding.

要与您的系统做同样的,你也需要设置,主机(支付),并保持你的程序的所有用户将谈话和行为,作为通信的桥梁互联网上的公共服务器。

To do the same with your system you would need to set up, host (pay for), and maintain a public server on the internet that all users of your program would talk to and act as the bridge for communications.

的步骤是这样的:

计算机1打开Server1上出站连接并保持打开状态,他们现在可以说话前后无端口porwarding。 计算机2打开Server1上出站连接并保持打开状态,他们现在可以说话前后无端口转发。 在计算机1将消息发送给服务器,上面写着转发如下,以电脑2 在Server1的使用步骤2的计算机2发起转发的邮件的打开连接。 在当前计算机2要回复发送一条消息给服务器,上面写着转发如下,以计算机1 在Server1上使用从步骤1中计算机1开始转发该消息的开放连接。