<div dir="ltr">I landed on a solution that seems to work fine. Does this seem reasonable?<div><br></div><div>When I pass this 3000 and port 3000 is in use, it returns 3001.<br><div><font face="arial, sans-serif"><br></font></div><div><font face="monospace">openPortFrom: aNumber<br> | hostAddress port socket |<br> <br></font><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><font face="monospace">hostAddress := NetNameResolver addressForName: 'localhost'.<br></font><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><font face="monospace">port := aNumber.<br></font><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><font face="monospace">socket := Socket newTCP.<br> <br></font><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><font face="monospace">[ true ] whileTrue: [<br></font><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">[</span></div><div><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">socket connectTo: hostAddress port: port waitForConnectionFor: 1.</span></div><div><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">socket disconnect.</span></div><div><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">port := port + 1.</span></div><div><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">] on: ConnectionTimedOut do: [ :ex | ^ port ].</span></div><div><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><font face="monospace">].</font><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 24, 2024 at 11:45 AM Jon Raiford via Cuis-dev <<a href="mailto:cuis-dev@lists.cuis.st">cuis-dev@lists.cuis.st</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div class="msg-5106429958001714919">
<div lang="EN-US" style="overflow-wrap: break-word;">
<div class="m_-5106429958001714919WordSection1">
<p class="MsoNormal"><span style="font-size:11pt">For what it’s worth, the way I looked at his code was that the “listenOn: port” should have thrown an RTE as the OS knows immediately that the port is not available. Of course Mark’s code below doesn’t handle
errors there, but that would have been resolved during debugging. Whether or not the rest of the code is sane is another matter entirely
</span><span style="font-size:11pt;font-family:"Apple Color Emoji"">😊</span><span style="font-size:11pt">.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">Jon<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<div id="m_-5106429958001714919mail-editor-reference-message-container">
<div>
<div>
<div style="border-width:1pt medium medium;border-style:solid none none;border-color:rgb(181,196,223) currentcolor currentcolor;padding:3pt 0in 0in">
<p class="MsoNormal" style="margin-bottom:12pt"><b><span style="color:black">From:
</span></b><span style="color:black">Cuis-dev <<a href="mailto:cuis-dev-bounces@lists.cuis.st" target="_blank">cuis-dev-bounces@lists.cuis.st</a>> on behalf of Gerald Klix via Cuis-dev <<a href="mailto:cuis-dev@lists.cuis.st" target="_blank">cuis-dev@lists.cuis.st</a>><br>
<b>Date: </b>Thursday, October 24, 2024 at 12:25</span><span style="font-family:Arial,sans-serif;color:black"> </span><span style="color:black">PM<br>
<b>To: </b><a href="mailto:cuis-dev@lists.cuis.st" target="_blank">cuis-dev@lists.cuis.st</a> <<a href="mailto:cuis-dev@lists.cuis.st" target="_blank">cuis-dev@lists.cuis.st</a>><br>
<b>Cc: </b>Gerald Klix <<a href="mailto:cuis.01@klix.ch" target="_blank">cuis.01@klix.ch</a>><br>
<b>Subject: </b>Re: [Cuis-dev] finding an open port<u></u><u></u></span></p>
</div>
<p class="MsoNormal">Dear Mark,<br>
<br>
why in name of Alan (Perlis) do you expect a connection to your newly<br>
created server socket pop out of nowhere?<br>
<br>
Increase the time out to 60 in `connected := socket <br>
waitForConnectionFor: 1 ifTimedOut: [ false ].`,<br>
send the #openPortFrom: message to your object.<br>
<br>
Switch to Terminal window, and start telnet like this<br>
`telnet <a href="http://127.0.0.1" target="_blank">
127.0.0.1</a> 3000` and marvel at the results.<br>
<br>
Sorry for my harsh words: This issue has nothing to do with Cuis,<br>
but a lot with socket, bind and listen.<br>
<br>
See man 2 bind, man 2 listen<br>
<br>
<br>
Just my 0,01€,<br>
<br>
Gerald<br>
<br>
<br>
<br>
On 10/24/24 2:29 AM, Mark Volkmann via Cuis-dev wrote:<br>
> I wrote the following code to take a port number and return the first port<br>
> starting from that number that is open. For example, if ports 3000 and 3001<br>
> are in use and I send "MyClass openPortFrom: 3000" then I expect it to<br>
> return 3002. Can you spot why this never finds an open port and loops<br>
> forever?<br>
><br>
> openPortFrom: aNumber<br>
> | connected port socket |<br>
><br>
> connected := false.<br>
> port := aNumber.<br>
><br>
> [ connected ] whileFalse: [<br>
> 'trying port {1}' format: { port } :: print.<br>
> socket := Socket newTCP.<br>
> socket listenOn: port.<br>
> connected := socket waitForConnectionFor: 1 ifTimedOut: [ false ].<br>
> connected ifTrue: [socket disconnect] ifFalse: [ port := port + 1 ].<br>
> ].<br>
><br>
> ^port.<br>
><br>
><br>
<br>
-- <br>
Cuis-dev mailing list<br>
<a href="mailto:Cuis-dev@lists.cuis.st" target="_blank">Cuis-dev@lists.cuis.st</a><br>
<a href="https://lists.cuis.st/mailman/listinfo/cuis-dev" target="_blank">https://lists.cuis.st/mailman/listinfo/cuis-dev</a><u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
-- <br>
Cuis-dev mailing list<br>
<a href="mailto:Cuis-dev@lists.cuis.st" target="_blank">Cuis-dev@lists.cuis.st</a><br>
<a href="https://lists.cuis.st/mailman/listinfo/cuis-dev" rel="noreferrer" target="_blank">https://lists.cuis.st/mailman/listinfo/cuis-dev</a><br>
</div></blockquote></div><br clear="all"><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><font face="arial, helvetica, sans-serif">R. Mark Volkmann</font></div><div><span style="font-size:12.8px"><font face="arial, helvetica, sans-serif">Object Computing, Inc.</font></span></div></div></div></div></div></div></div></div>