Forum: Ruby How to receive data using socket programming

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
31b6449618ce52d87f9215385c1eb467?d=identicon&s=25 Kamaljeet Saini (kamaljeetsinghsaini)
on 2009-01-22 23:44
Hi,

I have this following code that able to make connection and then
disconnect. But after doing connection, i wish to receive data before
doing disconnect. Im very new to understand socket programming will be
great to get some help here.

require 'socket'
streamSock = Socket::new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
myaddr = [Socket::AF_INET, 5555, 113, 129, 2, 162, 0,
0].pack("snCCCCNN")
print "connecting...\n"
streamSock.connect( myaddr )
print "connected\n"

# How to read/receive data ( sample code example will help )

print "disconnecting...\n"
streamSock.close
print "disconnected\n"
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2009-01-23 01:02
(Received via mailing list)
On Jan 22, 2009, at 14:43 , Kamaljeet Saini wrote:

> streamSock.connect( myaddr )
> print "connected\n"
>
> # How to read/receive data ( sample code example will help )
>
> print "disconnecting...\n"
> streamSock.close
> print "disconnected\n"

The Socket section in the pickaxe is a good place to start. pg 714.

ri Socket.connect
ri Socket.accept

also:

ri TCPServer

much better way to go if you're able to go high level (and are writing
the server side of things as well)
31b6449618ce52d87f9215385c1eb467?d=identicon&s=25 Kamaljeet Saini (kamaljeetsinghsaini)
on 2009-01-23 06:09
Where is that located ( pg 714 that you referring please ) somewhere on
my local machine or on website.

Can you please guide me to path that you point out in your previous
reply.
thanks
66d818f89ac9f6c2673d06097120a338?d=identicon&s=25 Jonathan Groll (Guest)
on 2009-01-23 10:12
(Received via mailing list)
On Fri, Jan 23, 2009 at 02:07:47PM +0900, Kamaljeet Saini wrote:
>Where is that located ( pg 714 that you referring please ) somewhere on
>my local machine or on website.
>
>Can you please guide me to path that you point out in your previous
>reply.
>thanks

The book Ryan was referring to (the pickaxe) is "Programming Ruby" by
Dave Thomas. It is a well known ruby book:
http://www.pragprog.com/titles/ruby/programming-ruby

There is a free online version of it available at:
http://www.ruby-doc.org/docs/ruby-doc-bundle/Progr...

(Not sure how complete or recent the online version is though)

Regards,
Jonathan
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-23 11:23
Kamaljeet Saini wrote:
> # How to read/receive data ( sample code example will help )

streamSock.gets      # read a line up to \n
streamSock.read(n)   # read n bytes, waiting if necessary
streamSock.readpartial(n)  # read between 1 and n bytes as available

see also Kernel#select for testing whether data is available right now
31b6449618ce52d87f9215385c1eb467?d=identicon&s=25 Kamaljeet Saini (kamaljeetsinghsaini)
on 2009-01-23 18:14
Can someone help please in interpreting the following C# logic/algorithm
to ruby socket programming. Kind of ruby socket code as how it wll look
like in doing so.


receivedBytesLen = clientSock.Receive(Recievbuf, bufoffset, packetsize,
0);

int fileNameLen = BitConverter.ToChar(Recievbuf, 0);

string fileName = "image1.text";//Encoding.ASCII.GetString(Recievbuf, 4,
fileNameLen);

// MessageBox.Show("Client:{0} connected & File {1} started received." +
clientSock.RemoteEndPoint + fileName);

bWrite = new BinaryWriter(File.Open(receivedPath + fileName,
FileMode.Append));

//bWrite.Write(Recievbuf, (int)(352 * 240 * 2 - test),
receivedBytesLen);

bWrite.Write(Recievbuf, bufoffset, receivedBytesLen);

// MessageBox.Show("File: {0} received & saved at path: {1}" + fileName
+ receivedPath);

if (receivedBytesLen != -1)

{

bufoffset += receivedBytesLen;

leftover = toRecv - bufoffset;

if (leftover < packetsize)

{

packetsize = leftover;

}

}
31b6449618ce52d87f9215385c1eb467?d=identicon&s=25 Kamaljeet Saini (kamaljeetsinghsaini)
on 2009-01-23 22:07
I got this following code from
"http://www.ruby-doc.org/core/classes/Socket.src/M0...

    require 'socket'
     include Socket::Constants
     socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
     sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
     socket.bind( sockaddr )
     socket.listen( 5 )
     client, client_sockaddr = socket.accept
     data = client.recvfrom( 20 )[0].chomp
     puts "I only received 20 bytes '#{data}'"
     sleep 1
     socket.close

How to make this code run ? Im trying to run from SciTE editor for ruby.
What correction i need to make to run this code so to get feel as how
this piece of code of socket works as seeing its output.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-25 16:48
Kamaljeet Saini wrote:
> I got this following code from
> "http://www.ruby-doc.org/core/classes/Socket.src/M0...
>
>     require 'socket'
>      include Socket::Constants
>      socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
>      sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
>      socket.bind( sockaddr )
>      socket.listen( 5 )
>      client, client_sockaddr = socket.accept
>      data = client.recvfrom( 20 )[0].chomp
>      puts "I only received 20 bytes '#{data}'"
>      sleep 1
>      socket.close
>
> How to make this code run ? Im trying to run from SciTE editor for ruby.
> What correction i need to make to run this code so to get feel as how
> this piece of code of socket works as seeing its output.

http://www.catb.org/~esr/faqs/smart-questions.html#intro

You didn't show any error. What makes you think it isn't running?

The code above is a server, listening for incoming TCP connections on
port 2200. A simpler way of implementing this is using the gserver.rb
module which is included in the Ruby standard library. There is
documentation at the top of this file.
31b6449618ce52d87f9215385c1eb467?d=identicon&s=25 Kamaljeet Saini (kamaljeetsinghsaini)
on 2009-01-25 17:58
When i try to run the above code in the SciTE editor ( in the .rb file )
by pressing F5, DOS console black window appear for ever and dont go
away.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-26 09:53
Kamaljeet Saini wrote:
> When i try to run the above code in the SciTE editor ( in the .rb file )
> by pressing F5, DOS console black window appear for ever and dont go
> away.

Good. It's a server, waiting for an incoming connection on TCP port
2100, as I already said. If you puts some 'puts' statements in, you'll
find it gets as far as socket.accept which is where it is waiting.

Try opening another window, typing "telnet 127.0.0.1 2100", and then
typing 20 characters.

If you don't understand the concepts of socket, bind, listen, accept
then you need to get a good book on TCP/IP sockets programming. "Unix
Network Programming vol.1" by Richard Stevens is excellent, although you
may find it has way more information than you need right now.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-26 09:53
s/2100/2200/
This topic is locked and can not be replied to.