| C#实现端口NAT转发可以用来实现远程桌面连接内网主机
					当前位置:点晴教程→知识管理交流
					
					→『 技术文档交流 』
					
				 
 gYP.Start() 方法调用后填写一台公网主机IP(192.168.0.225)和端口(51),然后公网主机用lcx转发51到510端口,远程桌面在公网主机连自己(127.0.0.1:510)的510端口,就可以了。 主窗口:FrmPortTransfer.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace testPortTransfer {     public partial class FrmPortTransfer : Form     {         public string exePath = System.Windows.Forms.Application.StartupPath;  //本程序所在路径         public int closeLcx = 0;         public FrmPortTransfer()         {             InitializeComponent();         }         protected void tYoZ(object sender, EventArgs e)         {         }         private void btnStart_Click(object sender, EventArgs e)         {             btnStart.Enabled = false;             btnStart.Text = "执行中...";             btnStop.Enabled = true;             if (llH.Text.Length < 7 || ZHS.Text == "" || eEpm.Text.Length < 7 || iXdh.Text == "") return;             Public.RemoteIP = llH.Text;                               //远程IP             Public.RemotePort = ZHS.Text;                             //远程端口             Public.LocalIP = eEpm.Text;                               //本地IP             Public.LocalPort = iXdh.Text;                             //本地端口             Public.StopServiceFlag = 1;                               //执行状态,0-停止、1-执行中             PortForward gYP = new PortForward();             gYP.Start(Public.RemoteIP, int.Parse(Public.RemotePort), Public.LocalIP, int.Parse(Public.LocalPort));         }         private void btnStop_Click(object sender, EventArgs e)         {             btnStart.Enabled = true;             btnStart.Text = "启动";             btnStop.Enabled = false;             Public.StopServiceFlag = 0;             PortForward gYP = new PortForward();             gYP.lyTOK();         }         private void btnServerStart_Click(object sender, EventArgs e)         {             btnServerStart.Enabled = false;             btnServerStart.Text = "执行中...";             btnServerStop.Enabled = true;             closeLcx = 1;             btnServerStop_Click(sender, e);             Thread.Sleep(1000);             string FilePath = (exePath + @"\lcx.exe").Replace(@"\\", @"\");             if (File.Exists(FilePath))             {                 Process.Start(FilePath, " -listen " + txtServerInPort.Text + " " + txtServerOutPort.Text);             }             else             {                 MessageBox.Show("启动应用程序失败:“" + FilePath + "”不存在!", "系统提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);             }         }         private void btnServerStop_Click(object sender, EventArgs e)         {             if (closeLcx == 0)             {                 DialogResult result = MessageBox.Show("您确定要关闭LCX服务吗?", "确认关闭", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);                 if (result != DialogResult.OK){ return; }                 btnServerStart.Enabled = true;                 btnServerStart.Text = "启动";                 btnServerStop.Enabled = false;             }             closeLcx = 0;             string FilePath = (exePath + @"\lcx.exe").Replace(@"\\", @"\");             if (File.Exists(FilePath))             {                 //停止后台服务程序                 try                 {                     string CSFilePath = FilePath.ToLower();                     if (File.Exists(CSFilePath))                     {                         Process[] myProcesses = Process.GetProcessesByName("lcx");                         foreach (Process myProcess in myProcesses)                         {                             if (CSFilePath == myProcess.MainModule.FileName.ToLower())                             {                                 Process p = Process.GetProcessById(myProcess.Id);                                 p.Kill();                                 break;                             }                         }                     }                 }                 catch (Exception) { }             }         }     }     public class PortForward     {         public string Localaddress;         public int LocalPort;         public string RemoteAddress;         public int RemotePort;         string type;         Socket ltcpClient;         Socket rtcpClient;         Socket server;         byte[] DPrPL = new byte[2048];         byte[] wvZv = new byte[2048];         public struct session         {             public Socket rdel;             public Socket ldel;             public int llen;             public int rlen;         }         public static IPEndPoint mtJ(string host, int port)         {             IPEndPoint iep = null;             //IPHostEntry aGN = Dns.Resolve(host);             //IPAddress rmt = aGN.AddressList[0];             IPAddress rmt = IPAddress.Parse(host);             iep = new IPEndPoint(rmt, port);             return iep;         }         public void Start(string Rip, int Rport, string lip, int lport)         {             try             {                 LocalPort = lport;                 RemoteAddress = Rip;                 RemotePort = Rport;                 Localaddress = lip;                 rtcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);                 ltcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);                 rtcpClient.BeginConnect(mtJ(RemoteAddress, RemotePort), new AsyncCallback(iiGFO), rtcpClient);             }             catch (Exception ex) { }         }         protected void iiGFO(IAsyncResult ar)         {             if (Public.StopServiceFlag == 0) { return; }             try             {                 session RKXy = new session();                 RKXy.ldel = ltcpClient;                 RKXy.rdel = rtcpClient;                 ltcpClient.BeginConnect(mtJ(Localaddress, LocalPort), new AsyncCallback(VTp), RKXy);             }             catch (Exception ex)             {                 PortForward gYP = new PortForward();                 gYP.Start(Public.RemoteIP, int.Parse(Public.RemotePort), Public.LocalIP, int.Parse(Public.LocalPort));             }         }         protected void VTp(IAsyncResult ar)         {             if (Public.StopServiceFlag == 0) { return; }             try             {                 session RKXy = (session)ar.AsyncState;                 ltcpClient.EndConnect(ar);                 RKXy.rdel.BeginReceive(DPrPL, 0, DPrPL.Length, SocketFlags.None, new AsyncCallback(LFYM), RKXy);                 RKXy.ldel.BeginReceive(wvZv, 0, wvZv.Length, SocketFlags.None, new AsyncCallback(xPS), RKXy);             }             catch (Exception ex)             {                 PortForward gYP = new PortForward();                 gYP.Start(Public.RemoteIP, int.Parse(Public.RemotePort), Public.LocalIP, int.Parse(Public.LocalPort));             }         }         private void LFYM(IAsyncResult ar)         {             if (Public.StopServiceFlag == 0) { return; }             try             {                 session RKXy = (session)ar.AsyncState;                 int Ret = RKXy.rdel.EndReceive(ar);                 if (Ret > 0)                     ltcpClient.BeginSend(DPrPL, 0, Ret, SocketFlags.None, new AsyncCallback(JTcp), RKXy);                 else lyTOK();             }             catch (Exception ex)             {                 PortForward gYP = new PortForward();                 gYP.Start(Public.RemoteIP, int.Parse(Public.RemotePort), Public.LocalIP, int.Parse(Public.LocalPort));             }         }         private void JTcp(IAsyncResult ar)         {             if (Public.StopServiceFlag == 0) { return; }             try             {                 session RKXy = (session)ar.AsyncState;                 RKXy.ldel.EndSend(ar);                 RKXy.rdel.BeginReceive(DPrPL, 0, DPrPL.Length, SocketFlags.None, new AsyncCallback(this.LFYM), RKXy);             }             catch (Exception ex)             {                 PortForward gYP = new PortForward();                 gYP.Start(Public.RemoteIP, int.Parse(Public.RemotePort), Public.LocalIP, int.Parse(Public.LocalPort));             }         }         private void xPS(IAsyncResult ar)         {             if (Public.StopServiceFlag == 0){ return; }             try             {                 session RKXy = (session)ar.AsyncState;                 int Ret = RKXy.ldel.EndReceive(ar);                 if (Ret > 0)                     RKXy.rdel.BeginSend(wvZv, 0, Ret, SocketFlags.None, new AsyncCallback(IZU), RKXy);                 else lyTOK();             }             catch (Exception ex)             {                 PortForward gYP = new PortForward();                 gYP.Start(Public.RemoteIP, int.Parse(Public.RemotePort), Public.LocalIP, int.Parse(Public.LocalPort));             }         }         private void IZU(IAsyncResult ar)         {             if (Public.StopServiceFlag == 0) { return; }             try             {                 session RKXy = (session)ar.AsyncState;                 RKXy.rdel.EndSend(ar);                 RKXy.ldel.BeginReceive(wvZv, 0, wvZv.Length, SocketFlags.None, new AsyncCallback(this.xPS), RKXy);             }             catch (Exception ex)             {                 PortForward gYP = new PortForward();                 gYP.Start(Public.RemoteIP, int.Parse(Public.RemotePort), Public.LocalIP, int.Parse(Public.LocalPort));             }         }         public void lyTOK()         {             try             {                 if (ltcpClient != null)                 {                     ltcpClient.Close();                 }                 if (rtcpClient != null)                     rtcpClient.Close();             }             catch (Exception ex) { }         }     } } 类:Public.cs using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace testPortTransfer {     class Public     {         public static string RemoteIP = "";                              //远程IP         public static string RemotePort = "";                            //远程端口         public static string LocalIP = "";                               //本地IP         public static string LocalPort = "";                             //本地端口         public static int StopServiceFlag = 0;                           //执行状态,0-停止、1-执行中     } } 该文章在 2021/6/10 10:37:35 编辑过 | 关键字查询 相关文章 正在查询... |