<p>Simply:</p>

<pre><code>peer.close();
</code></pre>

<p><a href="https://github.com/bitcoinj/bitcoinj/blob/v0.12.2/core/src/main/java/org/bitcoinj/core/Peer.java" rel="nofollow"><code>Peer.java</code></a> extends <a href="https://github.com/bitcoinj/bitcoinj/blob/v0.12.2/core/src/main/java/org/bitcoinj/core/PeerSocketHandler.java" rel="nofollow"><code>PeerSocketHandler.java</code></a>, which is the class that defines the <code>close()</code> method, so maybe that's why you weren't seeing it. It would also probably be easier to find if it were called <code>disconnect()</code>.</p>

CC BY-SA 3.0
2015-02-03T08:01:27.320Z
I have called peer.close() and it triggers onDisconnected() event but and in this event I am adding DNS to the peerGroup but despite of adding DNS it is still looking for peer. I want to completely switch from peer to DNS
Ankit Sharma
CC BY-SA 3.0
2015-02-03T15:47:21.583Z
@AnkitSharma, What do you mean by adding DNS? Do you care which peers you are connected to? And why can you not just add it from the start, why do you have to do it after a peer is disconnected?
morsecoder
CC BY-SA 3.0
2015-02-04T06:24:26.420Z
I want to download the bulk data from the bitcoin-qt for the sake of speed as when I download blockchain from DNS from the start it takes too much time. So when all the data has been downloaded from the local peer than  I want to switch to the DNS
Ankit Sharma
CC BY-SA 3.0
2015-02-03T05:50:55.030Z
2015-02-03T05:56:11.733Z
2015-02-03T05:56:11.733Z
morsecoder
<p>Here is my code.</p>

<pre><code>    params = TestNet3Params.get();
    blockStore = new MemoryBlockStore(params);

    chain = new BlockChain(params, blockStore);
    chain.addWallet(newwallet);

    final PeerGroup peerGroup = new PeerGroup(params, chain);
    peerGroup.setUserAgent("PeerMonitor", "1.0");
    peerGroup.setMaxConnections(1);
    peerGroup.addAddress(new PeerAddress(InetAddresses.forString("192.168.1.253"), 18333));

    peerGroup.addEventListener(new AbstractPeerEventListener() {
        @Override
        public void onPeerDisconnected(Peer peer, int peerCount) {
            System.out.println("hello "+peer.getAddress().getAddr().getHostAddress());
            peerGroup.addPeerDiscovery(new DnsDiscovery(params)); 
            System.out.println("DNS Added...!!");

        }
    });

    peerGroup.startAsync();

    peerGroup.downloadBlockChain();
    System.out.println("Full Downloaded...!!");

    List&lt;Peer&gt; peers=peerGroup.getConnectedPeers();


    System.out.println(peers);
    for (Peer peer : peers) {
        if(peer.getAddress().getAddr().getHostName().equals("192.168.1.253"))
        {
            System.out.println("checking peers...!!");
            break;
        }
    }
    System.out.println("going to stop...!!");
</code></pre>

<p>I want to stop the local peer and switch to the DNS when the downloading gets completed.</p>

<bitcoinj>
How to manually disconnect a peer from peergroup?
Ankit Sharma
# How to manually disconnect a peer from peergroup?

(Asked by: username_0 on 2015-02-02)

Here is my code.

```
    params = TestNet3Params.get();
    blockStore = new MemoryBlockStore(params);

    chain = new BlockChain(params, blockStore);
    chain.addWallet(newwallet);

    final PeerGroup peerGroup = new PeerGroup(params, chain);
    peerGroup.setUserAgent("PeerMonitor", "1.0");
    peerGroup.setMaxConnections(1);
    peerGroup.addAddress(new PeerAddress(InetAddresses.forString("192.168.1.253"), 18333));

    peerGroup.addEventListener(new AbstractPeerEventListener() {
        @Override
        public void onPeerDisconnected(Peer peer, int peerCount) {
            System.out.println("hello "+peer.getAddress().getAddr().getHostAddress());
            peerGroup.addPeerDiscovery(new DnsDiscovery(params)); 
            System.out.println("DNS Added...!!");

        }
    });

    peerGroup.startAsync();

    peerGroup.downloadBlockChain();
    System.out.println("Full Downloaded...!!");

    List<Peer> peers=peerGroup.getConnectedPeers();

    System.out.println(peers);
    for (Peer peer : peers) {
        if(peer.getAddress().getAddr().getHostName().equals("192.168.1.253"))
        {
            System.out.println("checking peers...!!");
            break;
        }
    }
    System.out.println("going to stop...!!");

```

I want to stop the local peer and switch to the DNS when the downloading gets completed.

**Tags:** bitcoinj


---

## 1 answers

### Answer by username_1 on 2015-02-03. Score: 1

Simply:

```
peer.close();

```

[`Peer.java`](https://github.com/bitcoinj/bitcoinj/blob/v0.12.2/core/src/main/java/org/bitcoinj/core/Peer.java) extends [`PeerSocketHandler.java`](https://github.com/bitcoinj/bitcoinj/blob/v0.12.2/core/src/main/java/org/bitcoinj/core/PeerSocketHandler.java), which is the class that defines the `close()` method, so maybe that's why you weren't seeing it. It would also probably be easier to find if it were called `disconnect()`.

> **Comments:**
> **username_0**: I have called peer.close() and it triggers onDisconnected() event but and in this event I am adding DNS to the peerGroup but despite of adding DNS it is still looking for peer. I want to completely switch from peer to DNS  
> **username_1**: @username_0, What do you mean by adding DNS? Do you care which peers you are connected to? And why can you not just add it from the start, why do you have to do it after a peer is disconnected?  
> **username_0**: I want to download the bulk data from the bitcoin-qt for the sake of speed as when I download blockchain from DNS from the start it takes too much time. So when all the data has been downloaded from the local peer than  I want to switch to the DNS  

---

