Skip to content

net.socket has no setsockopt() method,and How to create a socket whose handle is not null #49418

@dong-king

Description

@dong-king

What is the problem this feature will solve?

  1. My scenario is that the TCP client read speed is slow, so I need to set SO_RCVBUF through setsockopt().
    According to this post, I have implemented setsockopt().
    But can Node.js directly expose the corresponding interface?

  2. The normal TCP client implementation code is:

var conn = new net.Socket();  // conn._handle will be null
conn.connect(5555, '127.0.0.1');  // conn._handle will be not null

Only after the socket is connected, its handle is not null.

But because the setting of SO_RCVBUF needs to be before socket connect (client) or socket listen (server), so I do it with the following code:

const TCPWrap = process.binding('tcp_wrap');
const { TCP } = TCPWrap;
const handle  = new TCP(TCPWrap.constants.SOCKET);

var conn = new net.Socket({handle});
console.log(conn._handle)	=> print null
setSocketOption(conn._handle, SOL_SOCKET, SO_RCVBUF, 1024);
conn.connect(5555, '127.0.0.1');

I expected to get a socket with a non-null handle at line 4, but in fact, conn._handle is still null.
Is there any way to allocate the socket handle before the connect operation?

What is the feature you are proposing to solve the problem?

  1. Provide setsockeopt() interface;
  2. Provides a method to initialize its handle when instantiating net.Socket()

What alternatives have you considered?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.netIssues and PRs related to the net subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions