<div dir="ltr">I should have explained my use case. I'm using this in the setUp method for an SUnit test. It tests the endpoints of a web server. Previously the setUp method always started the server on a specific port. If that port was in use then the tests would fail. Now it always finds an available port and the tests pass regardless of whether specific ports are in use. If anyone is curious, you can see the code for the setUp and tearDown methods here: <a href="https://github.com/mvolkmann/Cuis-Smalltalk-WebClientPlus/blob/3eea948bc803aad814cc18932d7c57d45188b031/WebClientPlus.pck.st#L101">https://github.com/mvolkmann/Cuis-Smalltalk-WebClientPlus/blob/3eea948bc803aad814cc18932d7c57d45188b031/WebClientPlus.pck.st#L101</a></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Oct 25, 2024 at 8:30 AM Jon Raiford <<a href="mailto:raiford@labware.com">raiford@labware.com</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-5284243395195729685">
<div lang="EN-US" style="overflow-wrap: break-word;">
<div class="m_-5284243395195729685WordSection1">
<p class="MsoNormal"><span style="font-size:11pt">I think you are much better off grabbing the latest vm and using the listenOn: code with an exception handler. Connecting to your machine in a loop seems to be a backwards way to look for an unused port.<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_-5284243395195729685mail-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">Mark Volkmann <<a href="mailto:r.mark.volkmann@gmail.com" target="_blank">r.mark.volkmann@gmail.com</a>><br>
<b>Date: </b>Thursday, October 24, 2024 at 8:01</span><span style="font-family:Arial,sans-serif;color:black"> </span><span style="color:black">PM<br>
<b>To: </b>Discussion of Cuis Smalltalk <<a href="mailto:cuis-dev@lists.cuis.st" target="_blank">cuis-dev@lists.cuis.st</a>><br>
<b>Cc: </b>Jon Raiford <<a href="mailto:raiford@labware.com" target="_blank">raiford@labware.com</a>>, 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>
<div>
<p class="MsoNormal">I landed on a solution that seems to work fine. Does this seem reasonable?<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">When I pass this 3000 and port 3000 is in use, it returns 3001.<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">openPortFrom: aNumber<br>
| hostAddress port socket |<br>
<br>
<span class="m_-5284243395195729685gmail-apple-converted-space"> </span>hostAddress := NetNameResolver addressForName: 'localhost'.<br>
<span class="m_-5284243395195729685gmail-apple-converted-space"> </span>port := aNumber.<br>
<span class="m_-5284243395195729685gmail-apple-converted-space"> </span>socket := Socket newTCP.<br>
<br>
<span class="m_-5284243395195729685gmail-apple-converted-space"> </span>[ true ] whileTrue: [<br>
<span class="m_-5284243395195729685gmail-apple-converted-space"> </span> <span class="m_-5284243395195729685gmail-apple-converted-space"> </span>[</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New""> <span class="m_-5284243395195729685gmail-apple-converted-space"> </span> <span class="m_-5284243395195729685gmail-apple-converted-space"> </span> <span class="m_-5284243395195729685gmail-apple-converted-space"> </span>socket connectTo: hostAddress port: port
waitForConnectionFor: 1.</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New""> <span class="m_-5284243395195729685gmail-apple-converted-space"> </span> <span class="m_-5284243395195729685gmail-apple-converted-space"> </span> <span class="m_-5284243395195729685gmail-apple-converted-space"> </span>socket disconnect.</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New""> <span class="m_-5284243395195729685gmail-apple-converted-space"> </span> <span class="m_-5284243395195729685gmail-apple-converted-space"> </span> <span class="m_-5284243395195729685gmail-apple-converted-space"> </span>port := port + 1.</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New""> <span class="m_-5284243395195729685gmail-apple-converted-space"> </span> <span class="m_-5284243395195729685gmail-apple-converted-space"> </span>] on: ConnectionTimedOut do: [ :ex | ^ port ].</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New""> <span class="m_-5284243395195729685gmail-apple-converted-space"> </span>].</span><u></u><u></u></p>
</div>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Thu, Oct 24, 2024 at 11:45<span style="font-family:Arial,sans-serif"> </span>AM Jon Raiford via Cuis-dev <<a href="mailto:cuis-dev@lists.cuis.st" target="_blank">cuis-dev@lists.cuis.st</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-width:medium medium medium 1pt;border-style:none none none solid;border-color:currentcolor currentcolor currentcolor rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<div>
<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">.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt">Jon</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt"> </span><u></u><u></u></p>
<div id="m_-5284243395195729685m_-5106429958001714919mail-editor-reference-message-container">
<div>
<div>
<div style="border-width:1pt medium medium;border-style:solid none none;padding:3pt 0in 0in;border-color:currentcolor">
<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</span><u></u><u></u></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>
<p class="MsoNormal">-- <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>
</blockquote>
</div>
<p class="MsoNormal"><br clear="all">
<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal"><span class="m_-5284243395195729685gmailsignatureprefix">-- </span><u></u><u></u></p>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"><span style="font-family:Arial,sans-serif">R. Mark Volkmann</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt;font-family:Arial,sans-serif">Object Computing, Inc.</span><u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</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>