简体   繁体   English

Ruby TCPSocket请求未获得响应

[英]Ruby TCPSocket request not getting response

I have been reading up on TCPSockets over at TutorialsPoint and I need a simple client to make requests but I do not confused why there is no response. 我已经在TutorialsPoint上阅读了TCPSockets,我需要一个简单的客户端来发出请求,但是我不感到困惑,为什么没有响应。

Website API Login 网站API登录

The login command accepts a JSON object as argument followed by the EOT character for a response. login命令接受一个JSON对象作为参数,后跟EOT字符作为响应。

From the website's API 从网站的API

This is what I have 这就是我所拥有的

def connect
    url = "someurl.com"
    port = 19534 
    loginRequest = 'login {"protocol":1,"client":"testruby","clientver":0.1}' +"\04"

    s = TCPSocket.open(url,port)

    s.puts(loginRequest)
    while line = s.gets
        puts line.chop
    end
puts "end"
s.close
end
connect

My attempts 我的尝试

1) I run the ruby code in terminal but my connection to the server is immediately closed. 1)我在终端中运行ruby代码,但与服务器的连接立即关闭。

I'm totally clueless and would appreciate some directions on this. 我完全一无所知,不胜感激。

It seems that the server is using raw TCP/IP, using the 0x04 character to signify an "End of Message". 似乎服务器使用的是原始TCP / IP,使用0x04字符表示“消息结尾”。

Your code seems to prefer blocking IO (although it probably wouldn't be my first choice), so allow me to suggest a blocking API adjustment: 您的代码似乎更喜欢阻塞IO(尽管这可能不是我的首选),因此请允许我提出一个阻塞API调整建议:

require 'socket'

module VNDB
   public

   def send_message message
      raise "Not connected" unless @s
      @s.write (message + "\x04")
   end

   def get_message
      raise "Not connected" unless @s
      message = ''
      message << @s.getc until message[-1] == "\x04"
      message
   end

   def connect
       url = "someurl.com"
       port = 19534 
       loginRequest = 'login {"protocol":1,"client":"testruby","clientver":0.1}' +"\04"

       @s = TCPSocket.open(url,port)

       send_message loginRequest
       puts get_message
       puts "end"
       @s.close
       @s = nil
   end
   extend self
end

VNDB.connect

PS 聚苯乙烯

  1. I couldn't test the code, because "someurl.com" isn't really the address, I guess. 我无法测试代码,因为“ someurl.com”实际上并不是地址。

  2. The database server is NOT using Websockets (which is a name of a protocol), so please consider editing your question and removing that tag. 数据库服务器未使用Websockets(这是协议的名称),因此请考虑编辑问题并删除该标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM