| tcp_server - server handle returned by CreateTCPServer |
|
Closes a TCP server. The server stops listening and its port becomes free again. Be aware that closing a server also closes every stream that was accepted from it - connected clients are cut off mid-conversation - so if you only want to stop new arrivals while keeping current players connected, keep the server open and simply stop calling AcceptTCPStream. The handle is invalid afterwards. See also: CreateTCPServer, AcceptTCPStream, CloseTCPStream. |
; 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