Home > Blockchain >  Socket.io Javascript nodejs, TCP socket
Socket.io Javascript nodejs, TCP socket

Time:02-01

I am trying to send a TCP Request to a server via IP:PORT...

This code works for me:

var net = require('net'); var client = new net.Socket();
const port = 1337; const HOST = "192.168.1.101";
function connect() {
  client.connect({ port: PORT, host: HOST });
  client.on('data', (data) => {
    console.log(data);
    client.destroy();
    });
  
    client.on('close', function() {
    console.log('Connection closed');
    });
  }

This code hangs, no response: (also note: I input 'http://' prior to the IP in io.connect for the response to not return the hostname as undefined (see below)):

console.log(socket); //returns=> undefined://192.168.1.101:1337
//uri: 'undefined//192.168.1.101:730',


const io = require('socket.io-client');
const socket = io.connect('192.168.1.101:1337');//=undefined hostname; works, but hangs
//const socket = io.connect('http://192.168.1.101:1337'); //works, but hangs as well...

function connect() {
  console.log('connecting...');
  **socket.on('connect', () => {**
    console.log('Successfully connected!');
      // add handlers for socket events
  }); //I never receive response from socket.on('connect', ()=> {...

Either way I try to send a socket, I don't get a response... it's just hanging, waiting for a response...

ANY HELP WOULD BE GREATLY APPRECIATED!

console.log(socket); //this command returns the following response from command down below:

socket fail undefined//192.168.1.101:1337

<ref *1> Socket { connected: false, disconnected: true, receiveBuffer: [], sendBuffer: [], ids: 0, acks: {}, flags: {}, io: Manager { nsps: { '/': [Circular *1] }, subs: [ [Function: subDestroy], [Function: subDestroy], [Function: subDestroy] ], opts: { path: '/socket.io', hostname: '192.168.1.101', secure: false, port: '1337' }, setTimeoutFn: [Function: bound setTimeout], clearTimeoutFn: [Function: bound clearTimeout], _reconnection: true, _reconnectionAttempts: Infinity, _reconnectionDelay: 1000, _reconnectionDelayMax: 5000, _randomizationFactor: 0.5, backoff: Backoff { ms: 1000, max: 5000, factor: 2, jitter: 0.5, attempts: 0 }, _timeout: 20000, _readyState: 'opening', uri: 'http://192.168.1.101:1337', encoder: Encoder {}, decoder: Decoder {}, _autoConnect: true, engine: Socket { setTimeoutFn: [Function: bound setTimeout], clearTimeoutFn: [Function: bound clearTimeout], secure: false, hostname: '192.168.1.101', port: '1337', transports: [Array], readyState: 'opening', writeBuffer: [], prevBufferLen: 0, opts: [Object], id: null, upgrades: null, pingInterval: null, pingTimeout: null, pingTimeoutTimer: null, transport: [XHR], _callbacks: [Object] }, skipReconnect: false, _callbacks: { '$open': [Array], '$packet': [Array], '$error': [Array], '$close': [Array] } }, nsp: '/', subs: [ [Function: subDestroy], [Function: subDestroy], [Function: subDestroy], [Function: subDestroy] ] } */

CodePudding user response:

You forgot to declare the protocol in your URL, try http://192.168.1.101:1337 or ws://192.168.1.101:1337 (If your server does not use SSL)

And be sure the server side also implements socket.io server not just TCP protocol.

  •  Tags:  
  • Related