title: How to write a JSON File over TCP Socket in express to C++ program
body: <p>This is in connection with the question <a href="https://stackoverflow.com/questions/65212493/about-server-client-architecture-in-express-js">About server-client architecture in express.js</a></p>
  <p>I am currently writing the client part of the program which is supposed to be in express.jsvar</p>
  <pre><code>var request = require('request'),
      express = require('express'),
      path    = require('path');
  
  const net = require('net');
  
  var app = express();
  
  // for Express 4.16 above use this instead of
  // using body-Parser: app.use(bodyParser.json())
  app.use(express.json());
  
  
  // configuration setting
  const PORT1 = 5000;
  const port2 = 5001;
  const PORT3 = 1337;
  const HOST = '127.0.0.1';
  
  app.set('port', (process.env.PORT || PORT1));
  
  var client = new net.Socket();
  var id = 0;
  
  // start a tcp connection to connect to server
  client.connect(PORT3, HOST, function() {
      console.log('SEND PORT CONNECTED TO: ' + HOST + ':' + PORT3);
  });
  
  // using Github for testing web hook
  app.post('/getGithub', function(req, res) 
  {
      // tester code
      //var data = &quot;20a980dc00b413dc3d0000a3&quot;;
      //var data = &quot;e36ace0044ed3d0cc3810000&quot;;
      var data = req.body;
      console.log(data);
          
      client.write('Hello, server! Love, Client.' + id + &quot; &quot;);
      client.write(data);
      res.json(data);
  
  });
  
  // http server location
  var server = app.listen(app.get('port'), function() {
      var host   = server.address().address
      var portid = server.address().port
  
      console.log('App listening at http://%s:%s', host, portid)
      console.log(&quot;App listening on port &quot; + app.get('port'));
  });
  
  // error handler
  app.use(function (err, req, res, next) {
      res.status(400).send(err.message)
  });
  </code></pre>
  <p>I have some questions here</p>
  <ol>
  <li>How do I write a JSON object over to my server application (C++). I know of JSON.stringify(obj) will return string and could be written over?</li>
  <li>How do I write my C++ program over the other end? I have written some C++ program but it seem there is some problem while using another express program it seem ok. when I run the C++ program, the client program in express keeps on giving me ECONNECT refused</li>
  </ol>
  <p>My C++ program</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;jsoncpp/json/json.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  int main()
  {
      /* all previous code until
      printf(&quot;Reading from client\n&quot;); */
  
     int listenfd = 0, connfd = 0;    //related with the server
     int MAX_SIZE = 158;
     struct sockaddr_in serv_addr;
  
     //json_object * jobj;
     char buf[MAX_SIZE], i;
  
     memset(&amp;buf, '0', sizeof(buf));
     listenfd = socket(AF_INET, SOCK_STREAM, 0);
  
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
     serv_addr.sin_port = htons(1337); 
  
     bind(listenfd, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr));
     printf(&quot;binding\n&quot;);
  
     listen(listenfd, 5);
     printf(&quot;listening\n&quot;);
     connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
  
     printf(&quot;Reading from client\n&quot;);
      ssize_t r;
  
      for (;;)
      {
          r = read(connfd, buf, MAX_SIZE);
  
          if (r == -1)
          {
              perror(&quot;read&quot;);
              return EXIT_FAILURE;
          }
          if (r == 0)
              break;
  
          printf(&quot;READ: %s\n&quot;, buf);
      }
  
      return EXIT_SUCCESS;
  }
  </code></pre>
  <p>The error is:</p>
  <pre><code>Error: connect ECONNREFUSED 127.0.0.1:1337
      at Object._errnoException (util.js:1022:11)
      at _exceptionWithHostPort (util.js:1044:20)
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
  </code></pre>