GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Socket connects and gets data from non-existing service · Issue #2927 · nodejs/node

Socket connects and gets data from non-existing service #2927

New issue
Copy link
New issue
Copy link
Closed
Closed
Socket connects and gets data from non-existing service#2927
Copy link
Labels
netIssues and PRs related to the net subsystem.

Description

@rveerd
rveerd
opened on Sep 17, 2015
Issue body actions

The following program tries to connect to a TCP service on "localhost". I do not have a service running at port 4096, so it continuously gets ECONNREFUSED, as expected. However, if you let it run long enough (a couple of minutes on my machine), eventually it does connect, sends data and even receives data, which appears to be the same data as was send.

Any ideas what is going on?

It seems this only happens with "localhost" and with higher numbered ports For example, it does not show this behaviour when I try and connect to port 7 (which is also not available on my machine).

This is with Node.js v0.12.7 and Linux 2.6.32-504.el6.x86_64.

/*jslint node:true*/
"use strict";

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
if (!String.prototype.endsWith) {
    String.prototype.endsWith = function(searchString, position) {
        var subjectString = this.toString();
        if (position === undefined || position > subjectString.length) {
            position = subjectString.length;
        }
        position -= searchString.length;
        var lastIndex = subjectString.indexOf(searchString, position);
        return lastIndex !== -1 && lastIndex === position;
    };
}

var net = require("net");

var SERVICE_PORT = 4096;
var SERVICE_HOST = "localhost";

function send(req) {
    var service = new net.Socket();

    service.on("connect", function () {
        console.log("Connected");
        service.setEncoding("utf8");
        service.end(req);
    });

    var line = "";
    service.on("data", function (data) {
        line += data;
        if (line.endsWith("\n")) {
            console.log("Got response [%s]", line.trim());
        }
    });

    service.on("error", function (err) {
        console.error(err);
    });

    service.on("close", function (had_error) {
        if (had_error) {
            console.log("Connection closed WITH ERROR");
            send(req); // Try again.
        } else {
            console.log("Connection closed WITH SUCCESS");
        }
    });

    service.connect(SERVICE_PORT, SERVICE_HOST);
}

send("PING\n");

Example output (last few lines):

Connection closed WITH ERROR
{ [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect' }
Connection closed WITH ERROR
{ [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect' }
Connection closed WITH ERROR
Connected
Got response [PING]
Connection closed WITH SUCCESS

Activity

  1. brendanashworth
    added
    netIssues and PRs related to the net subsystem.
    on Sep 17, 2015
  2. bnoordhuis commented on Sep 18, 2015

    @bnoordhuis
    bnoordhuis
    on Sep 18, 2015
    Member
    More actions

    I don't think that's related to node, just that the socket's ephemeral port sooner or later ends up being 4096.

  3. rveerd commented on Sep 18, 2015

    @rveerd
    rveerd
    on Sep 18, 2015
    Author
    More actions

    You mean the application socket is communicating with itself?

    I captured the network traffic over the local interface and this indeed appears to be happening. The local port of the socket varies until it is the same as the service port.

    I am surprised that this possible and leads to valid TCP message exchange pattern (SYN, ACK and sequence numbers) that matches the sockets internal state.

    Agreed this is not related to node. Closing the issue.

  4. rveerd
    closed this as completedon Sep 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    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

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.