[Cuis-dev] finding an open port

Gerald Klix cuis.01 at klix.ch
Thu Oct 24 09:24:36 PDT 2024


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.
>
>



More information about the Cuis-dev mailing list