# Simple, simple socket question

**URL:** <https://www.ruby-forum.com/t/simple-simple-socket-question/147303>\
**Category:** Ruby\
**Created:** [August 30, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/simple-simple-socket-question/147303 "2008-08-30T18:14:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![willdeluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7cd45c/32.png) [@willdeluca](https://www.ruby-forum.com/u/willdeluca)\
**Post date:** [August 30, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/simple-simple-socket-question/147303/1 "2008-08-30T18:14:32Z")

</div>

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](http://www.google.com)’, 80)  
mySock.write(‘GET / HTTP/1.1\n’)  
mySock.write(‘Host: [www.google.com](http://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

---

<div class="post-metadata">

**Author:** ![willdeluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7cd45c/32.png) [@willdeluca](https://www.ruby-forum.com/u/willdeluca)\
**Post date:** [August 30, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/simple-simple-socket-question/147303/2 "2008-08-30T18:42:34Z")

</div>

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](http://www.google.com)’, 80)  
> mySock.write(‘GET / HTTP/1.1\n’)  
> mySock.write(‘Host: [www.google.com](http://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

---

<div class="post-metadata">

**Author:** ![willdeluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7cd45c/32.png) [@willdeluca](https://www.ruby-forum.com/u/willdeluca)\
**Post date:** [September 1, 2008, 11:35am UTC](https://www.ruby-forum.com/t/simple-simple-socket-question/147303/3 "2008-09-01T11:35:28Z")

</div>

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](http://www.google.com)”, 80)  
mySock.write(“GET / HTTP/1.1\n”)  
mySock.write(“Host: [www.google.com](http://www.google.com)\n”)  
mySock.write(“\n\n”) # extra “\n” added.

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

Cheers

John
