Ruby on Rails : How to use TCP socket to connect with GPS Device

淺唱寂寞╮ 提交于 2020-02-04 01:16:09

问题


ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux], Rails 4.2.4

I am using cloud9 IDE and webrick server. My project is to track GPS in real time.

I want to use TCP connection to communicate with GPS tracking device which requires to transmit data in hexa decimal format like 0x01. i put the following code in controller.

GPS device protocol is confidential. so i am unable to provide that here.

my server is running on port 8080. the device sending data to this port.

But i don't know how to write code to establish connection between our server and device and how to transmit and view the data sent by device to server.

i found something like this on net . i don't know whether is it correct or not.

 require 'socket'
    server = TCPServer.new ("127.0.0.1",8080)
    loop do
      Thread.start(server.accept) do |client|
        client.puts "Hello !"
        client.puts "Time is #{Time.now}"
        client.close
      end
    end

But it causes following Error.

Address already in use - bind(2) for nil port 8080

if i change it to another port it shows

Bad Gatewat Error.502

Kindly help me. Thanks in advance.

来源:https://stackoverflow.com/questions/39284765/ruby-on-rails-how-to-use-tcp-socket-to-connect-with-gps-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!