Search code examples
c++ruby-on-railsinteraction

Interaction between C++ and Rails applications


I have two applications: c++ service and a RoR web server (they are both running at same VPS)

I need to "send" some variables (and then do something with them) from each other. For exaple, i'm looking for something like this:

// my C++ sample
void SendMessage(string message) {
   SendTo("127.0.0.1", message);
}

void GetMessage(string message) {
   if (message == "stop")
      SendMessage("success");
}

# Ruby sample
# application_controller.rb

def stop
   @m = Messager.new
   @m.send("stop")
end

I have never used it before, and i even don't know which technology should i search and learn.


Solution

  • Ok, i have found the solution. Its TCP sockets:

    Ruby TCP server, to send messages:

    require 'socket'
    
    server = TCPServer.open(2000)
    loop {                       
      Thread.start(server.accept) do |client|
        client.puts(Time.now.ctime)
        client.puts "Closing the connection. Bye!"
        client.close               
      end
    
    }
    

    Ruby client, to accept messages:

    require 'socket'
    
    host = 'localhost'
    port = 2001 # it should be running server, or it will be connection error
    
    s = TCPSocket.open(host, port)
      while line = s.gets
        puts line.chop
      end
    s.close
    

    Now you should write TCP-server+client in another application. But you have got the idea.