Tom Pusateri 0cf5761579 Add TLS example and fix TCP example. (#31)
* Add TLS example and fix TCP buffer management.
Allow all examples to run from examples directory.

* Inspect response data decoding length, not request.

Simplify package require().
2018-03-27 22:06:14 +02:00

29 lines
605 B
JavaScript

'use strict'
const packet = require('..')
const dgram = require('dgram')
const socket = dgram.createSocket('udp4')
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const buf = packet.encode({
type: 'query',
id: getRandomInt(1, 65534),
flags: packet.RECURSION_DESIRED,
questions: [{
type: 'A',
name: 'google.com'
}]
})
socket.on('message', function (message, rinfo) {
console.log(rinfo)
console.log(packet.decode(message)) // prints out a response from google dns
socket.close()
})
socket.send(buf, 0, buf.length, 53, '8.8.8.8')