Receiving on an Unix-Dgram-Socket

97 views
Skip to first unread message

MaZderMind

unread,
Mar 28, 2011, 1:14:17 PM3/28/11
to nodejs
Hi

I'm trying to talk to a server (tirex) which can be connected through
a unix dgram socket. I can send messages to it by using client =
dgram.createSocket('unix_dgram', ..) and client.send(..) but the
server is not able to reply to that message, because it's unable to
determine peer address.

According to the docs I need to call dgram.bind in order to receive
messages when using a unix dgram socket, but I don't want to bind to
my own socket but just receive replies to my message.

This is possible using an UDP-socket (the message-event gets fired
when a message returns) without calling bind but it's not possible
using unix sockets.

An I missing sth. or is this functionality missing in node?

Peter

Matt Ranney

unread,
Mar 28, 2011, 6:29:06 PM3/28/11
to nod...@googlegroups.com, MaZderMind
On Mon, Mar 28, 2011 at 7:14 AM, MaZderMind <goo...@mazdermind.de> wrote:

This is possible using an UDP-socket (the message-event gets fired
when a message returns) without calling bind but it's not possible
using unix sockets.

An I missing sth. or is this functionality missing in node?

Bind is how you set an address.  If you don't do it, the server doesn't get an address.

Check out the dgram tests in the tests/ directory of the node source.  There are examples of exactly this scenario.

chris

unread,
Mar 28, 2011, 11:42:19 PM3/28/11
to nodejs
Here's something i'm working on right now. Basically you'll only get
your 'outgoing' port number after you send your message. So
immediately after sending, listen for the incoming message on the port
you sent on:

var dgram = require('dgram'); // dgram is UDP

// Listen for responses
function listen(port) {
var server = dgram.createSocket("udp4");

server.on("message", function (msg, rinfo) {
console.log('-----');
console.log("server got: " + msg + " from " +
rinfo.address + ":" + rinfo.port);
console.log('-----');
});

server.on("listening", function () {
var address = server.address();
console.log("server listening " +
address.address + ":" + address.port);
});

server.bind(port); // Bind to the random port we were given when
sending the message, not 1900

// Give it a while for responses to come in
setTimeout(function(){
console.log("Finished waiting");
server.close();
},2000);
}

function search() {

var message = new Buffer(
"M-SEARCH * HTTP/1.1\r\n" +
"HOST:239.255.255.250:1900\r\n" +
"MAN:\"ssdp:discover\"\r\n" +
"ST:ssdp:all\r\n" + // Essential, used by the client to specify what
they want to discover, eg 'ST:ge:fridge'
"MX:1\r\n" + // 1 second to respond (but they all respond
immediately?)
"\r\n"
);

var client = dgram.createSocket("udp4");
client.send(message, 0, message.length, 1900, "239.255.255.250");
listen(client.address().port); // Can only get the port *after*
sending the message
client.close();
}

search();
Reply all
Reply to author
Forward
0 new messages