<div dir="ltr">Hi JD, <div style>I think you are maybe confusing reuseadrr or (SO_REUSEADDR) usage in general.</div><div style><br></div><div style>AFAIK, In most sockets implementations, you can't bind two sockets with the same exact source(port/IP)/protocol combination. </div>
<div style>That's not specific to Erlang, but to most used operating systems.</div><div style><br></div><div style><div>Here's a good read on the topic (specially the table for possible combination)<br></div><div>
<a href="http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t">http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t</a></div>
<div><br></div><div><br></div></div><div style>A simple example to show one scenario of using reuseaddr: </div><div style>%Listening on a specific IP and on a wildcard </div><div style><br></div><div style><div>> {ok, LSock1} = gen_tcp:listen(12345, [{reuseaddr, true},{ip,{127,0,0,1}}]).</div>
<div>{ok,#Port<0.816>}</div><div>> {ok, LSock2} = gen_tcp:listen(12345, [{reuseaddr, true},{ip,{0,0,0,0}}]). </div><div>{ok,#Port<0.817>}</div><div>> </div><div><br></div><div style>%Same without reuseaddr:</div>
<div style><br></div><div>> </div><div>> f().</div><div>ok</div><div>> {ok, LSock1} = gen_tcp:listen(12346, [{ip,{127,0,0,1}}]). </div><div>{ok,#Port<0.818>}</div><div>> {ok, LSock2} = gen_tcp:listen(12346, [{ip,{0,0,0,0}}]). </div>
<div>** exception error: no match of right hand side value {error,eaddrinuse}</div><div><br></div><div style>So in your case, sometimes it will succeed (if socket got actually closed before the next gen_tcp:listen call, and in other times will fail because the socket was not yet closed.</div>
<div style><br></div><div style>Does this answer your question?</div><div style><br></div><div style>Best Regards, </div><div style>Ahmed Omar</div><div style><br></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Thu, Sep 5, 2013 at 10:27 AM, JD Bothma <span dir="ltr"><<a href="mailto:jbothma@gmail.com" target="_blank">jbothma@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div class="im">On 5 September 2013 09:58, aman mangal <span dir="ltr"><<a href="mailto:mangalaman93@gmail.com" target="_blank">mangalaman93@gmail.com</a>></span> wrote:<br></div><div class="gmail_extra">
<div class="gmail_quote"><div class="im">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div><div dir="ltr">
<br></div><div dir="ltr">The error is thrown in the first line. It is returning <i>{error, </i><span style="font-family:arial,sans-serif;font-size:13px"><i>eaddrinuse}</i> (where you are expeciting <i>{ok, LSock}</i>) because the port is already being used by some different process. Two process cannot listen on the same port at a time.</span></div>
</div></div></div></blockquote><div><br></div></div><div> <span style="font-family:arial,sans-serif;font-size:13px">It runs for a while, and sometimes gets through all bind/closes, but sometimes crashes mid-way.</span></div>
<div style="font-family:arial,sans-serif;font-size:13px">
<br></div><div style="font-family:arial,sans-serif;font-size:13px">reuseaddr should be telling the OS that the closing socket may be reused when this process tries to listen on the same port again. </div><div style="font-family:arial,sans-serif;font-size:13px">
<br></div><div style="font-family:arial,sans-serif;font-size:13px">No process was listening on this port before the test. This code should be ensuring that the OS allows listening on the socket even when the last close hasn't completely released the socket. Without reuseaddr, we can sometimes expect eaddrinuse because close returns before the socket is guaranteed to be completely released.</div>
<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">That's if I haven't misunderstood something. If this code is correct, either something's happening on my machines that I'm not controlling properly or there's a synchronisation bug in the erlang networking code that I'd like to help identify and solve.</div>
<div class="im">
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div>
<div dir="ltr">
</div></div><div class="gmail_quote"><div><div>On Thu, Sep 5, 2013 at 1:16 PM, JD Bothma <span dir="ltr"><<a href="mailto:jbothma@gmail.com" target="_blank">jbothma@gmail.com</a>></span> wrote:<br></div></div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><div>
<div dir="ltr"><div>Why can this sometimes exit with exception error: no match of right hand side value {error,eaddrinuse} ?</div><div><br></div><div>As far as I know, nothing's connecting to the socket in this time.</div>
<div><br></div>[ begin {ok, LSock} = gen_tcp:listen(12345, [{reuseaddr, true}]),<div> {error, timeout} = gen_tcp:accept(LSock, 0),</div><div> ok = gen_tcp:close(LSock)</div><div> end </div><div> || _X <- lists:seq(1, 100000) ].<br>
</div><div><br></div><div>This is on R14 and 15 on linux and R16 on OS X.</div><div><br></div><div>BTW it doesn't seem to happen if I don't accept. I'm debugging this behaviour in yaws with SSL but it also seems to happen with plain gen_tcp.</div>
<span><font color="#888888">
<div><br></div><div>JD</div></font></span></div>
<br></div></div>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div></div>
</blockquote></div></div><br></div></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>