' This is a sample showing how to use a socket
' using ScriptBasic
'

function waitforline(timeout, fileno)
local ch, n, myline

while waitevents(timeout) = fileno
	ch = input(1, fileno)
	if asc(ch) <> 10 then
		if asc(ch) = 13 then
			waitforline = myline
			exit sub
		endif
		myline = myline & ch
	endif
wend

waitforline = ""

end function




if command() = "" then
	print "\nPlease specify a destination in the form IPAddress:Portno\n"
	exit sub
endif

on error goto errorlabel

open command() for socket as 1
print "successfully opened socket\n"

' clear the event table
dummy = setevent(0)
' registers that we want to check for received data on descriptor 1
dummy = setevent(1)

myloop:
	print #1, "hello, please type a line\r\n"
	a = waitforline(10000, 1)
	if a <> "" then
		print "received line: ", a, "\n"
	else
		goto finished
	endif
	if a = "bye" or a like "bye*" then
		goto finished
	endif

goto myloop

finished:
close 1
print "Finished\n"
stop

errorlabel:
print "encountered an error opening the socket\n"
print error$, "\n"
