Skip to content

Instantly share code, notes, and snippets.

@oupo
Last active November 12, 2017 14:51
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/e1151ffe740bcf060f4611e3c031ce39 to your computer and use it in GitHub Desktop.
Save oupo/e1151ffe740bcf060f4611e3c031ce39 to your computer and use it in GitHub Desktop.
require "socket"
sock = TCPSocket.open("localhost", 1234)
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
while msg = STDIN.gets.chomp
sock.write(make_msg(msg))
while (str = read_msg(sock)) != "+"
p str
end
p read_msg(sock)
sock.write("+")
end
sock.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment