# Ruby-newbie

**URL:** <https://www.ruby-forum.com/t/ruby-newbie/160164>\
**Category:** Ruby\
**Created:** [February 12, 2009, 1:30pm UTC](https://www.ruby-forum.com/t/ruby-newbie/160164 "2009-02-12T13:30:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eugynon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b4bc9f/32.png) [@eugynon](https://www.ruby-forum.com/u/eugynon)\
**Post date:** [February 12, 2009, 1:30pm UTC](https://www.ruby-forum.com/t/ruby-newbie/160164/1 "2009-02-12T13:30:05Z")

</div>

## 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Ã™e{g6Ã»bzzzt, wrong  
Koplet fra [vortex.labs.pulltheplug.org](http://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](http://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}”

---

<div class="post-metadata">

**Author:** ![eugynon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b4bc9f/32.png) [@eugynon](https://www.ruby-forum.com/u/eugynon)\
**Post date:** [February 12, 2009, 2:11pm UTC](https://www.ruby-forum.com/t/ruby-newbie/160164/2 "2009-02-12T14:11:16Z")

</div>

Terje Haarstad wrote:

> require ‘socket’  
> host = ‘[vortex.labs.pulltheplug.org](http://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.

---

<div class="post-metadata">

**Author:** ![eugynon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b4bc9f/32.png) [@eugynon](https://www.ruby-forum.com/u/eugynon)\
**Post date:** [February 12, 2009, 2:16pm UTC](https://www.ruby-forum.com/t/ruby-newbie/160164/3 "2009-02-12T14:16:10Z")

</div>

require ‘socket’  
host = ‘[vortex.labs.pulltheplug.org](http://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}”

---

<div class="post-metadata">

**Author:** ![eugynon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b4bc9f/32.png) [@eugynon](https://www.ruby-forum.com/u/eugynon)\
**Post date:** [February 12, 2009, 5:00pm UTC](https://www.ruby-forum.com/t/ruby-newbie/160164/4 "2009-02-12T17:00:33Z")

</div>

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

Brian C. wrote:

> require ‘socket’  
> host = ‘[vortex.labs.pulltheplug.org](http://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

---

<div class="post-metadata">

**Author:** ![eugynon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b4bc9f/32.png) [@eugynon](https://www.ruby-forum.com/u/eugynon)\
**Post date:** [February 15, 2009, 9:54pm UTC](https://www.ruby-forum.com/t/ruby-newbie/160164/5 "2009-02-15T21:54:29Z")

</div>

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.
