net socket request never arrives

Posted on
  • I have the following code snippet:

    var options = { host:"192.168.0.113", port:8823 };		
    var socket = require("net").connect( options, function() {
           socket.on('data', function(data) {
                console.log("HTTP> "+data);
           } );
    } );
    socket.end( JSON.stringify( obj ) );		
    console.log( 'should be sent by now' );
    

    Upon running, the server does not receive any data.

    I tried also socket.write( JSON.stringify( obj ) ) and putting write/end calls inside of the callback function - neither worked.

    The test code in Groovy works just fine:

    Socket s = new Socket()
    s.connect( new InetSocketAddress( "192.168.0.113", 8823 ) )
    s.outputStream.withWriter{ it << 'aaaaa' }
    
  • In your Espruino code running on the ESP8266, we might try adding:

    socket.on('close', function() {
       console.log("Socket closed!");
    });
    

    it may be that there is an underlying socket connection error. Although I don't have any experience with it, another possibility would be to enable the ESP8266 specific debugging with:

    require("ESP8266").logDebug(true);
    

    and we can see if additional diagnostics are produced.

    I'd also look to see if base connectivity between the ESP8266 and 192.168.0.113 have been achieved using:

    require("ESP8266").ping(...)
    
  • Attached is some code to try for TCP comunications.
    You will need to edit the SSID and key to configure your router access.
    On one Pico start the server code and note the IP address it will display.
    Disconnect from WebIDE and connect with a Terminal program such as Putty.
    On a second Pico load the client code using WebIde.
    In addition to the SSID and key edit the IP variable to match the server IP address
    You can also change the port in both server and client code as it is 1234 in this code.


    2 Attachments

  • @Kolban thanks for the answer!

    1. Previously I had socket.on( 'close', .. ) and it was not invoked.

    2. I inserted require("ESP8266").logDebug(true); but no additional console output appeared...

    3. I can't really use ping, as the server doesn't provide any ICMP ports and so the code:

      require("ESP8266").ping( '192.168.0.113', function( p ){ console.log( 'ping', p ); } );
      

    prints out:

    =undefined
    ping { "totalCount": 0, "totalBytes": 0, "totalTime": 0, "respTime": 0,
    "seqNo": 0, "timeoutCount": 0, "bytes": 0, "error": -1 }

  • @ClearMemory041063 thanks for the code!

    Connecting 2 esp's is not my use-case. I rather need to establish a connection to a plain java (Server)Socket. My ESP JS code is very similar to yours. The code snippet I posted is executed in wifi.connect()'s callback-function and it's wifi-ing successfully. The rest is almost 1 to 1.

  • Btw, I tried also to use require( 'http' ) and it didn't work as well.

    The funny thing is, that WebSocket works like charm:

    var WS = require( "ws" );
    self.ws = new WS( self.serverHost, { port:8822 } );
    
  • So, after peeking the websocket code, I figured out what I did wrong. The correct code would be:

    require("net").connect( { host:host, port:8823 }, function ( socket ) {
          socket.end( JSON.stringify( obj ) );
    } );
    

    The trick was to use the socket as a callback-function's argument, instead of the connect() result.
    The documentation should be updated I guess, as this line is totally misguiding

  • @Injecteer, I'm not so sure what you try to do...

    The documented code shows that there are two event driven callbacks setup in the (client) connect-callback: on('data',cb1) and on('end',cb2), hence they are BOTH NOT EXECUTED right away. They are executed on respective event driven by the server, where as in your code, (client) socket.end is executed right away - not event driven - while the connect event is still on its way...

    In your code I read that the client is not waiting for data. It just connects, and when the connection is established - connection-callback executed (and socket passed into the callback) - the client sends data and closes the socket.

    The only issue with the documentation is that the comment in the server has a typo: A new client (h)as connected. ;-)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

net socket request never arrives

Posted by Avatar for Injecteer @Injecteer

Actions