JavaScript Net.connect() - Explicitly Set Local Endpoint IP Address

- 1 answer

I can't find a way to set the local endpoint field in the net.connect() function. I need to explicitly set the local endpoint to a specific IP address.

I am using the node-ads library to connect a node process to a PLC device.

In my example:

ads.tcpClient = net.connect(
    function () {
      ads.connected = true

The connect() function does not offer a parameter for this. The returning socket object also does not seem to have a field for the endpoint at all. What am I doing wrong?

The reason for this is that I am connecting to a Beckhoff PLC where the connection requires a differnt host IP address if another connection already exists on the same computer. The problem is mentionend here.

I know for a fact that it is possible in the Beckhoff-ADS C# implementation, as this code works:

 var socket = new AmsSocket(targetip, amsport)
     LocalEndPoint = new IPEndPoint(IPAddress.Parse(sourceip), 0)


UPDATE Solved the issue by doing the following:

  • Change your ads.js file to set the source IP address explicitly
  • It's better to not do this in node_modules, but to just copy the ads.js file somewhere where it does not get overwritten

Locate in ads.js:

var connect = function (cb) {
  var ads = this
  ads.tcpClient = net.connect({
    port: ads.options.port,
    localAddress: ads.options.sourceIp,   // the part you have to add

Pass the sourceIp in your options for the ADS connection:

 public options = {
        host: "",
        amsNetIdTarget: "",
        amsNetIdSource: "",
        sourceIp: "",

Connect to your PLC:

const client = ads.connect(this.options, () => {
            console.log("Trying to connect.");
            client.readDeviceInfo((err, result) => {
                if (err) {
                    console.log("Error: " + err);
                console.log("Connection result: " + JSON.stringify(result));

Simple enough, but unfortunately not supported in node-ADS.