Repository navigation
How to use fd with net.Socket? #8297
Description
Activity
TypeError: Unsupported fd type: FILE
nullis coerced to zero in your example and apparently file descriptor zero refers to a file. If you use the.fdoption, it must refer to a valid TCP or pipe file descriptor.Having a FILE fd does not help obviously, but there is literally zero documentation on how to create a file descriptor for TCP.
What exactly are you trying to do? If you want to connect to a remote host over TCP, hy do you want to create the file descriptor explicitly instead of letting node.js / libuv handle that for you?
- addedquestionIssues asking questions about Node.js.Issues asking questions about Node.js.netIssues and PRs related to the net subsystem.Issues and PRs related to the net subsystem.
on Aug 27, 2016 Does that mean a valid TCP pipe is only being created by
net.Serverand not create-able directly? Just in case: How to create a valid TCP pipe yourself?I had to add
fd: nullin a previous node version because of TTY bugs when listening to stdout/stdin. I guess that has changed somewhere in the last three(?) months. I was assuming that this is wanted, but I removed it now from the code.'TCP pipe' is a bit of an oxymoron - TCP sockets and pipes are different things. The
.fdproperty is for creatingnet.Socketinstances out of existing file descriptors that refer to a TCP socket or a pipe.I guess that has changed somewhere in the last three(?) months.
I'm not aware of any such changes, I don't think the logic around
.fdchanged much since v0.10.net.Socketinstances out of existing file descriptors that refer to a TCP socket or a pipeHow to do so in the code? Let's say I want to create and reuse such a socket/pipe.
So at the constructor point in time there can't be such a thing as an already existing TCP Pipe(!?!?!?!).
Put it in man pages terms, if only the public
netAPI are used,socket(2),bind(2),listen(2)/connect(2)all happen inserver.listen()orsocket.connect()calls,new Socket()doesn't do any of these. So yea, when doingnew Socket()nothing is actually allocated. On the other hand, after it starts connecting/listening, the fd can be accessed viasocket._handle.fd, though it's not recommended to use a underscore-prefixed property...(it's possible to add a newsocket.getFD()though)I guess at the moment there is no way to get a fd of a fresh unbound socket purely with the existing public
netAPI..you can get a fd from somewhere else (e.g. outside Node.js) though.
This code will cause problems with an error:
Randomly, depending on how stdout and stderr are repiped of the process, it will give this error:
TypeError: Unsupported fd type: FILE
What the heck does that mean? Everything on stackoverflow is 100% unrelated where people try to create sockets to files. Here I want to create a Socket to a TCP remote port (client-side socket implementation), but the pipe itself is later created using
connector.connect({host: host, port: port}).So at the constructor point in time there can't be such a thing as an already existing TCP Pipe(!?!?!?!).
Debugging
I figured out that the
TCPtype file descriptor is necessary lib/net.js#L31.The
tty_wrap.guessHandleType(fd)uses internal representations to return type TCP tty_wrap.cc#L66.Solution?
How to solve this and create a file descriptor for net.Socket?
Having a FILE fd does not help obviously, but there is literally zero documentation on how to create a file descriptor for TCP. I tried to read all the TCP relevant code, but UV_TCP is pretty much everywhere in code related to streams, files, handles and uv ... seems as this is a if/else construct in the whole codebase.
OS Information
Debian, Ubuntu, Archlinux. All amd64 and verified the identical problem.