Tcp monkey starter code

Community Forums/Monkey Talk/Tcp monkey starter code

Gauge(Posted February) [#1]
Hey i'm using the free version of monkey. However i cannot post on the monkey forums because I haven't bought anything. Well i'd like to play around a little and decide if I do want to buy this. However, I would like some starter code as the helpfiles are not very well defined, and the tutorials are lacking. Ie there appeas to be no readline, or if read available tcp documentation. So can anyone fill this in for me? I copied this off the monkey sight.

I'd like this just to check if the readavails from a stream and print it for now. If you can hep with that I think i can do the rest. thank you.


#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.dsl-mud.org",4000 )
Print "Failed to connect!" Return
Endif

Print "Connected!"

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

;just need a if readavail readline here


Print line
Wend

stream.Close

Print "BYE!!!!"

End


skidracer(Posted February) [#2]
Ie there appears to be no readline,


There is a Readline, and it's use is demonstrated in the tcpstream docs.

#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.monkeycoder.co.nz",80 )
		Print "Failed to connect!"
		Return
	Endif
	
	Print "Connected!"
	
	stream.WriteLine "GET / HTTP/1.0"
	stream.WriteLine "Host: www.monkeycoder.co.nz"
	stream.WriteLine ""
	
	While Not stream.Eof()
		Local line:=stream.ReadLine()
		Print line
	Wend
	
	stream.Close
	
	Print "BYE!!!!"

End



There is no ReadAvail and it could be argued such a command would not help you detect if a full line of data is available.

There is a ReadAll command that you could use to fill your own line buffer without blocking.

Also the bananas/mak/echoserver_tcp demonstrates some async features that are supported by TCPStream.

Interestingly the monkey2 Stream class supports a more useful Length function which is effectively a ReadAvail implementation.


In the case of non-seekable streams, Length is the number of bytes that can be read from the stream without 'blocking'.



as opposed to monkey1


Returns the stream length if the stream is seekable, else 0.