[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to receive data using socket programming

Kamaljeet Saini

1/22/2009 10:43:00 PM

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"
--
Posted via http://www.ruby-....

10 Answers

Ryan Davis

1/22/2009 11:54:00 PM

0


On Jan 22, 2009, at 14:43 , Kamaljeet Saini wrote:

> 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"

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)



Kamaljeet Saini

1/23/2009 5:08:00 AM

0

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
--
Posted via http://www.ruby-....

Jonathan Groll

1/23/2009 9:10:00 AM

0

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/progra...

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

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

Regards,
Jonathan

Brian Candler

1/23/2009 10:21:00 AM

0

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
--
Posted via http://www.ruby-....

Kamaljeet Saini

1/23/2009 5:13:00 PM

0

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;

}

}

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

Kamaljeet Saini

1/23/2009 9:05:00 PM

0

I got this following code from
"http://www.ruby-doc.org/core/classes/Socket.src/M002120....

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.
--
Posted via http://www.ruby-....

Brian Candler

1/25/2009 3:48:00 PM

0

Kamaljeet Saini wrote:
> I got this following code from
> "http://www.ruby-doc.org/core/classes/Socket.src/M002120....
>
> 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....

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.

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

Kamaljeet Saini

1/25/2009 4:56:00 PM

0

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.
--
Posted via http://www.ruby-....

Brian Candler

1/26/2009 8:51:00 AM

0

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.
--
Posted via http://www.ruby-....

Brian Candler

1/26/2009 8:52:00 AM

0

s/2100/2200/
--
Posted via http://www.ruby-....