Alex Young
8/27/2006 4:13:00 PM
Curtis Zimmerman wrote:
> I am running ruby 1.8.6 on WinXP.
Really? What does ruby -v say?
> 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 eof?
I think that's what you're missing. Without it, broadcast_string and
connection_accept end up being interpreted as nested methods within
runme, which is why you get an unrecognized method error.
--
Alex