[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby-newbie

Terje Haarstad

2/12/2009 12:29:00 PM

Hi!
Ive been trying for a couple of days to get connected to vortex wargame
with the help of ruby. Since this language is kinda new to me i dont get
what im doing wrong.
The mission is to get 4 unsigned integer, sum them together and send
the answer to server, but the output i get is:
-------------------------------------------------
639805687
passordet er îYY�{g6ûbzzzt, wrong
Koplet fra vortex.labs.pulltheplug.org
>Exit code: 0
-------------------------------------------------
Okey, i get the wrong code but why the chars just before?

require 'socket'
host = 'vortex.labs.pulltheplug.org'
port = 5842

s = TCPSocket.new(host, port)
buff = s.recvfrom(16)[0].chomp.unpack("I*")
sum = buff[0].to_i + buff[1].to_i + buff[2].to_i + buff[3].to_i
svar = [sum].pack("I")
s.puts (svar)
passwd = s.recvfrom(128)
puts "passordet er #{passwd}"
s.close
puts "Koplet fra #{host}"
--
Posted via http://www.ruby-....

9 Answers

Brian Candler

2/12/2009 1:10:00 PM

0

Terje Haarstad wrote:
> require 'socket'
> host = 'vortex.labs.pulltheplug.org'
> port = 5842
>
> s = TCPSocket.new(host, port)
> buff = s.recvfrom(16)[0].chomp.unpack("I*")

This is strange code. If you want to read 16 bytes, just do s.read(16).

Then to see what buff is at this point you should add:

puts "buff = #{buff.inspect}"

#or to see in hex:

puts buff.unpack("H*").first

> sum = buff[0].to_i + buff[1].to_i + buff[2].to_i + buff[3].to_i

puts "sum = #{sum.inspect}"

> svar = [sum].pack("I")

puts "svar = #{svar.inspect}"

> s.puts (svar)

Beware that puts() may add a newline; if that's not wanted, use write()
or print() instead.

> passwd = s.recvfrom(128)

recvfrom is normally only used for UDP sockets. s.read(128) is probably
want you want, as long as you know the server will definitely return 128
bytes and/or close the socket. Or maybe you want s.gets to get upto a
newline.

s.readpartial(128) will wait for at least 1 byte and then return however
many are available; but there's no guarantee that the server will send
its entire response in one chunk.

> puts "passordet er #{passwd}"
> s.close
> puts "Koplet fra #{host}"

...

> Okey, i get the wrong code but why the chars just before?

Well, it looks like that's what the server returned to you. If you have
any questions about the protocol, you'll have to talk to the people who
implement or design the protocol, or look at the protocol documentation.

However when I try this using telnet, I see ~16 bytes of test data, then
when I send my response I just get "bzzzt, wrong". So I think your
problem is using recvfrom when you should be using read.
--
Posted via http://www.ruby-....

Brian Candler

2/12/2009 1:15:00 PM

0

require 'socket'
host = 'vortex.labs.pulltheplug.org'
port = 5842

s = TCPSocket.new(host, port)
buff = s.read(16).unpack("I*")
sum = buff[0] + buff[1] + buff[2] + buff[3]
svar = [sum & 0xffffffff].pack("I")
s.write(svar)
passwd = s.read(128)
puts "passordet er #{passwd}"
s.close
puts "Koplet fra #{host}"
--
Posted via http://www.ruby-....

Terje Haarstad

2/12/2009 3:59:00 PM

0

thanks for reply mate!
it made a few things clearer although i
got a few more questions.

Brian Candler wrote:
> require 'socket'
> host = 'vortex.labs.pulltheplug.org'
> port = 5842
>
> s = TCPSocket.new(host, port)
> buff = s.read(16).unpack("I*")
> sum = buff[0] + buff[1] + buff[2] + buff[3]
> svar = [sum & 0xffffffff].pack("I")

Why do u have to use sum & 0xffffffff? an error occour
(`pack': bignum too big to convert into `unsigned long' (RangeError)
from vortex0.rb:16) if i DONT use 0xfffffffff while use s.gets
but why do they conflict with each other?

> s.write(svar)
> passwd = s.read(128)
> puts "passordet er #{passwd}"
> s.close
> puts "Koplet fra #{host}"

again, thanks for helping me out with this great answer!
-terje
--
Posted via http://www.ruby-....

Brian Candler

2/15/2009 8:53:00 PM

0

Terje Haarstad wrote:
>> sum = buff[0] + buff[1] + buff[2] + buff[3]
>> svar = [sum & 0xffffffff].pack("I")
>
> Why do u have to use sum & 0xffffffff? an error occour
> (`pack': bignum too big to convert into `unsigned long' (RangeError)
> from vortex0.rb:16) if i DONT use 0xfffffffff while use s.gets
> but why do they conflict with each other?

Ruby numbers don't wrap around - they can grow indefinitely large.

Sometimes the four random 32-bit numbers add up to a 33-bit number. In
this case, the server is expecting you to truncate the result to 32
bits. If you don't truncate explicitly, Array#pack will abort because
the number you're asking it to fit into a 32-bit space is too big.

HTH,

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

Kirk McElhearn

3/18/2011 3:35:00 PM

0

On 2011-03-18 13:15:49 +0100, James Pablos <james.pablos@gmail.com> said:

>
> I don't know why they didn't play that song live. It could have been a
> monster.

There's a lot of early stuff they never played live. Those first three
CDs in the big box set - Birth of the Dead and Rare Cuts & Oddities
(which I'm listening to now, inspired by this thread) have a lot of
un-Dead-like songs on them, but some that could have been good live
staples.

Kirk
--

Kirkville -- http://www.mce...
Writings about more than just Macs
Take Control of iTunes 10: The FAQ: http://www.mce.../itunes

onetaste2000

3/18/2011 6:17:00 PM

0

On Mar 18, 5:15 am, James Pablos <james.pab...@gmail.com> wrote:

> The studio version of "Alice D. Millionaire" is another good reason to
> buy this remaster.

Good lord, James, we are in a mileage situation. I just heard that for
the first time the day Bear died. If it were possible for me to
appreciate Hunter more than I do, this would be a great reason.

Fred

james.pablos

3/18/2011 8:19:00 PM

0

On Mar 18, 2:17 pm, Lfh <onetaste2...@yahoo.com> wrote:

> Good lord, James, we are in a mileage situation. I just heard that for
> the first time the day Bear died. If it were possible for me to
> appreciate Hunter more than I do, this would be a great reason.

I am BLASTING it right now thru the new Bose speakers that we just
bought.

YMMV




marcman

3/19/2011 12:35:00 AM

0

On Mar 18, 4:19 pm, James Pablos <james.pab...@gmail.com> wrote:
> On Mar 18, 2:17 pm, Lfh <onetaste2...@yahoo.com> wrote:
>
> > Good lord, James, we are in a mileage situation. I just heard that for
> > the first time the day Bear died. If it were possible for me to
> > appreciate Hunter more than I do, this would be a great reason.
>
> I am BLASTING it right now thru the new Bose speakers that we just
> bought.
>
> YMMV

Tell me about your speakers!

james.pablos

3/19/2011 1:43:00 AM

0

On Mar 18, 8:34 pm, marcman <marcmanstud...@gmail.com> wrote:

> Tell me about your speakers!

I'm not an audiophile, so I can't give you all the stats. But it's a
Bose Companion 3 Series II multimedia speaker system. About $225 on
Amazon.

It sure beats the little tinny pair of Jensen speakers that we had
before.