How to connect to node.js with C++ client??

5,293 views
Skip to first unread message

kmouja

unread,
Aug 30, 2010, 4:58:55 PM8/30/10
to nodejs
Hi all,

I'm new to node.js, I still managed to install it and run examples
with socket.io.

I need to connect to node.js from a desktop c++ application. I tried
to write a small socket client with a node.js socket server listening,
but my client does not connect (connection timeout), and my server
does not get a connection event...

I also write a php socket connection code and it won't connect either.

Is there something I am missing? node.js can connect to c++ socket,
right?

Guillermo Rauch

unread,
Aug 30, 2010, 6:14:15 PM8/30/10
to nod...@googlegroups.com
Node.JS can open a TCP socket through net.createConnection
If you intend for Node.JS to act as a proxy between a TCP stream and a browser, you can do that by leveraging socket.io and pass the messages from/to the C++ server.

--
Guillermo Rauch
http://devthought.com

mscdex

unread,
Aug 30, 2010, 7:16:22 PM8/30/10
to nodejs
If you're not running both the client and server on the same machine,
make sure that your server.listen() statement does not specify an IP
address as the second argument. Otherwise, you won't be able to
connect to the node.js server.

Isaac Schlueter

unread,
Aug 30, 2010, 7:31:18 PM8/30/10
to nod...@googlegroups.com
On Mon, Aug 30, 2010 at 16:16, mscdex <msc...@gmail.com> wrote:
> If you're not running both the client and server on the same machine,
> make sure that your server.listen() statement does not specify an IP
> address as the second argument. Otherwise, you won't be able to
> connect to the node.js server.

Sure you will. Just not as "localhost".

--i

kmouja

unread,
Aug 31, 2010, 3:54:21 AM8/31/10
to nodejs
thank you guys! it works. I needed to remove the second argument of
the listen statement just as mscdex recommended.

Jimb Esser

unread,
Sep 4, 2012, 2:02:53 PM9/4/12
to nod...@googlegroups.com
socket.io is not a simple/regular TCP socket server.  If you want to connect with standard raw sockets (without implementing or using an entire WebSockets or HTTP library in your C++ application), just use require('net').createServer (low-level socket listening).

On Thursday, August 30, 2012 12:42:39 PM UTC-7, wes wrote:
Hey. I realize this was years ago, but if this somehow reaches you... I think I'm struggling with a similar issue and I would really appreciate your help :)

Just to clarify, you successfully got a c++ application to connect to your node server using a regular old tcp connection, right?

I'm trying to do that with a server that looks roughly like:
var express = require('express');
var app = express();
var server = require('http').createServer(app)
var io = require('socket.io').listen(server);

app.use(express.cookieParser());
app.use(express.session({
  ...
}));
app.use(app.router);

// response handling -------
app.get('/', function(req, res){
  console.log('request get /');
});

io.sockets.on('connection', function(socket) {
  console.log('sockets.io connection!');
});
// -------

port = 3000
app.listen(port);
console.log('Listening on port: ' + port);

When I connect from my application all seems well (the connection completes and I can send data without encountering any errors) but I never trigger anything on the node server (io.sockets.on('connection' ...) is never called and no errors are spit out).

Do you see anything obviously wrong with my approach?
Again, I really really appreciate any help you can give me!

Cheers,
-Wes
Reply all
Reply to author
Forward
0 new messages