C#-远程控制没有WARING端口的PC(如TeamViewer)

C#-远程控制没有WARING端口的PC(如TeamViewer),c#,.net,sockets,tcp,C#,.net,Sockets,Tcp,编辑 我知道问题出在哪里,但现在我面临另一个问题。我想从另一台电脑远程控制1台电脑,这部分工作正常,但您必须将端口向前移动。是否可以像TeamViewer那样连接?所以我绕过防火墙,不必向前移植?如果是,如何进行?如果有人能帮我,那就太棒了:) 布拉姆 原创帖子 我做了一个代码,这样你可以从另一台计算机控制一台计算机。唯一的问题是,它不适用于您自己网络之外的计算机(可能我连接了错误的IP?我尝试了家伙的IPv4和来自的IP)。我如何使此功能在我的网络之外工作 这是服务器类(我删除了很多代码,

编辑
我知道问题出在哪里,但现在我面临另一个问题。我想从另一台电脑远程控制1台电脑,这部分工作正常,但您必须将端口向前移动。是否可以像TeamViewer那样连接?所以我绕过防火墙,不必向前移植?如果是,如何进行?如果有人能帮我,那就太棒了:)

  • 布拉姆
原创帖子

我做了一个代码,这样你可以从另一台计算机控制一台计算机。唯一的问题是,它不适用于您自己网络之外的计算机(可能我连接了错误的IP?我尝试了家伙的IPv4和来自的IP)。我如何使此功能在我的网络之外工作

这是服务器类(我删除了很多代码,因为它没有意义)

和客户端代码(同样,删除了很多):


我希望有人能帮助我,或者向我解释我做错了什么,以及如何让它工作。

Team Viewer这样做的方式是在交互中有第三台计算机。Team Viewer在internet上有一个公共服务器,您和您要与之通话的人都可以使用该服务器,它所做的只是充当将传入消息从一端转发到另一端的桥梁。现在两端都有出站连接,不需要端口转发

要对您的系统执行同样的操作,您需要在internet上设置、托管(付费)和维护一个公共服务器,您的程序的所有用户都可以与之交谈,并充当通信的桥梁

步骤如下:

  • Computer1打开到Server1的出站连接并保持打开状态,他们现在可以在不需要端口切换的情况下来回通话
  • Computer2打开到Server1的出站连接并保持打开状态,他们现在可以在不进行端口转发的情况下来回通话
  • Computer1向Server1发送一条消息,上面写着“将以下内容转发给Computer2”
  • Server1使用Computer2启动的步骤2中的开放连接转发消息
  • 当Computer2想要回复时,它会向Server1发送一条消息,上面写着“将以下内容转发给Computer1”
  • Server1使用Computer1启动的步骤1中的打开连接转发消息

  • Team Viewer执行此操作的方式是在交互中有第三台计算机。Team Viewer在internet上有一个公共服务器,您和您要与之通话的人都可以使用该服务器,它所做的只是充当将传入消息从一端转发到另一端的桥梁。现在两端都有出站连接,不需要端口转发

    要对您的系统执行同样的操作,您需要在internet上设置、托管(付费)和维护一个公共服务器,您的程序的所有用户都可以与之交谈,并充当通信的桥梁

    步骤如下:

  • Computer1打开到Server1的出站连接并保持打开状态,他们现在可以在不需要端口切换的情况下来回通话
  • Computer2打开到Server1的出站连接并保持打开状态,他们现在可以在不进行端口转发的情况下来回通话
  • Computer1向Server1发送一条消息,上面写着“将以下内容转发给Computer2”
  • Server1使用Computer2启动的步骤2中的开放连接转发消息
  • 当Computer2想要回复时,它会向Server1发送一条消息,上面写着“将以下内容转发给Computer1”
  • Server1使用Computer1启动的步骤1中的打开连接转发消息


  • 我怀疑您遇到的是网络连接问题,而不是代码问题。您和主机PC之间可能有防火墙,并且/或者它没有您可以路由到的IP地址。您可能需要转发TCP端口或允许它通过防火墙。像UltraVNC这样的应用程序经常会遇到这样的问题。那么,我需要连接什么IP呢?来自WhatIsMyIP.com的IP?还是IPv4?请尽量缩小您的问题范围,不要让我们阅读所有这些代码。大部分代码与您的问题无关。可能是WhatIsMyIP.com IP(也可能是IPv4)。其他计算机的本地地址并不重要,只是他需要在路由器上设置端口转发。此时,如果在服务器故障中找不到现有的问题,您可能需要询问此问题(SAN代码)。我怀疑您遇到的是网络连接问题,而不是代码问题。您和主机PC之间可能有防火墙,并且/或者它没有您可以路由到的IP地址。您可能需要转发TCP端口或允许它通过防火墙。像UltraVNC这样的应用程序经常会遇到这样的问题。那么,我需要连接什么IP呢?来自WhatIsMyIP.com的IP?还是IPv4?请尽量缩小您的问题范围,不要让我们阅读所有这些代码。大部分代码与您的问题无关。可能是WhatIsMyIP.com IP(也可能是IPv4)。其他计算机的本地地址并不重要,只是他需要在路由器上设置端口转发。在这一点上,如果你找不到现有的服务器,你可能想问这个关于服务器故障的问题(sans代码)。因此,如果我说的对,你会向一个Web服务器发送一些东西,Web服务器会向另一台pc发送信号,反之亦然?谢谢。我想我现在有点明白了。但是,Server1如何连接到Computer2并向其发送消息?Computer2在程序启动时启动了与Server1的连接,Computer1随后保持连接打开(在断开连接时重新连接),监听新消息。Server1使用打开的连接发送消息。谢谢,我现在明白了:)我之前评论中的Computer1应该是Computer2所以,如果我说的对,你会向Web服务器发送一些东西,Web服务器会向另一台pc发送信号,反之亦然
        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) { }
    
        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());
                }
            }
        }
    }