### socket.send(buf, offset, length, port, address, [callback])
-* `buf` Buffer object. Message to be sent
+* `buf` Buffer object or string. Message to be sent
* `offset` Integer. Offset in the buffer where the message starts.
* `length` Integer. Number of bytes in the message.
* `port` Integer. destination port
assigned a random port number and bound to the "all interfaces" address
(0.0.0.0 for `udp4` sockets, ::0 for `udp6` sockets).
+With consideration for multi-byte characters, `offset` and `length` will
+be calculated with respect to
+[byte length](buffer.html#buffer_class_method_buffer_bytelength_string_encoding)
+and not the character position.
+
Example of sending a UDP packet to a random port on `localhost`;
var dgram = require('dgram');
callback) {
var self = this;
+ if (util.isString(buffer))
+ buffer = new Buffer(buffer);
+
if (!util.isBuffer(buffer))
- throw new TypeError('First argument must be a buffer object.');
+ throw new TypeError('First argument must be a buffer or string.');
offset = offset | 0;
if (offset < 0)
assert.throws(function() {
socket.send(buf, 4, 4, common.PORT, '127.0.0.1', assert.fail);
});
+assert.throws(function() {
+ socket.send('abc', 4, 1, common.PORT, '127.0.0.1', assert.fail);
+});
+assert.throws(function() {
+ socket.send('abc', 0, 4, common.PORT, '127.0.0.1', assert.fail);
+});
+assert.throws(function() {
+ socket.send('abc', -1, 2, common.PORT, '127.0.0.1', assert.fail);
+});
socket.close(); // FIXME should not be necessary
// Should throw but not crash.
var socket = dgram.createSocket('udp4');
-assert.throws(function() { socket.send('BAM', 0, 1, 1, 'host') }, TypeError);
-assert.throws(function() { socket.sendto('BAM', 0, 1, 1, 'host') }, TypeError);
+assert.throws(function() { socket.send(true, 0, 1, 1, 'host') }, TypeError);
+assert.throws(function() { socket.sendto(5, 0, 1, 1, 'host') }, TypeError);
socket.close();
var common = require('../common');
var assert = require('assert');
-var Buffer = require('buffer').Buffer,
- fs = require('fs'),
+var fs = require('fs'),
dgram = require('dgram'), server, client,
server_port = 20989,
- message_to_send = new Buffer('A message to send'),
+ message_to_send = 'A message to send',
timer;
server = dgram.createSocket('udp4');