1

I'm trying to run a computation-heavy program that creates an image on a website. Is it possible to compute in C++ and have an output stream that connects to an input stream in Node.js to display an image, or alternatively, stream is to the user and display it through JavaScript?

Also, I'm using Monte-Carlo Sampling so the stream would be binary.

user939687
  • 13
  • 2
  • 2
    you just need to connect the right sockets together. – ratchet freak Jan 22 '15 at 20:41
  • I'm not sure what you mean by that (I'm relatively new to programming but I have background in math). Could you explain this in more detail? – user939687 Jan 22 '15 at 20:42
  • 1
    @user939687 Practically every language allows you to read and write to/from the [Standard streams](http://en.wikipedia.org/wiki/Standard_streams), and practically every shell lets you pipe one program's standard output to another program's standard input. It's just a matter of both programs agreeing on the meaning of the bytes. – Doval Jan 22 '15 at 20:46
  • So, would c++ and Javascript agree on the number of bytes? Also, would using streams be faster than doing heavy array computation on just Javascript? – user939687 Jan 22 '15 at 21:31
  • 1
    It's not a matter of which language you use; just make sure both programs agree on some format for the data. As for the second question, you'll have to benchmark it. – Doval Jan 22 '15 at 22:50
  • 1
    http://stackoverflow.com/q/23747892/ – rwong Jan 23 '15 at 08:32

1 Answers1

5

Short answer: yes.

A stream in the context of software is simply a sequence of bytes. What those bytes represent is up to you: it could be ASCII text, Unicode characters, a JPEG image, serialized object, anything your heart desires. The important thing is that the producer and consumer of the stream agree on the format.

Furthermore, a stream is not specific to two programs talking to each other. You can use streams inside the same program so two modules can talk to each other in a loosely coupled way.

What you are really looking for is the idea of sockets. A socket is typically used for IPC, or inter-process communication. A high level overview of the process looks like this:

  1. Program A opens a socket to a specific location, normally an IP address and port.
  2. Program B is listening on that IP address and port, and opens the socket on the other end.
  3. Both programs open streams for that socket. Program A can write bytes to its output stream, which show up in Program B's input stream after crossing over the socket. The reverse is also true, allowing bidirectional communication.
  4. When communication is done, one side closes the socket connection.

(If you think about it, this sounds a lot like loading a web page in your browser. This is not coincidental.)

Note that the language, architecture, and physical location of the two programs are not specified. That is the beauty of sockets: they are very low level and simply do not care about those details.

Normally you will use a library to deal with sockets and streams. Some languages have such facilities built-in to the standard libraries, some do not. Regardless, most popular languages will have something available because this is a very common task asked of modern computer systems.