From: (e-mail address removed)
# [mailto:
[email protected]] 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 =3D fpsockopen(....)
# if ($fp) return true; else return false;
i think you just want to check a udp connection to a udp server. no =20=
problem.
require 'socket'
s =3D 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 =20=
if it cannot connect and you'll have to catch the reason by rescue-=20
ing it.
Interesting idea, however, as I said in my last post, this is really =20
not possible to do reliably with UDP (in fact, at all in reality, as =20
the api will "lie"). UDP is stateless and connectionless, there are no =20=
connections. The PHP api was essentially lying to the OP, and ruby =20
will too (in fact, it's not a lie, it's PEBKAC, and the man page for =20
UDP(4) will explain what I'm telling you guys better than I have, =20
please do read it):
SocketError: getaddrinfo: nodename nor servname provided, or not known
from (irb):3:in `connect'
from (irb):3
from :0
IP does not exist, and there's no machine on it.
=3D> 0=3D> "PING 127.0.0.220 (127.0.0.220): 56 data bytes\n\n--- 127.0.0.220 =20=
ping statistics ---\n1 packets transmitted, 0 packets received, 100% =20
packet loss\n"=3D> 3
# N.N.B. No failures, at all, and yet there should be if there was a =20
'connection' semantic in the protocol.
=3D> "PING 192.168.253.253 (192.168.253.253): 56 data bytes\n\n--- =20
192.168.253.253 ping statistics ---\n1 packets transmitted, 0 packets =20=
received, 100% packet loss\n"
# N.N.B. The ping failed, see?
this IP does not exist, and there's no machine on it.
=3D> 0=3D> 3
# Still no failures...
this IP does not exist, and there's no machine on it *I think*. See =20
note at end of mail.
=3D> 0=3D> 3
Both of the above "connections" should have "failed" (they don't =20
because there are no connections, and there are no 'failures' for UDP, =20=
the protocol is dumb as hell and will never know, unless the packets =20
are actively rejected by a host at the receiving IP address), there =20
are no machines on those IPs. I've provided multiple examples on =20
different ip classes and you'll notice that the DNS error is a *DNS* =20
error, not a UDP socket connect. UDP doesn't "connect".
I have since looked up why the "connect()" function even exists, and =20
it is for the sole purpose of reserving a source port for sending data =20=
(which may aid in remote stateful logic, where the application layer =20
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 =20
level basic socket programming to high level uri... you might want =20
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 =20
whatever language, is not doing what they think.
P.S. The last IP, the one that's internet public I chose by searching =20=
for an IP that didn't respond to ICMP echo, so it may actually exist, =20=
nonetheless, it was the first empty publicly routable IP I could find =20=
that appeared to be down, without actually doing a massive scan range =20=
over the internet (which is frowned upon). Please don't abuse that IP, =20=
I don't know who it belongs to, nor do I care at this point.=