IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Sorry I'm late to the Party ... Ruby Version
As Ben suggested, Ruby is not a bad choice either.
\n#!/bin/env ruby\n\nrequire 'socket'\n\nhost, port, string = ARGV\nfail "Must supply host, port, and text string to send!" unless string\n\nputs "#{host}:#{port}:#{string}"\n\nTCPSocket.open(host, port) do |sock|\n  sock.puts(string)\n  puts sock.gets\nend\n
And the low level protocols are there too, if you wish to work at the bind/connect level.
--
-- Jim Weirich jim@weirichhouse.org [link|http://onestepback.org|http://onestepback.org]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
New Where's the error handling?
For the curious... and since the other examples had it. :-)
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New I wouldn't bet that it is missing
I don't know that library, but a common Ruby idiom is that if you pass a block to the open method, then the called method is responsible for doing the open, loop, and necessary error checks. :-)

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New OK, fine... but
Where is the error handling? :-)

Unless the library is just eating the error (bad), then the code is ignoring the error (which is what I'm asking about).

In other words, how will it know if the connection fails?
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New As I said...
If it follows the idiom that I've seen in other libraries, the library throws an exception that will display default error messages which will suffice for simple scripts.

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New Near enough to Python, then.
But for purposes of this example, that's not what's being done.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Re: Near enough to Python, then.
But for purposes of this example, that's not what's being done.

Actually, it is. The Perl and Python examples (1) close the socket (if it is open), (2) print a simple error message, (3) and exit with an error code.

The Ruby version handles (1) within the open method automatically, and (2) and (3) in the top level exception handler provided by Ruby.

If you want a more custom error message than is provided by the default error handler, or if you want a more specific error code on exit, you could do:
\nbegin\n  TCPSocket.open(host, port) do |sock|\n    sock.puts(string)\n    puts sock.gets\n  end\nrescue Exception => ex\n  puts "Error: #{ex} at #{ex.backtrace[0]}"\n  exit(1)\nend\n
Note that it is not necessary to explicitly close the socket, even in the presence of exceptions, when using the block form of open as coded in the examples.

Error output from original code:
\nrnet.rb:10:in `initialize': Connection refused - connect(2) (Errno::ECONNREFUSED)\n\tfrom rnet.rb:10:in `open'\n\tfrom rnet.rb:10\n
Error output from the modified code above:
\nError: Connection refused - connect(2) at rnet2.rb:11:in `initialize'\n
--
-- Jim Weirich jim@weirichhouse.org [link|http://onestepback.org|http://onestepback.org]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
New As I said...
The Python example does pretty much the same thing.

If you leave off the exception handling, it closes the socket, prints a simple error message, and exits with an error code. There's nothing magical in the Ruby example. :-)

So again, the examples being given here DO have a more custom error message. Which was all I was asking for. The point of the examples being good, explicit (as in, you can expect errors here, etc.) networking code suitable for showing to a beginner.

So, thanks for the example. :-)
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
Expand Edited by admin Sept. 19, 2005, 08:53:23 PM EDT
New Ahh, makes me long for the old days
NOT!

Doing IO programming (serial or parallel), bit twiddling the IOCTL calls for every byte, checking the handshake lines, etc.

Man that sucked!
     Simple network programming language? - (tjsinclair) - (51)
         Python. - (folkert)
         What Greg said. - (admin) - (3)
             That's exactly what I was thinking - (tjsinclair) - (2)
                 cautions, cause.. - (cforde) - (1)
                     Understood - (tjsinclair)
         REBOL gets you there the fastest - (ChrisR) - (4)
             Agreed. - (admin)
             I considered Java briefly - (tjsinclair) - (2)
                 Definietly not Java or anything with a compiler - (tuberculosis) - (1)
                     Granted - (tjsinclair)
         perl, creating sockets and listening is fairly easy -NT - (boxley) - (35)
             Thanks for the tip - (tjsinclair)
             But not as readable as Python. - (admin) - (33)
                 This isn't readable? - (broomberg) - (32)
                     Ben could reduce that to about 12 characters... ;-) -NT - (Another Scott) - (15)
                         Of course he could - (broomberg) - (14)
                             :-) Yup, but in Python one can't do that. - (Another Scott) - (11)
                                 Hmm - (broomberg) - (10)
                                     Write That Code! - (Another Scott)
                                     Sorry I'm late to the Party ... Ruby Version - (JimWeirich) - (8)
                                         Where's the error handling? - (admin) - (7)
                                             I wouldn't bet that it is missing - (ben_tilly) - (6)
                                                 OK, fine... but - (admin) - (5)
                                                     As I said... - (ben_tilly) - (4)
                                                         Near enough to Python, then. - (admin) - (3)
                                                             Re: Near enough to Python, then. - (JimWeirich) - (2)
                                                                 As I said... - (admin) - (1)
                                                                     Ahh, makes me long for the old days - (broomberg)
                             Oops. -NT - (inthane-chan)
                             ICJRLPD (new thread) - (inthane-chan)
                     As I said, "not AS readable as the Python" - (admin) - (15)
                         Not bad - (broomberg) - (4)
                             What. He. Said. - (jb4) - (3)
                                 WTF do YOU think it has on it...? - (admin)
                                 I don't think you'll be surprised. (img) - (Another Scott)
                                 Hiss - (ChrisR)
                         Wel, it's beautiful code - (Arkadiy) - (9)
                             Use the socket libraries then. - (admin)
                             It's introduction to networking - (tjsinclair) - (7)
                                 I'd go with socket basics. - (jake123) - (6)
                                     Re: I'd go with socket basics. - (admin) - (5)
                                         Didn't say it would take very long - (jake123) - (3)
                                             Programming Languages will influence the model - (ChrisR) - (2)
                                                 Indeed - (jake123) - (1)
                                                     Definitely want high level languages - (tjsinclair)
                                         Yep. -NT - (Arkadiy)
         I'd suggest Ruby... - (ben_tilly) - (2)
             I believe I stole that example from it - (broomberg)
             I'm thinking of quick and dirty HOWTO here - (tjsinclair)
         Consider Object REXX - (jake123) - (1)
             Thanks for the reminder - (tjsinclair)

'Cause the music rules.
165 ms