<p>I got the answer myself. Sorry!</p>

<p>The <code>src/net.cpp</code> has this logic which explains all:</p>

<pre><code>void CNode::BeginMessage(const char* pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSend)
{
    ENTER_CRITICAL_SECTION(cs_vSend);
    assert(ssSend.size() == 0);
    ssSend &lt;&lt; CMessageHeader(Params().MessageStart(), pszCommand, 0);
    LogPrint("net", "sending: %s ", SanitizeString(pszCommand));
}
</code></pre>

CC BY-SA 3.0
2015-12-22T04:05:49.687Z
@Jannes - True! But the forum allows me to mark my own post as answer only after two days.
Paarth
CC BY-SA 3.0
2015-12-21T03:22:31.440Z
2015-12-21T03:22:31.440Z
Paarth
<p>In order to send a <code>NetMsgType::PING</code> message to a node <code>pnode</code>, we simply invoke the function <code>pnode-&gt;PushMessage(NetMsgType::PING)</code> in <code>src/main.cpp</code></p>

<p>This translates into following logic in <code>src/net.h</code>:</p>

<pre><code>void PushMessage(const char* pszCommand)
{
    try
    {
        BeginMessage(pszCommand);
        EndMessage(pszCommand);
    }
    catch (...)
    {
        AbortMessage();
        throw;
    }
}
</code></pre>

<p>If we look carefully, the <code>BeginMessage</code> and <code>EndMessage</code> are just for acquiring and releasing the lock. Where exactly is the <code>PING</code> getting passed to the other node? I think the working is similar for other message types but the overloaded function <code>PushMessage</code> may use a Datastream which I guess does the passing to requested node. But I am particularly clueless about cases like <code>NetMsgType::GETADDR</code>, <code>NetMsgType::VERACK</code> and old version without nonce of <code>NetMsgType::PING</code> where there is only the command string <code>pszCommand</code> being passed</p>

<bitcoin-core><peers>
How are messages exactly sent in bitcoin p2p protocol
Paarth
# How are messages exactly sent in bitcoin p2p protocol

(Asked by: username_0 on 2015-12-21)

In order to send a `NetMsgType::PING` message to a node `pnode`, we simply invoke the function `pnode->PushMessage(NetMsgType::PING)` in `src/main.cpp`

This translates into following logic in `src/net.h`:

```
void PushMessage(const char* pszCommand)
{
    try
    {
        BeginMessage(pszCommand);
        EndMessage(pszCommand);
    }
    catch (...)
    {
        AbortMessage();
        throw;
    }
}

```

If we look carefully, the `BeginMessage` and `EndMessage` are just for acquiring and releasing the lock. Where exactly is the `PING` getting passed to the other node? I think the working is similar for other message types but the overloaded function `PushMessage` may use a Datastream which I guess does the passing to requested node. But I am particularly clueless about cases like `NetMsgType::GETADDR`, `NetMsgType::VERACK` and old version without nonce of `NetMsgType::PING` where there is only the command string `pszCommand` being passed

**Tags:** bitcoin-core, peers


---

## 1 answers

### Answer by username_0 on 2015-12-21. Score: 4 | Accepted answer

I got the answer myself. Sorry!

The `src/net.cpp` has this logic which explains all:

```
void CNode::BeginMessage(const char* pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSend)
{
    ENTER_CRITICAL_SECTION(cs_vSend);
    assert(ssSend.size() == 0);
    ssSend << CMessageHeader(Params().MessageStart(), pszCommand, 0);
    LogPrint("net", "sending: %s ", SanitizeString(pszCommand));
}

```

> **Comments:**
> **username_0**: @Jannes - True! But the forum allows me to mark my own post as answer only after two days.  

---

