tcp/ip exe transfer

Blitz3D Forums/Blitz3D Beginners Area/tcp/ip exe transfer

splinux(Posted 2005) [#1]
I tryed to transfer two file with the tcp/ip commands of Blitz3D.
I can't transfer an exacutable file from one computer to another.
In wich way can i do it?


GC-Martijn(Posted 2005) [#2]
something like this, g2g now

bla = readfile("sdfds.exe")

while not eof(bla)
   WriteByte tcp,readbyte(bla) 
wend




splinux(Posted 2005) [#3]
I tryed it, but it copy only 1 byte...

This is the code:

;Listener(receive file)

svrcom=CreateTCPServer(9015)

If svrcom<>0 Then
Print "Server started successfully."
Else
Print "Server failed to start."
End
End If

While Not KeyHit(1)
strstream=AcceptTCPStream(svrcom)
If strstream Then
Print ReadString$(strstream)
WaitKey()
End
Else
Print "Attempting messages..."
Delay 1000
End If
Wend

End


;Server(send file)
strmcom=OpenTCPStream("127.0.0.1",9015)

file = ReadFile("agent.exe")
fileout=ReadFile("agent2.exe")

If strmcom<>0 Then
Print "Client Connected successfully."
Else
Print "Server failed to connect."
WaitKey
End
End If


While Not Eof(file)
linea = ReadByte( file )
WriteByte(strmcom,linea)
Wend
CloseFile( file )
CloseFile(fileout)

Print "Completed sending message..."


GC-Martijn(Posted 2005) [#4]
Srry but I don't have the time today to write the code.

Check this (Code archives)
http://www.blitzbasic.com/codearcs/codearcs.php?code=487

Or "ftp"
http://www.blitzbasic.com/codearcs/codearcs.php?code=16


Strider Centaur(Posted 2005) [#5]
Are you sure the Server is not closeing the connection before the client reads the data?


splinux(Posted 2005) [#6]
I'm trying the file on the same computer.


BlackD(Posted 2005) [#7]
this is your client code:
svrcom=CreateTCPServer(9015) 

If svrcom<>0 Then 
Print "Server started successfully." 
Else 
Print "Server failed to start." 
End 
End If 

While Not KeyHit(1) 
strstream=AcceptTCPStream(svrcom) 
If strstream Then 
Print ReadString$(strstream) 
WaitKey() 
End 
Else 
Print "Attempting messages..." 
Delay 1000 
End If 
Wend 

End 

the problem is.. it's not creating a file.. its not saving any information. A TCP stream isn't like HTTP browsing a website.. you have to read and write every byte to a file manually..

First of all you have to have a handshake connection where the server tells the client that it's trying to send a file, and what it's name is - then the client has to open a new file under that name, and start reading each byte it receives to that file.

However - more specifically with your code (if you're just trying to display it to the screen), is that you've programmed it to end as soon as it recieves the first byte:
strstream=AcceptTCPStream(svrcom) 
If strstream Then 
Print ReadString$(strstream) 
WaitKey() 
End 


As soon as it gets a byte from the stream.. it ends. The server has to send EVERY byte in the file, one by one, from start to end, and the client has to recieve them in the same manner. The client needs to be passive, to constantly run and just accept whatever is sent to it. Otherwise it will never know when the end of the file is.

Suggestion: read the tcp example programs and learn how they work first. :) I think maybe you've got completely the wrong idea of network coding..

+BlackD


splinux(Posted 2005) [#8]
scuse me, that is the old code, the newest code write to a file, don't print in on the screen, but the problem is that it block as it write the first byte...


Damien Sturdy(Posted 2005) [#9]
And the new code is? ;)


splinux(Posted 2005) [#10]
Scuse me, i resolve the problem and i'll need no help, thanks to you all.


Gauge(Posted 2005) [#11]
oh well, heres my code:

Global stream
Global streamx
server=CreateTCPServer(4000) 

If server<>0 Then 
Print "Server started successfully." 
Else 
Print "Server failed to start." 
End 
End If 
Print "SERVER STARTED"
a=0

While a=0 
streamx=OpenTCPStream("localhost",4000)
stream=AcceptTCPStream(server)
Delay 500
If Not stream Print "NO STREAM 1"
If streamx Then
Print "Stream Connected sending file"
a=1
Else
Print "PAUSING"
Delay 500
Print "reconnecting"
EndIf
Wend

file=OpenFile("c:\agent.exe")
fileout=WriteFile("c:\newagent.exe")
a=0
Print "TRANSFER STARTED"

While Not Eof(file)
b=ReadByte(file)
WriteByte(stream,b)
WriteByte(fileout,b)
Cls
Text 10,10,a
a=a+1
Wend

CloseFile(file)
CloseFile(fileout)
CloseTCPStream(stream)
CloseTCPStream(streamx)
CloseTCPServer(server)
Print "DONE DONE DONE"
WaitKey()
End

WaitKey()
 



malicnite(Posted 2005) [#12]
wow there is alot of dead code in that last one. alot of globals that wasnt needed. it could be half of this, but thanks for the help.


DH(Posted 2005) [#13]
/me looks around for malicnite's coded example as an attempt to help rather than point out someone's mistake in a piece of code that probably took them 2 min to write up.

hmmm, can't seem to see it.


malicnite(Posted 2005) [#14]
hey watch it buddy


octothorpe(Posted 2005) [#15]
I believe the original problem was WriteByte() vs ReadString().