Simple, simple socket question

Hello all,

I am very sorry if this has already been asked and answered; I have,
however, searched to the best of my ability and cannot seem to come up
with anything.

I am merely a beginner with real programming languages, however I am
very experienced with higher-level scripting languages, and have worked
extensively with sockets in mIRCscript.

Here is my attempt at my very first Ruby socket program:

require ‘socket’

mySock = TCPSocket::new(‘www.google.com’, 80)
mySock.write(‘GET / HTTP/1.1\n’)
mySock.write(‘Host: www.google.com\n’)
mySock.write(‘\n’)

data = mySock.recv(128)
mySock.close
puts data

I would expect for this program to output the first 128 bytes of
Google’s HTML headers and code.
When this program is run, it simply hangs on the console window and does
not appear to do, well, anything. Even if I replace puts data with puts
‘hi’, the program still yields no result. Why? What is needed to make
this code work as expected?
Also, in reading many different Ruby socket tutorials, I am unable to
distinguish between two socket-related methods that I’ve encountered:
send and write. I’ve attempted using both. In every tutorial I’ve
encountered, the send method is called with only one String parameter
(just like my calls to write in the above code), however when I try to
emulate this, Ruby tells me that send requires two parameters, the
second of which must be an Integer. So, my questions here are, what
does this Integer parameter represent, and why is it needed? And, what
exactly is the difference between the write and send methods?

Thank you so much for your time,
-Will

Hi,

Change your singel quotes to double quotes.
With singel quotes \n won’t be understandable as an carrige-return,
just as a backslash \ and an “n” n.

/erik

Will Dresh wrote:

Hello all,

I am very sorry if this has already been asked and answered; I have,
however, searched to the best of my ability and cannot seem to come up
with anything.

I am merely a beginner with real programming languages, however I am
very experienced with higher-level scripting languages, and have worked
extensively with sockets in mIRCscript.

Here is my attempt at my very first Ruby socket program:

require ‘socket’

mySock = TCPSocket::new(‘www.google.com’, 80)
mySock.write(‘GET / HTTP/1.1\n’)
mySock.write(‘Host: www.google.com\n’)
mySock.write(‘\n’)

data = mySock.recv(128)
mySock.close
puts data

I would expect for this program to output the first 128 bytes of
Google’s HTML headers and code.
When this program is run, it simply hangs on the console window and does
not appear to do, well, anything. Even if I replace puts data with puts
‘hi’, the program still yields no result. Why? What is needed to make
this code work as expected?
Also, in reading many different Ruby socket tutorials, I am unable to
distinguish between two socket-related methods that I’ve encountered:
send and write. I’ve attempted using both. In every tutorial I’ve
encountered, the send method is called with only one String parameter
(just like my calls to write in the above code), however when I try to
emulate this, Ruby tells me that send requires two parameters, the
second of which must be an Integer. So, my questions here are, what
does this Integer parameter represent, and why is it needed? And, what
exactly is the difference between the write and send methods?

Thank you so much for your time,
-Will

This works on my system (Just brings back 128 bytes HTTP header). (Note
the extra ‘\n’…

Strings changed from single quotes to double quotes.

(As per last poster’s suggestion)

require ‘socket’

mySock = TCPSocket::new(“www.google.com”, 80)
mySock.write(“GET / HTTP/1.1\n”)
mySock.write(“Host: www.google.com\n”)
mySock.write(“\n\n”) # extra “\n” added.

data = mySock.recv(128)
mySock.close
puts data

Cheers

John