TCP打孔不起作用

时间:2016-04-05 11:23:35

标签: c# hole-punching

我要做一个同行2同行应用程序,我是编程的新手,我读了一些关于打孔的文章,现在我有NAT和打孔技术的基本概念。但实际上我无法成功。 我将使用打孔技术,并且我已经在C#中进行了这个简单的测试应用程序,它向PHP服务器发送请求以找到公共IP和PORT,在实现IP和端口之后它向此实现的IP发送请求(循环回来),但它不起作用。 我不知道为什么它不起作用,我在哪里做错了? 这是我的代码。

class Program
{
    static EndPoint myPublicAddress;
    Socket stunClient;
    IPEndPoint stunServer = new IPEndPoint(Dns.GetHostAddresses("ultrarate.tech")[0], 80);
    Socket listener;
    EndPoint localEndpoint;
    String stunRequestPacket = "GET http://ultrarate.tech/test/stun.php HTTP/1.1\nUser-Agent: Mozilla/4.0\nHost: 10.154.90.59:4030\nConnection: close\n\n";

    byte[] buffer = new byte[1024];

    static void Main(string[] args)
    {
        Program p = new Program();
        p.go();
        Console.ReadKey();
    }
    public void go() {
        stunClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        localEndpoint = new IPEndPoint(IPAddress.Parse("10.154.90.59"),4030);
        try
        {
            stunClient.Bind(localEndpoint);
            stunClient.Connect(stunServer);
            Console.WriteLine("MainThread:__ Your Private IP and Port:-  " + stunClient.LocalEndPoint.ToString() + "\n");
            Console.WriteLine("MainThread:__ Requesting your public IP and Port....\n\n\n");
            stunClient.Send(Encoding.ASCII.GetBytes(stunRequestPacket));
            int x = stunClient.Receive(buffer);
            string response = Encoding.ASCII.GetString(buffer, 0, x);

            string ip = response.Substring(response.IndexOf("<ip>") + 4,
                                           response.IndexOf("</ip>") - response.IndexOf("<ip>") - 4);

            int port = Convert.ToInt32(response.Substring(
                                                            response.IndexOf("<port>") + 6,
                                                            response.IndexOf("</port>") - response.IndexOf("<port>") - 6
                                                          )
                                       );

            Console.WriteLine("MainThread:__ Your Public IP and Port is:-   " + ip + ":" + port + "\n\n");
            myPublicAddress = new IPEndPoint(IPAddress.Parse(ip), port);
            stunClient.Disconnect(false);
            stunClient.Close();
            stunClient.Dispose();
            stunClient = null;
        }
        catch (Exception ex) { Console.WriteLine(ex.Message); }
        listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        listener.Bind(localEndpoint);
        listener.Listen(10);
        Console.WriteLine("MainThread:__ Listening On\t" + localEndpoint.ToString() + "\n\n\n");
        ThreadStart ts = new ThreadStart(makeRequest);
        Thread t = new Thread(ts);
        t.Start();
        Socket ns = listener.Accept();
        Console.WriteLine("MainThread:__ Connection Recieved:"+ns.RemoteEndPoint.ToString());
        int count = ns.Receive(buffer);
        Console.WriteLine("MainThread:__ Message Recieved:" + Encoding.ASCII.GetString(buffer, 0, count));

        listener.Close();
        listener.Dispose();
    }
    static void makeRequest()
    {
        try
        {
            Socket newSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Console.WriteLine("\tThread 2:__ Loopback Request to Public Addr (" + myPublicAddress.ToString() + ")");
            newSock.Connect(myPublicAddress);
            newSock.Send(Encoding.ASCII.GetBytes("hello i am from outSide"));
            newSock.Close();
        }
        catch(Exception ex) {
            Console.WriteLine("\t\nThread 2:__ "+ex.Message);
        }
    }
}

这是我的PHP文件。

<?php 
     echo "<result>";
     echo "<ip>".$_SERVER['REMOTE_ADDR']."</ip>";
     echo "<port>".$_SERVER['REMOTE_PORT']."</port>";
     echo "</result>";
?>

当我运行应用程序时,输出就像这个

Application Output ScreenShot

0 个答案:

没有答案