LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

C#實現端口NAT轉發可以用來實現遠程桌面連接內網主機

admin
2021年6月10日 10:28 本文熱度 4099
gYP.Start() 方法調用后填寫一臺公網主機IP(192.168.0.225)和端口(51),然后公網主機用lcx轉發51到510端口,遠程桌面在公網主機連自己(127.0.0.1:510)的510端口,就可以了。

源碼附件:testPortTransfer.rar 附件:lcx.rar


主窗口: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 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲最近精品视频 | 亚洲欧美日韩中文无线码 | 在线不卡国产午夜电影 | 在线午夜精品自拍小视频 | 亚洲日本va中文字幕久久 | 亚洲欧美精品在线精品二区 |