On 17 Apr 2008, at 03:40, Pe, Botp wrote:
> From: list-bounce / example.com
> # [mailto:list-bounce / example.com] 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  roblem.
>
> 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  f 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  onnections. 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):

 >> require 'socket'
=> true
 >> sock = UDPSocket.new
=> #<UDPSocket:0x8320c>
 >> sock.connect('some.really.long.domain.that.doesnt.exist', 1234)
SocketError: getaddrinfo: nodename nor servname provided, or not known
	from (irb):3:in `connect'
	from (irb):3
	from :0
 >> sock.connect('127.0.0.220',1234) # Please note carefully that this  P does not exist, and there's no machine on it.
=> 0
 >> `ping -c 1 127.0.0.220`
=> "PING 127.0.0.220 (127.0.0.220): 56 data bytes\n\n--- 127.0.0.220  ing statistics ---\n1 packets transmitted, 0 packets received, 100%  
packet loss\n"
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3

# N.N.B. No failures, at all, and yet there should be if there was a  
'connection' semantic in the protocol.

 >> `ping -c 1 192.168.253.253`
=> "PING 192.168.253.253 (192.168.253.253): 56 data bytes\n\n---  
192.168.253.253 ping statistics ---\n1 packets transmitted, 0 packets  eceived, 100% packet loss\n"

# N.N.B. The ping failed, see?

 >> sock.connect('192.168.253.253', 1234) # Please note carefully that  his IP does not exist, and there's no machine on it.
=> 0
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3

# Still no failures...

 >> sock.connect('67.207.151.240', 1234) # Please note carefully that  his IP does not exist, and there's no machine on it *I think*. See  
note at end of mail.
=> 0
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3
 >> sock.write('foo')
=> 3

Both of the above "connections" should have "failed" (they don't  
because there are no connections, and there are no 'failures' for UDP,  he 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