Last active
August 29, 2015 14:01
-
-
Save boddhisattva/f19c4d812e2c3904f968 to your computer and use it in GitHub Desktop.
Chat Application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
#this is like the chat room.. | |
port = ARGV[0] | |
ds = UDPSocket.new | |
ds.bind(nil,port) | |
$b = IO.new(2,"w+") | |
$client_id = 1 | |
$clients = [] | |
$flag = 0 | |
loop do | |
request, address = ds.recvfrom(1024) | |
response = request | |
clientaddr = address[3] | |
clientname = address[2] | |
clientport = address[1] | |
if ( $clients.empty? ) | |
$clients[$client_id-1] = clientport | |
puts "Client #{$client_id} enters the chat room " | |
$client_id += 1 | |
puts "#{$clients}" | |
else | |
unless $clients.include? clientport | |
puts "Client #{$client_id} enters the chat room " | |
$clients[$client_id-1] = clientport | |
$client_id += 1 | |
end | |
end | |
if( response.chop != "quit") | |
puts "Client #{clientport} says: #{response} " | |
else | |
puts "Client #{clientport} leaves the chat room " | |
$client_id -= 1 | |
end | |
if($client_id == 1) | |
print " Do you want to close the chat room(y/n)?: " | |
a = $b.readline | |
if ( a.chop == "y") | |
break | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment