[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

What am I doing wrong? -Chat server code-

Curtis Zimmerman

8/27/2006 4:09:00 AM

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! :)

--
Posted via http://www.ruby-....

5 Answers

Paul Lutus

8/27/2006 4:38:00 AM

0

Curtis Zimmerman wrote:

> 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):

/ ... snipped code listing

The code you listed has a rather obvious syntax error. Was it really copied
directly from the IBM site, or did you transcribe it?

> What am I doing wrong?

The code is broken. Try indenting the code using a beautifier, and you will
discover that the carefully worked out indentations are wrong, they are
marked incorrectly as well, and the code is missing one "end" statement
within "runme".

The remedy is to avoid commenting an "end" statement until you have actually
matched it up with its corresponding beginning statement, by following that
level of indentation to its origin.

Look at this:

def runme
while true
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 ... # snipped
broadcast_string(str, socket)
socket.close
socketThreads.delete(socket)
else
str = sprintf("[%s|%s] ... #snipped
broadcast_string(str, socket)
end # if socket.eof
end # if socket == @cx
end # for socket in conChk[0]
end # if conChk != nil then
end # while true
end # runme

See the difference? This is accurately indented, and the error popped out
right away.

Also, use "true" rather than "1", for good form and portability.

> If I do not include the last 'end', it generates
> a syntax error on the last line ('myChatServer.runme').

Don't apply patches without understanding why the problem came up in the
first place. That only adds to the confusion.

Again, is your code really copied directly, unchanged, from the IBM site?

--
Paul Lutus
http://www.ara...

Curtis Zimmerman

8/27/2006 5:26:00 AM

0


First of all, thanks for helping me out. I had a problem that I
personally couldn't solve, and needed help. Second, I hadn't touched
Ruby before about six hours ago, so this is completely new territory.
Despite the fact that it was a miserably simple mistake, it didn't pop
right out at me. Third, I'm not sure where you're going with your
insistence about querying the origins of this code? Would I imply that I
(me, myself) was doing something wrong if the code had been taken
*directly* from an IBM site? I didn't bill myself as being in any way
associated with IBM. I thought this was "rather obvious" that it was
"transcribed", modified code, that it wasn't taken directly from an IBM
site.

> The code you listed has a rather obvious syntax error. Was it really
> copied
> directly from the IBM site, or did you transcribe it?

> Again, is your code really copied directly, unchanged, from the IBM
> site?

--
Posted via http://www.ruby-....

Paul Lutus

8/27/2006 7:15:00 AM

0

Curtis Zimmerman wrote:

>
> First of all, thanks for helping me out. I had a problem that I
> personally couldn't solve, and needed help. Second, I hadn't touched
> Ruby before about six hours ago, so this is completely new territory.
> Despite the fact that it was a miserably simple mistake, it didn't pop
> right out at me. Third, I'm not sure where you're going with your
> insistence about querying the origins of this code?

I wanted to know whether the IBM site contained code with an error in it, or
the error came in after the code was downloaded.

> Would I imply that I
> (me, myself) was doing something wrong if the code had been taken
> *directly* from an IBM site?

I don't care about that. I only wanted to know where the error originated.
If the code on the IBM site was in error, I would want to let them know.
That way, tomorrow, we won't be answering the same query about the same
code.

> I didn't bill myself as being in any way
> associated with IBM. I thought this was "rather obvious" that it was
> "transcribed", modified code, that it wasn't taken directly from an IBM
> site.

Here is what you said in your original message:

> Here's the code (taken from a tutorial site by IBM):

With no other comments, certainly not the word "transcribed". This strongly
suggests that the code was "taken from a tutorial site by IBM." In fact, it
is difficult to give it any other interpretation.

Please don't take this the wrong way. Errors are common in computer
programming. I just wanted to know where the error originated.

--
Paul Lutus
http://www.ara...

Alex Young

8/27/2006 4:13:00 PM

0

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


Hell Stomper

5/6/2013 5:31:00 AM

0

On May 6, 12:57 am, AlleyCat <al...@aohell.com> wrote:
> On Sun, 5 May 2013 23:06:27 -0400, In article <Y6Fht.33651$Ht.1296
> @newsfe24.iad>, Sno...@Teranews.com says...
>
> >  that's right, and he's gonna fix yall hillbillies next
>
> Why are you such a knee-pad wearing cheerleader, for the WORST President
> EVER?  Are you a gay 14 year old white boy that would love to suck
> Obama's black/white dick?

Was there ever any doubt? Birdy is a buss station boy lover.