NodeJS Server and C++ Client Not Communicating

Hi C++ Community!,

I am posting here after spending a few hours with my client and server application.

My server is written in NodeJS (posted at the bottom along with the C++ client)

I am having a issue getting the Client to accept and receive the array of objects from the NodeJs server and i'm not sure how to work my way though this one.

Ideally I want the server running passing the array of object(s) to my client.
I am having no issues with the NodeJs server but am not receiving the array of objects that i'm attempting to pass through the socket to my client, i'm receiving a exit code of 0 immediately after running my C++ file

If someone could kindly help me out and tell me what this is not working it would be much appreciated! thank you.


** I should mention that the expected output should be 4 float objects for example in my NodeJs server the output of console.log is example:
[ 0.100300,0.100900,0.100100,0.10000]
Which I expect to obtain in the C++ output as well

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

NodeJS Server

const _ = require('lodash');
const Bluetooth = require('./scan.js');
const express = require('express'),
  app = express(),
  server = require('net').createServer(app);
//server = require('http').createServer(app);
var io = require('socket.io')(server);
var geolocation = require('geolocation')
// libs
const Ganglion = require('openbci-ganglion').Ganglion;
const ganglion = new Ganglion();



server.listen(4000, () => {
  console.log('Listening For  Client Connection');
});

io.on('connection', socket => {
  console.log("\n Client has connected to our server");
  ganglion.searchStart();
  console.log("I have finished searching");
});

/*=============================================================================
This function will take in the RAW EEG, EMG, ECG data and send the Array with 4 objects inside ( Microvolts) to the 
read.js class 

=============================================================================*/
ganglion.once('ganglionFound', (peripheral) => {
  // Stop searching for BLE devices once a ganglion is found.
  ganglion.searchStop();

  /*
  Use throttle to control the sample rate
  */
  
  ganglion.on('sample', _.throttle(sample => {
    /** Work with sample */

    const transferdata = [];
    for (let i = 0; i < ganglion.numberOfChannels(); i++) {
      GanglionObjects = sample.channelData[i].toFixed(8);
      transferdata.push(GanglionObjects);

    }

    pass_data(transferdata);
  }, 200, { leading: true }))




  ganglion.once('ready', () => {
    ganglion.streamStart();
  });

  ganglion.connect(peripheral);

});

/*
Function to actually pass the data from the socket to the C++ client socket
*/
function pass_data(transferdata) {
  console.log(transferdata)
 io.emit("arraytransfer", transferdata);

}





C++ CLIENT SIDE


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "Connect.hpp"



#define PORT_NUMBER (4100) // port number where to port in application
#define LENGTH (512)


int clientSocket;
char buffer[100];
struct sockaddr_in serverAddr;
socklen_t addr_size;

using namespace:: std;

void Connection::connect_client(){
    
    /*---- Create the socket. The three arguments are: ----*/
    /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
    clientSocket = socket(AF_UNIX, SOCK_STREAM, 0);
    
    /*---- Configure settings of the server address struct ----*/
    /* Address family = Internet */
    serverAddr.sin_family = AF_UNIX;
  
    /* Set port number, using htons function to use proper byte order */
    serverAddr.sin_port = htons(PORT_NUMBER);
    
    /* Set IP address to localhost */
    serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    
    /* Set all bits of the padding field to 0 */
    memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

    /*---- Connect the socket to the server using the address struct ----*/
    addr_size = sizeof serverAddr;
 
    connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
    
    /*---- Read the message from the server into the buffer ----*/

    
     recv(clientSocket, buffer, 100, 0);

    cout<<"This is whats I have" <<buffer;
    
    /*---- Print the received message ----*/

    
    cout<<" I have executed";
    
   // close(clientSocket);
    
    
    
}




C++ HEADER FILE CLIENT






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ifndef Connect_hpp
#define Connect_hpp
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>


#include <string>
#include <cstdlib>
#include <string>
#include <iostream>

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <resolv.h>



#endif /* Connect_hpp */

using namespace :: std;

class Connection{
    
public:
    
    
    
   void  connect_client();
    
    
};




C++ MAIN CLIENT


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "Connect.hpp"
int main(int argc, const char * argv[]) {

    Connection class_connection;
    
    class_connection.connect_client();
    
    
    
    
    
    
    
    return 0;
}
Last edited on
Unless you've simply omitted it, it would seem your C++ code doesn't have a main() function. No idea how the linker could let that slide, but here we are.
@helios


I have now updated the C++ Client code with the complete files.
Apologies I should have been more clear describing the C++ file. The function is called from the main what you see here is just the programs *.cpp file with the function and code in question, this code is executing properly there are no issues there but, the actual intended logic is not performing the task of connecting to the server.
Last edited on
You need to check the results of connect(...) and recv(...) to see whether there is an error. See:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms737625(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms740121(v=vs.85).aspx

If everything is ok and you receive actual data (recv(...) returns > 0) the issue is the format of the data. Do you know the format?
Topic archived. No new replies allowed.