简体   繁体   中英

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. 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 . 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:

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

It's linked from this page that describes what the protocol is:

http://wiki.vg/Server_List_Ping

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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