Skip to content

Instantly share code, notes, and snippets.

@champierre
Created December 3, 2014 06:46
Show Gist options
  • Save champierre/ce016eb74bf667741224 to your computer and use it in GitHub Desktop.
Save champierre/ce016eb74bf667741224 to your computer and use it in GitHub Desktop.
# TCPSocket を使うため socket ライブラリを読み込みます
require "socket"
# localhost の TCP 番号 42001 番に接続します
socket = TCPSocket.open("localhost", 42001)
# 命令の内容
#command = "broadcast \"a\""
command = "sensor-update \"a\" 1"
# 命令の文字数(=バイト数)
length = command.length
# 10進数のバイト数をバイト(16進数)に変換
bytes = [length].pack("N")
# バイトに変換した命令のバイト数と、命令をつなげたものをスクラッチに送る
socket.write(bytes + command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment