; CloseTCPServer Example ; ---------------------- ; This program is server AND client at once: it listens on a ; port, connects to itself, then closes the server and shows ; that new connections are refused afterwards. port=4005 server=CreateTCPServer(port) If server=0 Then Print "Could not listen on port "+port+" (it may be in use)" Else Print "Server: listening on port "+port ; Prove the server works: connect to it once client=OpenTCPStream("127.0.0.1",port) If client<>0 Then Print "Client: connected - the server is alive" CloseTCPStream client Else Print "Client: could not connect" EndIf ; Shut the server down CloseTCPServer server Print "Server: closed with CloseTCPServer" ; A new connection attempt now fails - nobody is listening again=OpenTCPStream("127.0.0.1",port) If again=0 Then Print "Client: reconnect failed - port "+port+" is no longer open" Else Print "Client: unexpectedly reconnected" CloseTCPStream again EndIf EndIf Print "" Print "Press any key to close the example" WaitKey End