Post #224,871
9/15/05 5:50:29 PM
|
Hmm
Before this degenerates into a language war, something is missing.
COMPETING CODE!
Until posted, the evangelists of nothing to say about it.
|
Post #224,872
9/15/05 6:00:41 PM
|
Write That Code!
Contestant: "Tom, I can Write That Code in 15 Characters!"
Audience:Whooo!
Tom Kennedy: "Write That Code!"
:-D
Cheers, Scott.
|
Post #225,517
9/19/05 6:10:12 PM
|
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)
|
Post #225,518
9/19/05 6:19:26 PM
|
Where's the error handling?
For the curious... and since the other examples had it. :-)
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #225,526
9/19/05 6:39:45 PM
|
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)
|
Post #225,529
9/19/05 7:11:36 PM
|
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..."
|
Post #225,531
9/19/05 7:25:13 PM
|
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)
|
Post #225,532
9/19/05 7:29:04 PM
|
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..."
|
Post #225,540
9/19/05 8:42:11 PM
|
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)
|
Post #225,543
9/19/05 8:52:02 PM
9/19/05 8:53:23 PM
|
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..."
Edited by admin
Sept. 19, 2005, 08:53:23 PM EDT
|
Post #225,544
9/19/05 8:57:07 PM
|
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!
|