[Cuis-dev] finding an open port
Gerald Klix
cuis.01 at klix.ch
Fri Oct 25 02:29:25 PDT 2024
Hi Mark,
Now you have written a scanner for open ports listening
on 127.0.0.1.
Since I do not know, whether you want
to find an unbound port for a server socket
or want to know which ports listen on localhost,
I can't tell you whether your solution is reasonable.
My advise is make a digression to C programming
to learn the semantics of sockets.
https://www.geeksforgeeks.org/socket-programming-cc/
was the first example that duckduckgo found.
To me these a reasonable examples.
As far as I understand the squeak VM uses
asynchronous sockets, which interact
with your Cuis image via semaphores.
You can look at the socket plugins source here:
https://github.com/OpenSmalltalk/opensmalltalk-vm/blob/Cog/src/plugins/SocketPlugin/SocketPlugin.c
Please do not ask me for details, because I do not
understand them either. Looking at this
plugin's code reminds me of
this XKCD cartoon:
https://commadot.com/wtf-per-minute/
Alas I am not sure in which of the two rooms we are sitting.
Sorry,
Gerald
On 10/25/24 2:01 AM, Mark Volkmann via Cuis-dev wrote:
> I landed on a solution that seems to work fine. Does this seem reasonable?
>
> When I pass this 3000 and port 3000 is in use, it returns 3001.
>
> openPortFrom: aNumber
> | hostAddress port socket |
>
> hostAddress := NetNameResolver addressForName: 'localhost'.
> port := aNumber.
> socket := Socket newTCP.
>
> [ true ] whileTrue: [
> [
> socket connectTo: hostAddress port: port waitForConnectionFor:
> 1.
> socket disconnect.
> port := port + 1.
> ] on: ConnectionTimedOut do: [ :ex | ^ port ].
> ].
>
> On Thu, Oct 24, 2024 at 11:45 AM Jon Raiford via Cuis-dev <
> cuis-dev at lists.cuis.st> wrote:
>
>> 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 😊.
>>
>>
>>
>> Jon
>>
>>
>>
>> *From: *Cuis-dev <cuis-dev-bounces at lists.cuis.st> on behalf of Gerald
>> Klix via Cuis-dev <cuis-dev at lists.cuis.st>
>> *Date: *Thursday, October 24, 2024 at 12:25 PM
>> *To: *cuis-dev at lists.cuis.st <cuis-dev at lists.cuis.st>
>> *Cc: *Gerald Klix <cuis.01 at klix.ch>
>> *Subject: *Re: [Cuis-dev] finding an open port
>>
>> Dear Mark,
>>
>> why in name of Alan (Perlis) do you expect a connection to your newly
>> created server socket pop out of nowhere?
>>
>> Increase the time out to 60 in `connected := socket
>> waitForConnectionFor: 1 ifTimedOut: [ false ].`,
>> send the #openPortFrom: message to your object.
>>
>> Switch to Terminal window, and start telnet like this
>> `telnet 127.0.0.1 3000` and marvel at the results.
>>
>> Sorry for my harsh words: This issue has nothing to do with Cuis,
>> but a lot with socket, bind and listen.
>>
>> See man 2 bind, man 2 listen
>>
>>
>> Just my 0,01€,
>>
>> Gerald
>>
>>
>>
>> On 10/24/24 2:29 AM, Mark Volkmann via Cuis-dev wrote:
>>> I wrote the following code to take a port number and return the first
>> port
>>> starting from that number that is open. For example, if ports 3000 and
>> 3001
>>> are in use and I send "MyClass openPortFrom: 3000" then I expect it to
>>> return 3002. Can you spot why this never finds an open port and loops
>>> forever?
>>>
>>> openPortFrom: aNumber
>>> | connected port socket |
>>>
>>> connected := false.
>>> port := aNumber.
>>>
>>> [ connected ] whileFalse: [
>>> 'trying port {1}' format: { port } :: print.
>>> socket := Socket newTCP.
>>> socket listenOn: port.
>>> connected := socket waitForConnectionFor: 1 ifTimedOut: [ false ].
>>> connected ifTrue: [socket disconnect] ifFalse: [ port := port + 1 ].
>>> ].
>>>
>>> ^port.
>>>
>>>
>> --
>> Cuis-dev mailing list
>> Cuis-dev at lists.cuis.st
>> https://lists.cuis.st/mailman/listinfo/cuis-dev
>> --
>> Cuis-dev mailing list
>> Cuis-dev at lists.cuis.st
>> https://lists.cuis.st/mailman/listinfo/cuis-dev
>>
>
>
More information about the Cuis-dev
mailing list