在 C++ 和 node.js(服务器)之间建立套接字通信的困难

Difficulties in building socket communication between c++ and node.js (server)

我尝试在 C++ 和 node.js 服务器之间建立套接字通信。 这是我的 index.js 代码:

'use strict';

const express     = require('express');
const app         = express();
const serverHttp  = require('http').Server(app); 
const io = require('socket.io')(serverHttp);

const port = 8081;

io.on('connection', function (socket) {   
    socket.on('message', function (data) {
        console.log("key received!!!" + data);
    socket.emit('server', 'hello socket io');
        console.log("sent server msg");
    });
});

serverHttp.listen(port, function() {  
    console.log("init!!!");    
});

这是我编译成功的 C++ 代码:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif

#ifdef _WIN32
#define close(sockdep) closesocket(sockdep)
#define perror(errmsg) { fprintf(stderr, "%s: %d\n", (errmsg), WSAGetLastError()); }
#endif

#define net_assert(err, errmsg) { if ((err)) { perror(errmsg); assert(!(err)); } }

#define SERVER "localhost"
#define PORT 8081
#define BLEN 128

int
main(int argc, char *argv[])
{
  struct sockaddr_in server;
  struct hostent *sp;
  int sd;
  int n;
  char buf[BLEN];
#ifdef _WIN32
  int err;
  WSADATA wsa;
  extern int write();
  
  err = WSAStartup(MAKEWORD(2,2), &wsa);  // winsock 2.2
  net_assert(err, "sample client: WSAStartup");
#endif

  sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

  memset((char *) &server, 0, sizeof(struct sockaddr_in));
  server.sin_family = AF_INET;
  server.sin_port = htons((u_short) PORT);
  sp = gethostbyname(SERVER);
  memcpy(&server.sin_addr, sp->h_addr, sp->h_length);

  connect(sd, (struct sockaddr *) &server, sizeof(struct sockaddr_in));
    
  n = recv(sd, buf, sizeof(buf), 0);
  while (n > 0) {
    write(1, buf, n);
    n = recv(sd, buf, sizeof(buf), 0);
  } 

  close(sd);

#ifdef _WIN32
  WSACleanup();
#endif
  exit(0);
}

我的环境是Mac,我用Xcode编译c++。我将带有 node index.js 的节点连接到本地主机,还编译了 运行 c++ 程序。但是,当我打开 localhost:8081 时,我在控制台中收到此错误:

GET http://localhost:8081/ 404 (Not Found)        localhost/:1 

Refused to load the image 'http://localhost:8081/favicon.ico' because it violates the following Content Security Policy directive: "default-src 'none'". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback.            localhost/:1

我对编程很陌生,如果您能指出错误之处,我将不胜感激。我无法识别 C++ 中的发送和接收代码在哪里,因为它与 JavaScript 非常不同。那么,我怎样才能建立一个简单的通信,我只想将一个数组从 C++ 发送到 index.js?

您的方法无法工作,因为 socket.io 是一种定义为与其他 socket.io 客户端或服务器一起工作的协议,与在 C++ 中创建的原始 TCP 套接字之间存在混淆,后者将与其他 TCP 套接字一起工作。

您在 C++ 代码中创建的是一个 TCP 套接字,遗憾的是它与 socket.io 使用的内容无关。 Socket.io 在可用的情况下使用 HTTP 流或 WebSocket(另一种协议)启用通信。

Here is an example of a C++ library that seems to implement the socket.io protocol. I have never used it myself, but it seems to be what you are searching for. Try using this on the C++ side, or implementing a raw tcp socket on the node.js side. This 图书馆可能是 well-suited。

我建议 this tutorial 让你开始使用直接 C++ 实现 client-server 通信的基础知识,然后你可以从那里开始了解如何在 nodejs 中创建客户端。