mirror of https://github.com/coolaj86/fizzbuzz.git
Added it347 class project
This commit is contained in:
parent
d4900dacd3
commit
daf6f48fca
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env ruby
|
||||
#http://www.tutorialspoint.com/ruby/ruby_socket_programming.htm
|
||||
|
||||
require 'socket'
|
||||
host, port = ARGV[0..1]
|
||||
unless host && port
|
||||
print("USAGE: ./TCPClient.rb host port\n")
|
||||
exit
|
||||
end
|
||||
|
||||
server = TCPSocket.open(host, port)
|
||||
|
||||
@connections = [server, STDIN]
|
||||
stop = false
|
||||
loop {
|
||||
res = select(@connections, nil, nil)
|
||||
res[0].each do |socket|
|
||||
if socket == server
|
||||
puts socket.gets
|
||||
end
|
||||
if socket == STDIN
|
||||
line = socket.gets
|
||||
server.puts line
|
||||
stop = ("quit" == line.chomp || "exit" == line.chomp || "q" == line.chomp)
|
||||
end
|
||||
break if stop
|
||||
end
|
||||
break if stop
|
||||
}
|
||||
|
||||
server.close
|
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env ruby
|
||||
require 'socket' # Get sockets from stdlib
|
||||
|
||||
MAX_CLIENTS = 20
|
||||
num_clients = 0
|
||||
|
||||
host = '0.0.0.0'
|
||||
port = 7886
|
||||
|
||||
puts "Starting server on #{host}:#{port}. Allowing #{MAX_CLIENTS} connections"
|
||||
server = TCPServer.open(host, port)
|
||||
loop { # Servers run forever
|
||||
Thread.start(server.accept) do |client|
|
||||
num_clients += 1
|
||||
if MAX_CLIENTS >= num_clients
|
||||
client.puts "(#{Time.now.ctime}) Hello Client ##{num_clients}!"
|
||||
puts "(#{Time.now.ctime}) Hello Client ##{num_clients}!"
|
||||
while !client.nil? && line = client.gets
|
||||
puts "Client says:" + line.inspect
|
||||
c_msg = case line
|
||||
when /^q\W/
|
||||
when /^quit\W/
|
||||
when /^exit\W/
|
||||
else line
|
||||
end
|
||||
if c_msg.nil?
|
||||
client.puts "Adios Muchacho! (you didn't close your connection, I'll do it for you)"
|
||||
puts "Client Left"
|
||||
client.close
|
||||
client = nil
|
||||
else
|
||||
client.puts c_msg
|
||||
end
|
||||
end
|
||||
else
|
||||
client.puts "(#{Time.now.ctime}) Too many connections (#{num_clients}). Bye!"
|
||||
end
|
||||
num_clients -= 1
|
||||
end
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env ruby
|
||||
#http://www.tutorialspoint.com/ruby/ruby_socket_programming.htm
|
||||
|
||||
require 'socket'
|
||||
host, port = ARGV[0..1]
|
||||
unless host && port
|
||||
print("USAGE: ./TCPClient.rb host port\n")
|
||||
exit
|
||||
end
|
||||
|
||||
server = TCPSocket.open(host, port)
|
||||
|
||||
@connections = [server, STDIN]
|
||||
stop = false
|
||||
loop {
|
||||
res = select(@connections, nil, nil)
|
||||
res[0].each do |socket|
|
||||
if socket == server
|
||||
puts socket.gets
|
||||
end
|
||||
if socket == STDIN
|
||||
line = socket.gets
|
||||
server.puts line
|
||||
stop = ("ADIOS" == line.chomp)
|
||||
end
|
||||
break if stop
|
||||
end
|
||||
break if stop
|
||||
}
|
||||
|
||||
server.close
|
|
@ -0,0 +1,52 @@
|
|||
#!/usr/bin/env ruby
|
||||
require 'socket' # Get sockets from stdlib
|
||||
|
||||
MAX_CLIENTS = 20
|
||||
num_clients = 0
|
||||
|
||||
host = '0.0.0.0'
|
||||
port = 7886
|
||||
|
||||
puts "Starting server on #{host}:#{port}. Allowing #{MAX_CLIENTS} connections"
|
||||
server = TCPServer.open(host, port)
|
||||
names = []
|
||||
words = []
|
||||
loop { # Servers run forever
|
||||
Thread.start(server.accept) do |client|
|
||||
num_clients += 1
|
||||
if MAX_CLIENTS >= num_clients
|
||||
client.puts "(#{Time.now.ctime}) Hello Client ##{num_clients}!"
|
||||
puts "(#{Time.now.ctime}) Hello Client ##{num_clients}!"
|
||||
while !client.nil? && line = client.gets
|
||||
puts "Client says:" + line.inspect
|
||||
c_msg = case line
|
||||
when /^GET it347\W/ then "IT Rocks!"
|
||||
when /^GET \w+/
|
||||
word = line.gsub(/^GET /, '')
|
||||
words << word
|
||||
word
|
||||
when /^NAME \w+/
|
||||
name = line.gsub(/^NAME /, '')
|
||||
names << name
|
||||
"Thank You"
|
||||
when /^GETNAMES\W/ then names.inspect
|
||||
when /^GETWORDS\W/ then words.inspect
|
||||
when /^ADIOS\W/
|
||||
else "400: Bad Request"
|
||||
end
|
||||
if c_msg.nil?
|
||||
client.puts "Adios Muchacho! (you didn't close your connection, I'll do it for you)"
|
||||
puts "Client Left"
|
||||
client.close
|
||||
client = nil
|
||||
else
|
||||
puts "Sent client:" + c_msg
|
||||
client.puts c_msg
|
||||
end
|
||||
end
|
||||
else
|
||||
client.puts "(#{Time.now.ctime}) Too many connections (#{num_clients}). Bye!"
|
||||
end
|
||||
num_clients -= 1
|
||||
end
|
||||
}
|
Loading…
Reference in New Issue