| tcp_stream - stream handle returned by OpenTCPStream or AcceptTCPStream |
|
Closes a TCP stream. The connection is shut down and the handle becomes invalid. The program at the other end sees the close as Eof returning 1 on its stream - the polite way to say goodbye, so send any parting message before you close. Open streams are closed automatically when the program ends, but a server that never closes finished client streams slowly collects dead connections - close them as soon as a client is done. See also: OpenTCPStream, AcceptTCPStream, CloseTCPServer, Eof. |
; CloseTCPStream Example
; ----------------------
; This program is server AND client at once: it listens on a
; port, then connects to itself over the loopback address.
port=4004
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
; Open a client connection to our own server
client=OpenTCPStream("127.0.0.1",port)
If client=0 Then
Print "Client: could not connect"
Else
Print "Client: connected to 127.0.0.1:"+port
; The server accepts the incoming connection - poll briefly,
; because AcceptTCPStream returns 0 until one has arrived
accepted=0
For try=1 To 100
accepted=AcceptTCPStream(server)
If accepted<>0 Then Exit
Delay 10
Next
If accepted=0 Then
Print "Server: no connection arrived"
Else
Print "Server: connection accepted"
WriteLine client,"One last message"
Print "Client: sent 'One last message'"
Print "Server: received '"+ReadLine$(accepted)+"'"
; Every opened or accepted TCP stream must be closed
; with CloseTCPStream when you are finished with it
CloseTCPStream accepted
Print "Server: closed its stream with CloseTCPStream"
EndIf
CloseTCPStream client
Print "Client: closed its stream with CloseTCPStream"
EndIf
CloseTCPServer server
Print "Server: closed"
EndIf
Print ""
Print "Press any key to close the example"
WaitKey
End