P2p tcp nat schematic + Source Code (original), p2pnat

Source: Internet
Author: User

P2p tcp nat schematic + Source Code (original), p2pnat

Currently, p2p udp nat penetration articles on the Internet are numerous, and there are few p2p tcp nat articles. After several days of study, I have finally learned something about it. I would like to share it with you. Thank you for your support!

 

1. First, you must have an internet server or a telecom carrier to support the forwarding of the router (the specific process is not detailed, please Baidu)

2. A computer that can access the Internet (the computer in the Intranet is meaningless)

 

The core code is:

socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

The function is to enable the connected port to listen again to achieve the purpose of tcp nat

Schematic:

Internet server code (ps: up the Master cannot afford to buy a server and can only use the company's router for forwarding, please forgive me !) :

Using System; using System. collections. generic; using System. linq; using System. net; using System. net. sockets; using System. text; using System. threading; namespace TcpNatTestS {class Program {public static IPAddress GetLocalIP () {try {string HostName = Dns. getHostName (); // obtain the host name IPHostEntry IpEntry = Dns. getHostEntry (HostName); for (int I = 0; I <IpEntry. addressList. length; I ++) {// filter IPv4 IP addresses from the IP address list // AddressFamily. interNetwork indicates that the IP address is IPv4, // AddressFamily. interNetworkV6 indicates that this address is of the IPv6 Type if (IpEntry. addressList [I]. addressFamily = AddressFamily. interNetwork) {return IpEntry. addressList [I] ;}} return IPAddress. any;} catch (Exception ex) {return IPAddress. any ;}} static void Main (string [] args) {TcpListener tcp = new TcpListener (new IPEndPoint (GetLocalIP (), 8085); tcp. start (); new Thread (e => {while (true) {var lianjie = tcp. acceptTcpClient (); // new TcpClient (). connect (IPEndPoint) lianjie. client. remoteEndPoint); lianjie. client. send (UnicodeEncoding. unicode. getBytes ("your Internet ip address is:" + lianjie. client. remoteEndPoint); Console. writeLine ("intranet server: local port:" + lianjie. client. localEndPoint + "remote port:" + lianjie. client. remoteEndPoint );}}). start (); Console. readKey ();}}}

 

Intranet client (you need to enter the ip address of the internet server ):

 

Using System; using System. collections. generic; using System. linq; using System. net; using System. net. sockets; using System. text; using System. threading; namespace TcpNatTestA {class Program {public static IPAddress GetLocalIP () {try {string HostName = Dns. getHostName (); // obtain the host name IPHostEntry IpEntry = Dns. getHostEntry (HostName); for (int I = 0; I <IpEntry. addressList. length; I ++) {// filter IPv4 IP addresses from the IP address list // AddressFamily. interNetwork indicates that the IP address is IPv4, // AddressFamily. interNetworkV6 indicates that this address is of the IPv6 Type if (IpEntry. addressList [I]. addressFamily = AddressFamily. interNetwork) {return IpEntry. addressList [I] ;}} return IPAddress. any;} catch (Exception ex) {return IPAddress. any ;}} static void Main (string [] args) {new Thread (e =>{ TcpClient clinet = new TcpClient (); clinet. client. setSocketOption (SocketOptionLevel. socket, SocketOptionName. reuseAddress, true); var bendi = new IPEndPoint (GetLocalIP (), 7896); clinet. client. bind (bendi); clinet. connect (new IPEndPoint (IPAddress. parse ("Enter your server ip Address"), 8085); TcpListener tcp = new TcpListener (bendi); tcp. server. setSocketOption (SocketOptionLevel. socket, SocketOptionName. reuseAddress, true); tcp. start (); new Thread () => {while (true) {var jiqi = tcp. acceptTcpClient (); Console. writeLine ("intranet client: Local endpoint:" + bendi + "remote endpoint:" + jiqi. client. remoteEndPoint );}}). start (); clinet. client. send (UnicodeEncoding. unicode. getBytes ("Haha"); byte [] hh = new byte [500]; var weishu = clinet. client. receive (hh); byte [] temp = new byte [weishu]; Array. copy (hh, temp, weishu); // Console. writeLine ("remote port:" + clinet. client. remoteEndPoint); Console. writeLine (UnicodeEncoding. unicode. getString (temp ));}). start (); Console. readKey ();}}}

 

 

If you have any questions, contact me.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.