示例#1
0
        public TCPHolePunching(ChatterService from,ChatterService to,string guid)
        {
            this.from = from;
            this.to = to;
            this.guid = guid;
            Socket tcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPAddress[] arrIPAddresses = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress ip in arrIPAddresses)
            {
                if (ip.AddressFamily.Equals(AddressFamily.InterNetwork))
                {
                    ipa = ip;
                    break;
                }
            }

            tcpServer.Bind(new IPEndPoint(ipa,0));
            serverEndPoint= tcpServer.LocalEndPoint as IPEndPoint;

            tcpServer.Listen(1);
            new Thread(() =>
            {

                bool isFromBack = false;
                bool isToBack = false;
                EndPoint fromRemoteEP = null;
                EndPoint toRemoteEP = null;
                while (true)
                {
                    var client = tcpServer.Accept();

                    byte[] buffer = new byte[1];
                    client.Receive(buffer);
                    var remoteEP = client.RemoteEndPoint as IPEndPoint;
                    string s=Encoding.UTF8.GetString(buffer);
                    if (s == "1")
                    {
                        if (isFromBack)
                            continue;
                        isFromBack = true;
                        fromRemoteEP = remoteEP;
                       
                    }
                    else if (s == "2")
                    {
                        if (isToBack)
                            continue;
                        isToBack = true;
                       
                        toRemoteEP = remoteEP;
                       
                    }

                    if (isFromBack && isToBack)
                    {
                       

                        this.to.callback.SendTCPEndPoint(new MyEndPoint()
                        {
                            Address = (fromRemoteEP as IPEndPoint).Address.ToString(),
                            Port = (fromRemoteEP as IPEndPoint).Port
                        }, guid);

                        this.from.callback.SendTCPEndPoint(new MyEndPoint()
                        {
                            Address = (toRemoteEP as IPEndPoint).Address.ToString(),
                            Port = (toRemoteEP as IPEndPoint).Port
                        }, guid);

                        MyLogger.Logger.Info("from:" + fromRemoteEP.ToString());
                        MyLogger.Logger.Info("to:" + toRemoteEP.ToString());

                        break;
                    }
                    
                }
                Console.WriteLine("tcp打洞");

            }) { IsBackground=true}.Start();


            
        }
示例#2
0
        public UDPHolePunching(ChatterService from,ChatterService to)
        {
            // TODO: Complete member initialization
            this.from = from;
            this.to = to;



            Socket udpserver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPAddress[] arrIPAddresses = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress ip in arrIPAddresses)
            {
                if (ip.AddressFamily.Equals(AddressFamily.InterNetwork))
                {
                    ipa = ip;
                    break;
                }
            }
            udpserver.Bind(new IPEndPoint(ipa, 0));
            serverEndPoint = udpserver.LocalEndPoint as IPEndPoint;


            EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
            new Thread(() =>
            {
                try
                {
                    byte[] buffer = new byte[1];
                    bool isFromBack = false;
                    bool isToBack = false;

                    EndPoint fromRemoteEP = null;
                    EndPoint toRemoteEP = null;
                    while (true)
                    {
                        udpserver.ReceiveFrom(buffer, ref remoteEP);
                        string s = Encoding.UTF8.GetString(buffer);

                        if (s == "1")
                        {
                            if (isFromBack)
                                continue;

                            isFromBack = true;
                            fromRemoteEP = remoteEP;
                        }
                        else if (s == "2")
                        {
                            if (isToBack)
                                continue;
                            isToBack = true;
                            toRemoteEP = remoteEP;
                        }

                        if (isFromBack && isToBack)
                        {
                            this.from.callback.SendUDPEndPoint(new MyEndPoint()
                            {
                                Address = (toRemoteEP as IPEndPoint).Address.ToString(),
                                Port = (toRemoteEP as IPEndPoint).Port
                            }, to.member, true);

                            this.to.callback.SendUDPEndPoint(new MyEndPoint()
                            {
                                Address = (fromRemoteEP as IPEndPoint).Address.ToString(),
                                Port = (fromRemoteEP as IPEndPoint).Port
                            }, from.member, false);
                            break;
                        }
                    }
                    Console.WriteLine("udp打洞");
                }
                catch (Exception ex)
                {
                    MyLogger.Logger.Error("接收udp包出错", ex);
                }
            }) { IsBackground=true}.Start();



        }