Skip to content

Instantly share code, notes, and snippets.

@oupo
Created November 19, 2017 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oupo/e949371d934588832205e22d9d1543be to your computer and use it in GitHub Desktop.
Save oupo/e949371d934588832205e22d9d1543be to your computer and use it in GitHub Desktop.
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