I try to build a socket communication between c++ and a node.js server. This is my index.js code:
'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!!!");
});
and this is my c++ code which compiles successfully:
#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);
}
My environment is Mac, and I am using Xcode to compile the c++. I connect the node with node index.js
to the localhost and also compiled and run the c++ program. However, when I open localhost:8081, I receive this error in the console:
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
I am very new to programming, and I would appreciate it if you clarify what is wrong. I cannot recognize where is the send and receive codes in the c++, as it is very different from JavaScript. So, how can I just build a simple communication where I only want to send an array from the c++ to the index.js?
Your approach can not work because of the confusion made between socket.io , which is a protocol defined to work with other socket.io clients or servers, and the raw TCP socket created in C++, which will work with other TCP sockets.
What you are creating in the C++ code is a TCP socket, which sadly has nothing to do with what socket.io uses. Socket.io enables communication using HTTP streaming or WebSocket (another protocol) wherever available.
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 library might be well-suited for that.
I recommend this tutorial to get you started on the basics of implementing a client-server communication in straight C++, then you can go from there to figure how to create the client in nodejs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.