Re: checking connection to server:port [socket]




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):

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
IP 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
ping 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
received, 100% packet loss\n"

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

sock.connect('192.168.253.253', 1234) # Please note carefully that
this 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
this 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,
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.
.



Relevant Pages

  • Re: Socket Exception
    ... In this case the server most likely rejected your ... connection. ... Each command is a new socket connection that is opened and closed ... /// Required designer variable. ...
    (microsoft.public.win32.programmer.networks)
  • Re: ASP.NET 2.0 - a newbie question
    ... Is there some experienced ASP.NET 2.0 developer, ... server that comes bundled Microsoft VWDE. ... A connection attempt failed because the ... connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& ...
    (microsoft.public.dotnet.framework.aspnet)
  • error when move web service from dev machine to test server
    ... I have a simple web service that I developed on my laptop that resides ... test server, & when I try to run it, I get the error "No connection could be ... No connection could be made because the target machine actively refused it ... Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Messaging alert
    ... I was hopping that I could use sockets to create a connection back and forth in order to send the message. ... I want to be able to send a file name and other information thru the client socket to then process in the servers. ... fileevent $sock w ... At the server side I am listening at the port and execute Accept2 when the socket is created. ...
    (comp.lang.tcl)
  • Re: Strange server socket behaviour
    ... asynchronous server socket: ... I already have a perfectly working server that uses asynchronous sockets ... Why should the remote client close the connection? ... when a .NET client connects. ...
    (microsoft.public.dotnet.framework)