<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Aptos;
panose-1:2 11 0 4 2 2 2 2 2 4;}
@font-face
{font-family:"Apple Color Emoji";
panose-1:0 0 0 0 0 0 0 0 0 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:12.0pt;
font-family:"Aptos",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
span.m-5284243395195729685gmail-apple-converted-space
{mso-style-name:m_-5284243395195729685gmail-apple-converted-space;}
span.m-5284243395195729685gmailsignatureprefix
{mso-style-name:m_-5284243395195729685gmailsignatureprefix;}
span.gmailsignatureprefix
{mso-style-name:gmail_signature_prefix;}
span.EmailStyle21
{mso-style-type:personal-reply;
font-family:"Aptos",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;
mso-ligatures:none;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style>
</head>
<body lang="EN-US" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt">Yes, you are still trying to determine an available port by connecting to yourself and once you find a port that takes longer than a second to connect then you answer the port and then listen on it. This is
not guaranteed to work. Imagine if you chose a local port that was being used on an open connection but not set up for listening. Another possibility is a service that requires port knocking. The listening port could be tied up but not allow a connection to
succeed.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">The sockets support provided by the OS provides a mechanism to determine if a port is available or not. You should use that instead. Unfortunately the network plugin used by Cuis hides this call so you can’t
test for it directly. With the current vm though the listen primitive does throw an error if the port is already in use regardless if the port is used for listening or for another connection.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Jon<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<div id="mail-editor-reference-message-container">
<div>
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="margin-bottom:12.0pt"><b><span style="color:black">From:
</span></b><span style="color:black">Mark Volkmann <r.mark.volkmann@gmail.com><br>
<b>Date: </b>Friday, October 25, 2024 at 9:35</span><span style="font-family:"Arial",sans-serif;color:black"> </span><span style="color:black">AM<br>
<b>To: </b>Jon Raiford <raiford@labware.com><br>
<b>Cc: </b>Discussion of Cuis Smalltalk <cuis-dev@lists.cuis.st>, Gerald Klix <cuis.01@klix.ch><br>
<b>Subject: </b>Re: [Cuis-dev] finding an open port<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal">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><o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Fri, Oct 25, 2024 at 8:30<span style="font-family:"Arial",sans-serif"> </span>AM Jon Raiford <<a href="mailto:raiford@labware.com">raiford@labware.com</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt">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.</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt">Jon</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<div id="m_-5284243395195729685mail-editor-reference-message-container">
<div>
<div>
<div style="border:none;border-top:solid windowtext 1.0pt;padding:3.0pt 0in 0in 0in;border-color:currentcolor currentcolor">
<p class="MsoNormal" style="mso-margin-top-alt:auto;margin-bottom:12.0pt"><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</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">I landed on a solution that seems to work fine. Does this seem reasonable?<o:p></o:p></p>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">When I pass this 3000 and port 3000 is in use, it returns 3001.<o:p></o:p></p>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><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><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><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><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><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><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><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><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><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><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Courier New""> <span class="m-5284243395195729685gmail-apple-converted-space"> </span>].</span><o:p></o:p></p>
</div>
</div>
</div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">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:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt;border-color:currentcolor currentcolor currentcolor rgb(204,204,204)">
<div>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt">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:11.0pt;font-family:"Apple Color Emoji"">😊</span><span style="font-size:11.0pt">.</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt">Jon</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<div id="m_-5284243395195729685m_-5106429958001714919mail-editor-reference-message-container">
<div>
<div>
<div style="border:none;border-top:solid windowtext 1.0pt;padding:3.0pt 0in 0in 0in;border-color:currentcolor">
<p class="MsoNormal" style="mso-margin-top-alt:auto;margin-bottom:12.0pt"><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><o:p></o:p></p>
</div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">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><o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">--
<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><o:p></o:p></p>
</div>
</blockquote>
</div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><br clear="all">
<o:p></o:p></p>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
</div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span class="m-5284243395195729685gmailsignatureprefix">--
</span><o:p></o:p></p>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-family:"Arial",sans-serif">R. Mark Volkmann</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:9.5pt;font-family:"Arial",sans-serif">Object Computing, Inc.</span><o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"><br clear="all">
<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal"><span class="gmailsignatureprefix">-- </span><o:p></o:p></p>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"><span style="font-family:"Arial",sans-serif">R. Mark Volkmann</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt;font-family:"Arial",sans-serif">Object Computing, Inc.</span><o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>