[Cuis-dev] finding an open port

Mark Volkmann r.mark.volkmann at gmail.com
Wed Oct 23 17:29:53 PDT 2024


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.

-- 
R. Mark Volkmann
Object Computing, Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20241023/43a180e4/attachment.htm>


More information about the Cuis-dev mailing list