7

I've been working with node.js for the past couple of weeks and I need to implement the FAST-CGI protocol. The problem is that when I create a UNIX socket (via "new Socket") I need to get the filename, or file descriptor. But socket.fd is null (default parameter).

My question is: Does "new Socket" creates a operating system socket object file, and if so, how can I get the Socket File Descriptor or File Name?

I'm not sure if this is how I should create a Socket, but here is the case:
node:

var net = require(net)
var socket = new net.Socket()
console.log(socket);

{
 bufferSize: 0,
 fd:null,
 type: null,
 allowHalfOpen: false,
 _writeImpl: [Function],
 _readImpl: [Function],
 _shutdownImpl: [Function]
}
6
  • Yeah, exactly, socket.fd is empty until you actually do socket.connect() ;) Commented Sep 23, 2011 at 16:07
  • Oohhh.... So the actual system socket is created when connect is called.But when i call socket.connect('/tmp/file.sock') it gives me (Error: ENOENT, No such file or directory)... Should I create the file before ? or what should I do ? Commented Sep 23, 2011 at 16:46
  • Yes, you should. Also, as I noted in my answer below, FD can be given when creating new socket: new net.Socket({fd : '/tmp/file.sock'}) Commented Sep 23, 2011 at 17:06
  • It turns out, that you should only specify the existing file descriptor of socket, and it should be a number (fd:NaN)... So how do I create the file ? Should I use fs.open ??? Commented Sep 23, 2011 at 17:18
  • Yeah, fs.open is the way. I appended the code to my answer below. Works for me on Ubuntu (with node 0.4.11) Commented Sep 23, 2011 at 17:30

1 Answer 1

7

Well when you connect a socket, socket.fd is not null, at least not in my case, so provide an example case please.

Note that you can also specify existing file descriptor at socket creation.

Edit:

var net = require('net'),
    fs = require('fs'),
    sock;

// Create socket file
fs.open('/tmp/node.test.sock', 'w+', function(err, fdesc){
    if (err || !fdesc) {
        throw 'Error: ' + (err || 'No fdesc');
    }

    // Create socket
    sock = new net.Socket({ fd : fdesc });
    console.log(sock);
});
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't work anymore, it says "Unsupported fd type: FILE". :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.