How to receive data using socket programming


#1

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”


#2

On Jan 22, 2009, at 14:43 , Kamaljeet S. 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)


#3

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


#4

On Fri, Jan 23, 2009 at 02:07:47PM +0900, Kamaljeet S. 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 T… 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/ProgrammingRuby/index.html

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

Regards,
Jonathan


#5

Kamaljeet S. 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


#6

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

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.


#7

Kamaljeet S. wrote:

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

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.


#8

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.


#9

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;

}

}


#10

s/2100/2200/


#11

Kamaljeet S. 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.