简体   繁体   English

从C客户端接收和发送msgs到nodejs服务器

[英]Receive and send msgs from C client to nodejs server

I have made a TCP client in C, and Im looking to make the server side in nodejs for simplicity and for easy integration into a existing app... 我在C中创建了一个TCP客户端,我希望在nodejs中创建服务器端以简化并轻松集成到现有应用程序中...

Well in my C code I receive and send msgs in the next way: 在我的C代码中,我接收并以下一种方式发送消息:

numBytes = send( sock, &timeStamp, sizeof( timeStamp ), 0 );
if( numBytes < 0 )
    DieWithSystemMessage( "send( ) failed" );

After reading a little about the "net" and "socket.io" packages for nodejs I haven't found the way to make it work... I apologize if this is somethog simple but it is my first time working with nodejs. 在阅读了一些关于nodejs的“net”和“socket.io”软件包之后,我还没有找到让它工作的方法...我很抱歉,如果这是简单的东西,但这是我第一次使用nodejs。 If you have some blog or github link that have made something similar I will be glad to take a look, Thanks!!! 如果你有一些类似的博客或github链接我会很高兴看看,谢谢!

You need to use the net module, so a simple TCP listener would look something like: 您需要使用net模块,因此简单的TCP侦听器看起来像:

const
   port = 1234,
   net = require('net');
server = net.createServer(function(connection) {
            connection.write("Welcome to my server"); });
server.listen(port, function() { 
            console.log("Listening..."); });

See Node.js net API for further details 有关更多详细信息,请参阅Node.js net API

Basically, node works with callbacks due to its asynchronous nature, so if you want to register a callback for a 'message' event on the socket, like that: 基本上,节点由于其异步性质而使用回调,因此如果要在套接字上注册“消息”事件的回调,则:

server.on('data', function(data) { 
    console.log('Received data');
    /* Do manipulations on the inbound data */
});

So there you define a callback to be executed on a 'data' event, so whenever you receive data on the socket, the anonymus function defined in the on() call will be executed. 因此,您定义了一个要在'data'事件上执行的回调,因此每当您在套接字上接收数据时,on()调用中定义的anonymus函数将被执行。

Node.JS is very usable for such an application (I started the exact same project a month ago and I didn't have any regrets that I chose Node.JS at all). Node.JS非常适用于这样的应用程序(我在一个月前开始完全相同的项目,我没有任何遗憾,我选择了Node.JS)。 Here an example code of a Node.JS server: 这是Node.JS服务器的示例代码:

const port = 46500;
var net = require('net');
var server = net.createServer(function(connection) {
    console.log('client connected');

    connection.on('close', function (){
        console.log('client disconnected');
     });

    connection.on('data', function (data) {
        data = data.toString();
        console.log('client sended the folowing string:'+data);
        connection.write("Response");
        console.log('Sended responst to client');
        connection.end();
        console.log('Disconnected the client.');
   });

});


server.listen(port, function () {
    console.log('server is listening');
}); 

I saw a comment of you in another post where I can't response (due to less than 50 reputation) but you can hard code the ip address of the Node.JS server inside of your c code (but make sure you have a backup option for when the ip of the server changes) or you can implement a DNS client inside of the c application if the server has a domain name. 我在另一篇文章中看到了你的评论,我无法回复(由于声誉不到50)但你可以在你的c代码中硬编码Node.JS服务器的ip地址(但要确保你有备份服务器的IP更改时的选项)或者如果服务器具有域名,您可以在c应用程序内部实现DNS客户端。

您可以使用argv传递ip,也可以使用像Hxd这样的十六进制编辑器进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM