Exemplo n.º 1
0
        private async void button5_Click(object sender, RoutedEventArgs e)
        {
            List <PeerAddress> ips = await P2PConnectionManager.GetDNSSeedIPAddressesAsync(P2PNetworkParameters.DNSSeedHosts, _networkParameters);

            Thread threadLable = new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    Dispatcher.Invoke(() =>
                    {
                        label.Content = "Inbound: " + P2PConnectionManager.GetInboundP2PConnections().Count + " Outbound: " + P2PConnectionManager.GetOutboundP2PConnections().Count;
                    });
                    Thread.CurrentThread.Join(1000);
                }
            }));

            threadLable.IsBackground = true;
            threadLable.Start();

            foreach (PeerAddress ip in ips)
            {
                Thread connectThread = new Thread(new ThreadStart(() =>
                {
                    P2PConnection p2p = new P2PConnection(ip.IPAddress, _networkParameters, new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp));
                    p2p.ConnectToPeer(1);
                }));
                connectThread.IsBackground = true;
                connectThread.Start();
            }
        }
Exemplo n.º 2
0
        private void button_Click(object sender, RoutedEventArgs e)
        {
            Thread connectThread = new Thread(new ThreadStart(() =>
            {
                ushort port = 8333;

                if (_networkParameters.IsTestNet)
                {
                    port = 18333;
                }

                p2p          = new P2PConnection(IPAddress.Parse("82.45.214.119"), _networkParameters, new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp), port);
                bool success = p2p.ConnectToPeer(1);

                if (!success)
                {
                    MessageBox.Show("Not connected");
                }
            }));

            connectThread.IsBackground = true;
            connectThread.Start();

            Thread threadLable = new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    Dispatcher.Invoke(() =>
                    {
                        label.Content = "Inbound: " + P2PConnectionManager.GetInboundP2PConnections().Count + " Outbound: " + P2PConnectionManager.GetOutboundP2PConnections().Count;
                    });
                    Thread.CurrentThread.Join(250);
                }
            }));

            threadLable.IsBackground = true;
            threadLable.Start();
        }
        public static async Task <bool> ListenForIncomingP2PConnectionsAsync(IPAddress ipInterfaceToBind, P2PNetworkParameters netParams)
        {
            if (!_listening)
            {
                if (netParams.ListenForPeers)
                {
                    try
                    {
                        LingerOption lo = new LingerOption(false, 0);
                        _socket             = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        _socket.LingerState = lo;
                        _listening          = true;
                        _localEndPoint      = new IPEndPoint(ipInterfaceToBind, netParams.P2PListeningPort);
                        if (_socket.IsBound)
                        {
                            _socket.Close();
                        }
                        _socket.Bind(_localEndPoint);
                        _socket.Listen(1000);

                        if (netParams.UPnPMapPort)
                        {
                            //try upnp port forward mapping
                            await SetNATPortForwardingUPnPAsync(netParams.P2PListeningPort, netParams.P2PListeningPort);
                        }

                        _listenThread = new Thread(new ThreadStart(() =>
                        {
                            while (_listening)
                            {
                                try
                                {
                                    Socket newConnectedPeerSock = _socket.Accept();

                                    //if we haven't reached maximum specified to connect to us, allow the connection
                                    if (GetInboundP2PConnections().Count < P2PNetworkParameters.MaxIncomingP2PConnections)
                                    {
                                        //we've accepted a new peer create a new P2PConnection object to deal with them and we need to be sure to mark it as incoming so it gets stored appropriately
                                        P2PConnection p2pconnecting = new P2PConnection(((IPEndPoint)newConnectedPeerSock.RemoteEndPoint).Address, netParams, newConnectedPeerSock, Convert.ToUInt16(((IPEndPoint)newConnectedPeerSock.RemoteEndPoint).Port), true);
                                        p2pconnecting.ConnectToPeer(0);
                                    }
                                    else
                                    {
                                        newConnectedPeerSock.Close();
                                    }
                                }
                                catch (SocketException sex)
                                {
                                    //trap the exception "A blocking operation was interrupted by a call to WSACancelBlockingCall" thrown when we kill the listening socket but throw any others
                                    if (sex.ErrorCode != 10004)
                                    {
                                        //he said sex hehehehehehe
                                        throw sex;
                                    }
                                }
                            }
                        }));
                        _listenThread.IsBackground = true;
                        _listenThread.Start();
                    }
#if (!DEBUG)
                    catch
                    {
                        _listening = false;
                    }