| tcp_stream - a TCP stream handle from OpenTCPStream or AcceptTCPStream |
|
Returns the IP address of the machine at the other end of a TCP stream. Every TCP stream is one end of a two-ended connection, and this command looks across to the far side. On a stream you opened with OpenTCPStream that is the server you connected to; on a stream handed to you by AcceptTCPStream it is the client who just joined. That second case is the everyday one for a game server: log who connected, show "player joined from ..." in the lobby, or match a reconnecting player to their earlier address. The value is a packed integer like every Blitz3D+ address - DottedIP turns it into readable "a.b.c.d" text. The address is captured when the connection is made and doesn't change over the stream's life. If it could not be determined at all, the command returns 0. TCPStreamPort completes the picture with the far end's port number; UDPMsgIP is the equivalent idea for connectionless UDP, where the "other end" can change with every packet. See also: TCPStreamPort, OpenTCPStream, AcceptTCPStream, DottedIP, UDPMsgIP. |
; TCPStreamIP Example
; -------------------
; This program is server AND client at once: it listens on a
; port, then connects to itself over the loopback address.
port=4006
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
client=OpenTCPStream("127.0.0.1",port)
If client=0 Then
Print "Client: could not connect"
Else
; 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
; TCPStreamIP returns the address of the OTHER end of
; the stream, as a packed integer for DottedIP
Print "Client's stream: talking to "+DottedIP$(TCPStreamIP(client))+" (the server)"
Print "Server's stream: talking to "+DottedIP$(TCPStreamIP(accepted))+" (the client)"
Print "Both report the loopback address, as expected here"
CloseTCPStream accepted
EndIf
CloseTCPStream client
EndIf
CloseTCPServer server
Print "Server: closed"
EndIf
Print ""
Print "Press any key to close the example"
WaitKey
End