saltycracker

SimpleTCPWithThreadToShutDown2

Jun 11th, 2020
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.79 KB | None | 0 0
  1. #! /usr/bin/env ruby
  2. #encoding utf-8
  3.  
  4. $LOAD_PATH<<"."
  5.  
  6. require 'io/console'
  7. require 'socket'
  8. require 'getoptlong'
  9.  
  10. def check_commandline
  11.  
  12.   GetoptLong.new(
  13.     ['--Port', '--P', GetoptLong::OPTIONAL_ARGUMENT],
  14.     ['--Addr', '--A', GetoptLong::OPTIONAL_ARGUMENT],
  15.     ['--Help', '--H', GetoptLong::OPTIONAL_ARGUMENT]
  16.   ).yield_self(
  17.     &->(g){
  18.       port = nil
  19.       addr = nil
  20.       g.each {
  21.         |o, a|
  22.         case o
  23.         when '--Port' || '--P'
  24.           begin
  25.             port = Integer(a)
  26.           rescue
  27.             puts "Port number must be an integer: (#{a})"
  28.             exit(1)
  29.           end
  30.         when '--Addr' || '--A'
  31.           addr = a
  32.         when '--Help' || '--H'
  33.           puts "Must call #{$PROGRAM_NAME} with --Port|P=1234 --Addr|A=127.0.0.1"
  34.           exit(0)
  35.         end              
  36.       }
  37.       [port, addr]
  38.     }
  39.   )
  40.  
  41. end
  42.  
  43. def box_string(str)
  44.   line = "|--"+'-' * str.length+"--|"
  45.   puts line
  46.   puts "|->"+str+"<-|"
  47.   puts line
  48. end
  49.  
  50. def quit_server
  51.   puts "Hit 'q' to shutdown server and exit"
  52.   loop do
  53.     char = STDIN.getch
  54.     if char == 'q'
  55.       puts "Shutting down server... Exiting..."
  56.       exit(0)
  57.     end
  58.   end
  59. end
  60.  
  61. if __FILE__ == $0
  62.  
  63.   port, addr = check_commandline
  64.  
  65.   if port.nil? || addr.nil?
  66.     puts("Must call #{$PROGRAM_NAME} with --Port|P=1234 --Addr|A=127.0.0.1")
  67.     exit(1)
  68.   end
  69.  
  70.   box_string("Server on: #{addr}:(#{port})")
  71.  
  72.   print "Start server(y/n)? "
  73.   STDOUT.flush
  74.  
  75.   unless gets.chomp =~ /^y$/
  76.     puts "Exiting #{$PROGRAM_NAME}"
  77.     exit(0)
  78.   end
  79.  
  80.   server = TCPServer.new(addr, port)
  81.  
  82.   box_string("Server active on: #{addr}:(#{port})")
  83.   puts "Server running..."
  84.  
  85.   Thread.new{ quit_server }
  86.  
  87.   loop do
  88.     client = server.accept
  89.     user = client.gets.chomp
  90.     print "Message from #{user}\r\n"
  91.     client<<"Here's the current date/time for #{user}: "<<(Time.now.ctime)<<"\n"
  92.     client.close
  93.   end
  94.  
  95.   server.close
  96.  
  97. end
  98.  
  99. ###---Client follows
  100.  
  101. #! /usr/bin/env ruby
  102. #encoding utf-8
  103.  
  104. $LOAD_PATH<<"."
  105.  
  106. require 'io/console'
  107. require 'socket'
  108. require 'getoptlong'
  109.  
  110. def check_commandline
  111.  
  112.   GetoptLong.new(
  113.     ['--Port', '--P', GetoptLong::OPTIONAL_ARGUMENT],
  114.     ['--Addr', '--A', GetoptLong::OPTIONAL_ARGUMENT],
  115.     ['--User', '--U', GetoptLong::OPTIONAL_ARGUMENT],
  116.     ['--Help', '--H', GetoptLong::OPTIONAL_ARGUMENT]
  117.   ).yield_self(
  118.     &->(g){
  119.       port = nil
  120.       addr = nil
  121.       user = nil
  122.       g.each {
  123.         |o, a|
  124.         case o
  125.         when '--Port' || '--P'
  126.           begin
  127.             port = Integer(a)
  128.           rescue
  129.             puts "Port number must be an integer: (#{a})"
  130.             exit(1)
  131.           end
  132.         when '--Addr' || '--A'
  133.           addr = a
  134.         when '--User' || '--U'
  135.           user = a
  136.         when '--Help' || '--H'
  137.           puts "Must call #{$PROGRAM_NAME} with --Port|P=1234 --Addr|A=127.0.0.1 --User|U=UserName"
  138.           exit(0)
  139.         end              
  140.       }
  141.       [port, addr, user]
  142.     }
  143.   )
  144.  
  145. end
  146.  
  147. def box_string(str)
  148.   line = "|--"+'-' * str.length+"--|"
  149.   puts line
  150.   puts "|->"+str+"<-|"
  151.   puts line
  152. end
  153.  
  154. if __FILE__ == $0
  155.  
  156.   port, addr, user = check_commandline
  157.  
  158.   if port.nil? || addr.nil? || addr.nil?
  159.     puts("Must call #{$PROGRAM_NAME} with --Port|P=1234 --Addr|A=127.0.0.1 --User|U=UserName")
  160.     exit(1)
  161.   end
  162.  
  163.   box_string("Client(#{user}) on: #{addr}:(#{port})")
  164.  
  165.   print "Start client(y/n)? "
  166.   STDOUT.flush
  167.  
  168.   unless gets.chomp =~ /^y$/
  169.     puts "Exiting #{$PROGRAM_NAME}"
  170.     exit(0)
  171.   end
  172.  
  173.   begin
  174.     client = TCPSocket.new(addr, port)
  175.  
  176.     box_string("Client(#{user}): #{addr}:(#{port})")
  177.    
  178.     client<<user<<"\n"
  179.  
  180.     client.each{|l| puts l}
  181.    
  182.     client.close;
  183.   rescue
  184.     puts "Client connection refused on #{addr}:(#{port})"
  185.   end
  186.  
  187. end
Add Comment
Please, Sign In to add comment