C
Curtis Zimmerman
I am running ruby 1.8.6 on WinXP.
I get the following error when I try to run this code:
httpserv.rb:22:in `runme': undefined local variable or method
`connection_accept' for #<ServerBrutus:0x282af80> (NameError)
Here's the code (taken from a tutorial site by IBM):
--==--
#!/usr/bin/ruby
require 'socket'
class ServerBrutus
def initialize(servAddr, servPort)
@servPort = servPort
@servAddr = servAddr
@socketThreads = Array::new
@cx = TCPServer.new("", servPort)
printf("Server started on port: %d\n", servPort)
@socketThreads.push(@cx)
end # initialize
def runme
while 1
conChk = select(@socketThreads, nil, nil, nil)
if conChk != nil then
for socket in conChk[0]
if socket == @cx then
connection_accept
else
if socket.eof? then
str = sprintf("Client left
%s%s\n",socket.peeraddr[2],socket.peeraddr[1])
broadcast_string(str, socket)
socket.close
@socketThreads.delete(socket)
else
str = sprintf("[%s|%s]:
%s",socket.peeraddr[2],socket.peeraddr[1],socket.gets())
broadcast_string(str, socket)
end # if cx
end # for sock
end # if conChk
end # while infinite
end # runme
def broadcast_string(str, omit_sock)
@socketThreads.each do |clientSocket|
if clientSocket != @cx && clientSocket != omit_sock
clientSocket.write(str)
end
end
end # broadcast_string
def connection_accept
newSocket = @cx.accept
@socketThreads.push(newSocket)
newSocket.write("You're connected to the server!\n")
str = sprintf("Client joined
%s%s\n",newSocket.peeraddr[2],newSocket.peeraddr[1])
broadcast_string(str, newSocket)
end # connection_accept
end # class ServerBrutus
servAddr = 'localhost'
servPort = 180
myChatServer = ServerBrutus.new(servAddr, servPort)
myChatServer.runme
end
--==--
What am I doing wrong? I'm using FreeRIDE, but it poops the same error
on the command prompt. If I do not include the last 'end', it generates
a syntax error on the last line ('myChatServer.runme').
Any help please? Thanks ahead of time!
I get the following error when I try to run this code:
httpserv.rb:22:in `runme': undefined local variable or method
`connection_accept' for #<ServerBrutus:0x282af80> (NameError)
Here's the code (taken from a tutorial site by IBM):
--==--
#!/usr/bin/ruby
require 'socket'
class ServerBrutus
def initialize(servAddr, servPort)
@servPort = servPort
@servAddr = servAddr
@socketThreads = Array::new
@cx = TCPServer.new("", servPort)
printf("Server started on port: %d\n", servPort)
@socketThreads.push(@cx)
end # initialize
def runme
while 1
conChk = select(@socketThreads, nil, nil, nil)
if conChk != nil then
for socket in conChk[0]
if socket == @cx then
connection_accept
else
if socket.eof? then
str = sprintf("Client left
%s%s\n",socket.peeraddr[2],socket.peeraddr[1])
broadcast_string(str, socket)
socket.close
@socketThreads.delete(socket)
else
str = sprintf("[%s|%s]:
%s",socket.peeraddr[2],socket.peeraddr[1],socket.gets())
broadcast_string(str, socket)
end # if cx
end # for sock
end # if conChk
end # while infinite
end # runme
def broadcast_string(str, omit_sock)
@socketThreads.each do |clientSocket|
if clientSocket != @cx && clientSocket != omit_sock
clientSocket.write(str)
end
end
end # broadcast_string
def connection_accept
newSocket = @cx.accept
@socketThreads.push(newSocket)
newSocket.write("You're connected to the server!\n")
str = sprintf("Client joined
%s%s\n",newSocket.peeraddr[2],newSocket.peeraddr[1])
broadcast_string(str, newSocket)
end # connection_accept
end # class ServerBrutus
servAddr = 'localhost'
servPort = 180
myChatServer = ServerBrutus.new(servAddr, servPort)
myChatServer.runme
end
--==--
What am I doing wrong? I'm using FreeRIDE, but it poops the same error
on the command prompt. If I do not include the last 'end', it generates
a syntax error on the last line ('myChatServer.runme').
Any help please? Thanks ahead of time!