[erlang-questions] inet_res:getbyname/2 and udp:connect/3

Tony Rogvall tony@REDACTED
Tue Jun 15 09:48:27 CEST 2010


On 15 jun 2010, at 00.51, Per Hedeland wrote:


> {ok,U} = gen_udp:open(0).
{ok,#Port<0.682>}

> inet:setopts(U, [{active, false}]).
ok

> gen_udp:connect(U, "127.0.0.1", 12345).
ok

> gen_udp:send(U, <<1,2,3>>).            
ok

> gen_udp:recv(U, 0, 1000).              
{error,econnrefused}             <==================

> gen_udp:recv(U, 0, 1000).
{error,timeout}


Working? Maybe you missed the first recv?
Maybe I am missing something?

/Tony







> Raimo Niskanen <raimo+erlang-questions@REDACTED> wrote:
>> 
>> gen_udp:connect/3 calls
>> 
>>   int connect(int socket, const struct sockaddr *address,
>>             socklen_t address_len);
>> 
>> on the socket. Excerpt from man connect:
>> 
>>   If the initiating socket is not connection-mode, then connect()
>>   shall set the socket's peer address, and no connection  is  made.  For
>>   SOCK_DGRAM  sockets,  the  peer  address identifies where all datagrams
>>   are sent on subsequent send() functions, and limits the remote
>>   sender for subsequent recv() functions. If address is a null address
>>   for the protocol, the socket's peer address shall be reset.
>> 
>> i.e default destination is set and reception is limited. I think
>> it is receiving from only the inteded DNS server that is the purpose.
> 
> Hm, in general the purpose of calling connect() for a UDP socket is
> this:
> 
> 1> {ok,S}=gen_udp:open(0).
> {ok,#Port<0.427>}
> 2> gen_udp:send(S,"127.0.0.1",12345,<<>>).  
> ok
> 3> flush().
> ok
> 4> gen_udp:connect(S,"127.0.0.1",12345).
> ok
> 5> gen_udp:send(S,"127.0.0.1",12345,<<>>).  
> ok
> 6> flush().                               
> Shell got {udp_error,#Port<0.427>,econnrefused}
> ok
> 
> I.e. if you send to a port that doesn't have a listener (on a host that
> is running), you get an "immediate" "reply" instead of having to time
> out waiting for a *real* reply that never arrives (to get into the gory
> details, the kernel can match the ICMP Port Unreachable message to your
> socket because it is connected, and delivers it as an error when you try
> to receive from the socket).
> 
> However this does not seem to work in passive mode with gen_udp (recv/3
> always blocks until Timeout), and since that is what inet_res uses, the
> purpose of the connect() is unclear. Responses from the wrong IP/Port
> are thrown away though, and enetunreach/econnrefused errors are handled,
> so I would guess that at some point this worked for passive mode too (or
> maybe it was just assumed to work:-) and *is* the purpose.
> 
> If gen_udp:connect/3 was documented, I think the non-working-as-above in
> passive mode should be considered a bug...
> 
> --Per Hedeland
> 
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
> 



More information about the erlang-questions mailing list