title: How to send v2 traps in net snmp using c
body: <p>I have a the following configurations:</p>
  
  <ol>
  <li>Trap oid = .1.3.6.1.4.1.78945.1.1.1.1.1</li>
  <li>Trap variable oid = .1.3.6.1.4.1.78945.1.1.2.1.0, variable type = string</li>
  <li>Another Trap variable oid = .1.3.6.1.4.1.78945.1.1.2.4.0, variable type = integer.</li>
  <li>Trap listener ip and port = 192.168.4.10:1234</li>
  </ol>
  
  <p>How can I send traps using C or C++ and net-snmp module in linux? I need a sample code. All sample codes at net-snmp site did not work for me.</p>
  
  <p>my sample code:</p>
  
  <pre><code>#include &lt;net-snmp/net-snmp-config.h&gt;
  #include &lt;net-snmp/net-snmp-includes.h&gt;
  
  oid             objid_id[] = { 1,3,6,1,4,1,78945,1,1,2,4,0};
  oid             objid_name[] = { 1,3,6,1,4,1,78945,1,1,2,1,0};
  oid           trap_oid[] = {1,3,6,1,4,1,78945,1,1,1,1,1};
  
  
  int main()
  {
      netsnmp_session session, *ss;
      netsnmp_pdu    *pdu, *response;
  
      char comm[] = "public";
      snmp_sess_init( &amp;session );
      session.version = SNMP_VERSION_2c;
      session.community = comm;
      session.community_len = strlen(session.community);
      session.peername = "192.168.4.10:1234";
      ss = snmp_open(&amp;session);
      if (!ss) {
        snmp_sess_perror("ack", &amp;session);
        exit(1);
      }
  
      pdu = snmp_pdu_create(SNMP_MSG_TRAP2);
      pdu-&gt;community = comm;
      pdu-&gt;community_len = strlen(comm);
      pdu-&gt;enterprise = trap_oid;
      pdu-&gt;enterprise_length = sizeof(trap_oid) / sizeof(oid);
      pdu-&gt;trap_type = SNMP_TRAP_ENTERPRISESPECIFIC;
      snmp_add_var(pdu, objid_name, sizeof(objid_name) / sizeof(oid), 's', "Test Name");
      snmp_add_var(pdu, objid_id, sizeof(objid_id) / sizeof(oid), 'i', "5468");
  
      send_trap_to_sess (ss, pdu);
      snmp_close(ss);
      return (0);
  }
  </code></pre>
  
  <p>The heartbeat notification example from net-snmp site confused me with where to give the listener details?</p>
  
  <p>Thank you in advance.</p>