C ++和Rails应用程序之间的交互

时间:2010-03-14 17:40:03

标签: c++ ruby-on-rails interaction

我有两个应用程序:c ++服务和一个RoR Web服务器(它们都在相同的VPS下运行)

我需要互相“发送”一些变量(然后用它们做一些事情)。对于exaple,我正在寻找这样的东西:

// 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

我之前从未使用过它,我甚至不知道应该搜索和学习哪种技术。

1 个答案:

答案 0 :(得分:1)

好的,我找到了解决方案。它的TCP套接字:

Ruby TCP服务器,用于发送消息:

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客户端,接受消息:

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

现在你应该在另一个应用程序中编写TCP-server + client。但是你有了这个想法。