简体   繁体   English

从TCPSocket解析响应

[英]Parsing response from TCPSocket

I'm attempting to create a ruby script that uses a TCPSocket to communicate w/ a Minecraft server and return the name, current number of players, and the maximum number of players. 我正在尝试创建一个使用TCPSocket与Minecraft服务器通信的ruby脚本,并返回名称,当前播放器数量和最大播放器数量。 So far I have the following code 到目前为止,我有以下代码

require 'socket'

class MinecraftServer
  def self.ping(ip, port = 25565)
    server = TCPSocket.new ip, port

    server.write "\xfe"

    response = []
    while line = server.gets
      response << line
    end

    server.close 

    response = response.join

    response
  end
end

puts MinecraftServer.ping('xxx.xxx.xxx.xxx')

This gives me something back like A Minecraft Server 0 20 . 这给了我诸如 A Minecraft Server 0 20类的东西。 This gives me back all of the information but in a sring when what I would like is a hash. 这给了我所有的信息,但是当我想要的是哈希值时,我却得到了回报。 How do I get rid of the odd characters and put the information into a Hash? 如何摆脱奇数字符并将信息放入哈希?

Here's an example of ruby code that does exactly this: 这是完全做到这一点的ruby代码示例:

https://gist.github.com/4poc/6281388 https://gist.github.com/4poc/6281388

It's linked from this page that describes what the protocol is: 该页面的链接描述了协议是什么:

http://wiki.vg/Server_List_Ping http://wiki.vg/Server_List_Ping

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

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