Legal Notice, Privacy Policy

Impressum, Datenschutzerklärung

Cerberus X Documentation

Class TcpStream

A tcp stream represents a synchronous tcp connection to a host on the internet. More...

Declarations

Extends
Stream Streams are used to read or write data in a sequential manner.
Constructors
New () Creates a new unconnected tcp stream.
New ( socket:Socket ) Creates a new tcp stream from an existing socket.
Methods
Connect : Bool ( host:String, port:Int ) Attempts to connect to the specified host and returns true if successful.
Inherited Properties
Eof : Int () Returns 1 if end-of-file has been reached, -1 if an IO error has occurred or 0 if the stream can be read.
Length : Int () Returns the stream length if the stream is seekable, else 0.
Position : Int () Returns the read/write position within the stream if the stream is seekable, else 0.
Inherited Methods
Close : Void () Closes the stream and released any OS resources in use.
Read : Int ( buffer:DataBuffer, offset:Int, count:Int ) Reads count bytes from the stream into databuffer, starting at address offset within the databuffer.
ReadAll : DataBuffer () Reads all remaining bytes from the stream into a databuffer.
ReadAll : Void ( buffer:DataBuffer, offset:Int, count:Int ) Reads count bytes from the stream into databuffer, starting at address offset within the databuffer.
ReadByte : Int () Reads an 8 bit byte from the stream.
ReadFloat : Float () Reads a 32 bit float value from the stream.
ReadInt : Int () Reads a 32 bit int value from the stream.
ReadShort : Int () Reads a 16 bit int value from the stream.
ReadString : String ( count:Int, encoding:String="utf8" ) Reads count bytes from the stream and converts them to a string using the given encoding.
ReadString : String ( encoding:String="utf8" ) Reads all bytes from the stream until end-of-file and converts them to a string using the given encoding.
Seek : Int ( position:Int ) If the stream is seekable, adjusts the read/write position within the stream and returns the new read/write position.
Write : Int ( buffer:DataBuffer, offset:Int, count:Int ) Writes count bytes from databuffer to the stream, starting at address offset within the databuffer.
WriteAll : Void ( buffer:DataBuffer, offset:Int, count:Int ) Writes count bytes from databuffer to the stream, starting at address offset within the databuffer.
WriteByte : Void ( value:Int ) Writes an 8 bit byte to the stream.
WriteFloat : Void ( value:Float ) Writes a 32 bit float value to the stream.
WriteInt : Void ( value:Int ) Writes a 32 bit int value to the stream.
WriteShort : Void ( value:Int ) Write a 16 bit int value to the stream.
WriteString : Void ( value:String, encoding:String="utf8" ) Writes a string to the stream using the given encoding.

Detailed Discussion

A tcp stream represents a synchronous tcp connection to a host on the internet.

IMPORTANT! Performing synchronous tcp I/O may cause your app to be rejected by some publishers, as it may result in your app 'blocking' during execution as it waits for incoming data. To prevent this, use a plain Socket to perform asynchronous I/O instead. It is strongly recommended that synchronous tcp streams are only used with the stdcpp target.

After creating a tcp stream, you should use the Connect method to make a connection to a remote host.

Once connected, you can read and write the stream using any of the Stream methods.

Tcp streams are only currently supported on the android, ios, win8, glfw and stdcpp targets.

Example
#If TARGET<>"glfw" And TARGET<>"android" And TARGET<>"ios" And TARGET<>"stdcpp"
#Error "Invalid target!"
#Endif

Import brl.tcpstream

Function Main()

Local stream:=New TcpStream

If Not stream.Connect( "www.cerberus-x.com",80 )
Print "Failed to connect!"
Return
Endif

Print "Connected!"

stream.WriteLine "GET / HTTP/1.0"
stream.WriteLine "Host: www.cerberus-x.com"
stream.WriteLine ""

While Not stream.Eof()
Local line:=stream.ReadLine()
Print line
Wend

stream.Close

Print "BYE!!!!"

End

Constructors Documentation

Method New ()

Creates a new unconnected tcp stream.

Method New ( socket:Socket )

Creates a new tcp stream from an existing socket. The socket must be a stream type socket.


Methods Documentation

Method Connect : Bool ( host:String, port:Int )

Attempts to connect to the specified host and returns true if successful.

Once connected, you can use stream read/write commands to communicate with the host.