C#實現(xiàn)端口轉(zhuǎn)發(fā)可以用來實現(xiàn)遠程桌面連接內(nèi)網(wǎng)主機
當前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
核心代碼:gYP.Start()方法。 填寫一臺公網(wǎng)主機ip和端口(11111),然后公網(wǎng)主機用lcx轉(zhuǎn)發(fā)11111到22222端口,在公網(wǎng)主機上打開遠程桌面程序,連本機(127.0.0.1:22222)的22222端口就可以了,可能需要點2次連接。 using System; using System.Net; using System.Net.Sockets; using System.Windows.Forms; namespace PortTransfer { public partial class Form_Port_Transfer : Form { public Form_Port_Transfer() { InitializeComponent(); } private void btn_Start_Click(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)); } private void btn_Stop_Click(object sender, EventArgs e) { PortForward gYP = new PortForward(); gYP.lyTOK(); } } 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) { } } } } 該文章在 2021/2/3 14:41:47 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |