Skip to content

Instantly share code, notes, and snippets.

Created November 28, 2013 03:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
This is my first stab at a simple socket client to go against my cowsay server.
require 'socket'
module CowSay
class Client
class << self
attr_accessor :host, :port
# Convert our arguments into a document that we can send to the cowsay
# Options:
# message: The message that you want the cow to say
# body: The cowsay body that you want to use
def self.say(options)
if !options[:message]
raise "ERROR: Missing message argument"
if !options[:body]
options[:body] = "default"
request <<EOF
MESSAGE #{options[:message]}
BODY #{options[:body]}
def self.request(string)
# Create a new connection for each operation
@client =, port)
# Send EOF after writing the request
# Read until EOF to get the response
end = 'localhost'
CowSay::Client.port = 4481
puts CowSay::Client.say message: 'this is cool!'
puts CowSay::Client.say message: 'This SUCKS!', body: 'beavis.zen'
puts CowSay::Client.say message: 'Moshi moshi!', body: 'hellokitty'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment