LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

c# 实现端口转发可以用来实现远程桌面连接内网主机

admin
2018年2月10日 11:1 本文热度 7513
gYP.Start()方法调用后填写一台公网主机ip和端口(11111)然后公网主机用lcx转发11111到22222端口,远程桌面在公网主机连自己(127.0.0.1:22222)的22222端口就可以了可能需要点2次连接

 protected void wDZ(object sender, EventArgs e)
        {
            if (iXdh.Text == "" ││ eEpm.Text.Length < 7 ││ ZHS.Text == "") return;
            PortForward gYP = new PortForward();
            gYP.Start(llH.Text, int.Parse(ZHS.Text), eEpm.Text, int.Parse(iXdh.Text));         
        }
        protected void vJNsE(object sender, EventArgs e)
        {
            PortForward gYP = new PortForward();
            gYP.lyTOK();
          
        }
        protected void tYoZ(object sender, EventArgs e)
        {
        }
    }
  
    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];
            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)
        {
            try
            {
                session RKXy = new session();
                RKXy.ldel = ltcpClient;
                RKXy.rdel = rtcpClient;
                ltcpClient.BeginConnect(mtJ(Localaddress, LocalPort), new AsyncCallback(VTp), RKXy);
            }
            catch (Exception ex) { }
        }

        protected void VTp(IAsyncResult ar)
        {
            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) { }
        }
        private void LFYM(IAsyncResult ar)
        {
            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) { }
        }
        private void JTcp(IAsyncResult ar)
        {
            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) { }
        }
        private void xPS(IAsyncResult ar)
        {
            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) { }
        }
        private void IZU(IAsyncResult ar)
        {
            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) { }
        }
        public void lyTOK()
        {
            try
            {
                if (ltcpClient != null)
                {
                    ltcpClient.Close();
                }
                if (rtcpClient != null)
                    rtcpClient.Close();
            }
            catch (Exception ex) { }
        }
    }
}

该文章在 2018/2/10 11:02:23 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved