From daf6f48fca4178e8f092a83a46cc51356fe3b5f1 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 26 Jun 2010 11:30:19 -0600 Subject: [PATCH] Added it347 class project --- EchoTCP/EchoClient.rb | 31 +++++++++++++++++++++++++ EchoTCP/EchoServer.rb | 40 ++++++++++++++++++++++++++++++++ EchoTCP/IT347Client.rb | 31 +++++++++++++++++++++++++ EchoTCP/IT347Server.rb | 52 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100755 EchoTCP/EchoClient.rb create mode 100755 EchoTCP/EchoServer.rb create mode 100755 EchoTCP/IT347Client.rb create mode 100755 EchoTCP/IT347Server.rb diff --git a/EchoTCP/EchoClient.rb b/EchoTCP/EchoClient.rb new file mode 100755 index 0000000..6b27296 --- /dev/null +++ b/EchoTCP/EchoClient.rb @@ -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 diff --git a/EchoTCP/EchoServer.rb b/EchoTCP/EchoServer.rb new file mode 100755 index 0000000..d127a0c --- /dev/null +++ b/EchoTCP/EchoServer.rb @@ -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 +} diff --git a/EchoTCP/IT347Client.rb b/EchoTCP/IT347Client.rb new file mode 100755 index 0000000..87cd3a6 --- /dev/null +++ b/EchoTCP/IT347Client.rb @@ -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 diff --git a/EchoTCP/IT347Server.rb b/EchoTCP/IT347Server.rb new file mode 100755 index 0000000..d103701 --- /dev/null +++ b/EchoTCP/IT347Server.rb @@ -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 +}