サーバに接続したら問題が出されるようなサーバプログラムを書いてみようと思ったのですが、うまく行きません。
サーバ側でCtrl+Cをした時に問題が送られてきます。
クライアントが接続してきた時にサーバ側からメッセージを送ることはできるんでしょうか?
Ruby
1#client_side 2require 'socket' 3 4addr = "192.168.33.11" 5port = 20000 6 7sock = TCPSocket.open(addr, port) 8 9while true 10 print sock.gets 11 print sock.gets 12 sock.write gets 13end
Ruby
1#server_side 2require 'socket' 3 4port = 20000 5 6gate = TCPServer.open(port) 7sock = gate.accept 8loser = false 9 105.times do |i| 11 a = rand(100)+1 12 b = rand(100)+1 13 14 sock.write "\##{i+1}" 15 sock.write "#{a}+#{b} = ?" 16 if (a+b) == sock.gets.to_i then 17 sock.write "collect!" 18 else 19 sock.write "You are loser!\nSee you" 20 loser = true 21 break 22 end 23end 24 25if loser then 26else 27 sock.write "Excellent!\nYou are Winner!!!" 28end 29 30sock.close 31gate.close 32
ちなみにIPアドレスはVagrantのものです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/08 13:55