Re: checking connection to server:port [socket]
- From: James Tucker <jftucker@xxxxxxxxx>
- Date: Thu, 17 Apr 2008 06:04:06 -0500
On 17 Apr 2008, at 03:40, Peña, Botp wrote:
From: list-bounce@xxxxxxxxxxx
# [mailto:list-bounce@xxxxxxxxxxx] On Behalf Of Kr Alfabeta
# ...connect method. I need to check connection to server
# www.example.com:xxxx (UDP)
# ...
# The problem is that I don't know what I should print there.
#
# I just need to check the connection.
# In PHP there are very simple solution:
# $fp = fpsockopen(....)
# if ($fp) return true; else return false;
i think you just want to check a udp connection to a udp server. no
problem.
require 'socket'
s = UDPSocket.new
s.connect("10.10.10.10", 10101)
puts "you are connected" #<-- if you get here, you are connected
the reason the above works is that s.connect will raise an exception
if it cannot connect and you'll have to catch the reason by rescue-
ing it.
Interesting idea, however, as I said in my last post, this is really
not possible to do reliably with UDP (in fact, at all in reality, as
the api will "lie"). UDP is stateless and connectionless, there are no
connections. The PHP api was essentially lying to the OP, and ruby
will too (in fact, it's not a lie, it's PEBKAC, and the man page for
UDP(4) will explain what I'm telling you guys better than I have,
please do read it):
=> truerequire 'socket'
=> #<UDPSocket:0x8320c>sock = UDPSocket.new
SocketError: getaddrinfo: nodename nor servname provided, or not knownsock.connect('some.really.long.domain.that.doesnt.exist', 1234)
from (irb):3:in `connect'
from (irb):3
from :0
IP does not exist, and there's no machine on it.sock.connect('127.0.0.220',1234) # Please note carefully that this
=> 0
=> "PING 127.0.0.220 (127.0.0.220): 56 data bytes\n\n--- 127.0.0.220`ping -c 1 127.0.0.220`
ping statistics ---\n1 packets transmitted, 0 packets received, 100%
packet loss\n"
=> 3sock.write('foo')
=> 3sock.write('foo')
=> 3sock.write('foo')
=> 3sock.write('foo')
=> 3sock.write('foo')
=> 3sock.write('foo')
=> 3sock.write('foo')
=> 3sock.write('foo')
# N.N.B. No failures, at all, and yet there should be if there was a
'connection' semantic in the protocol.
=> "PING 192.168.253.253 (192.168.253.253): 56 data bytes\n\n---`ping -c 1 192.168.253.253`
192.168.253.253 ping statistics ---\n1 packets transmitted, 0 packets
received, 100% packet loss\n"
# N.N.B. The ping failed, see?
this IP does not exist, and there's no machine on it.sock.connect('192.168.253.253', 1234) # Please note carefully that
=> 0
=> 3sock.write('foo')
=> 3sock.write('foo')
=> 3sock.write('foo')
=> 3sock.write('foo')
=> 3sock.write('foo')
=> 3sock.write('foo')
# Still no failures...
this IP does not exist, and there's no machine on it *I think*. Seesock.connect('67.207.151.240', 1234) # Please note carefully that
note at end of mail.
=> 0
=> 3sock.write('foo')
=> 3sock.write('foo')
=> 3sock.write('foo')
=> 3sock.write('foo')
Both of the above "connections" should have "failed" (they don't
because there are no connections, and there are no 'failures' for UDP,
the protocol is dumb as hell and will never know, unless the packets
are actively rejected by a host at the receiving IP address), there
are no machines on those IPs. I've provided multiple examples on
different ip classes and you'll notice that the DNS error is a *DNS*
error, not a UDP socket connect. UDP doesn't "connect".
I have since looked up why the "connect()" function even exists, and
it is for the sole purpose of reserving a source port for sending data
(which may aid in remote stateful logic, where the application layer
protocol is not completely stateless, even though UDP is).
# and just then you can send and receive data.
# Maybe there are any solutions within sockets?
socket programming in ruby is very complete and ranges from low
level basic socket programming to high level uri... you might want
to read the ruby programming language book..
And I'll repeat, please read UDP(4).
That is `man 4 udp`. Other good references can be found on wikipedia:
http://en.wikipedia.org/wiki/User_Datagram_Protocol
It's very very important that the OP realises that their code in
whatever language, is not doing what they think.
kind regards -botp
P.S. The last IP, the one that's internet public I chose by searching
for an IP that didn't respond to ICMP echo, so it may actually exist,
nonetheless, it was the first empty publicly routable IP I could find
that appeared to be down, without actually doing a massive scan range
over the internet (which is frowned upon). Please don't abuse that IP,
I don't know who it belongs to, nor do I care at this point.
.
- References:
- checking connection to server:port [socket]
- From: Kr Alfabeta
- Re: checking connection to server:port [socket]
- From: Peña, Botp
- checking connection to server:port [socket]
- Prev by Date: Re: How to "break" a "case-when" ?
- Next by Date: Re: Ada vs Ruby
- Previous by thread: Re: checking connection to server:port
- Next by thread: write ruby code on a blog
- Index(es):
Relevant Pages
|