Created
November 19, 2017 08:07
-
-
Save oupo/e949371d934588832205e22d9d1543be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "socket" | |
HOST = "localhost" | |
PORT = 55555 | |
def main | |
sock = TCPSocket.open(HOST, PORT) | |
sock.write("+") | |
sock.write(make_msg("g")) | |
while (str = read_msg(sock)) != "+" | |
end | |
p read_msg(sock) | |
sock.write("+") | |
sock.close | |
end | |
def sum(str) | |
str.bytes.inject(0, :+) % 256 | |
end | |
def make_msg(str) | |
"$" + str + "#" + sum(str).to_s(16) | |
end | |
def read_msg(sock) | |
c = sock.getc | |
if c == "$" | |
str = "" | |
while (c = sock.getc) != "#" | |
str << c | |
end | |
# checksum のチェックは省略 | |
sock.getc | |
sock.getc | |
ret = str | |
else | |
ret = c | |
end | |
end | |
main() if $0 == __FILE__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment