Sending files

BlitzMax Forums/BlitzMax Programming/Sending files

ckob(Posted 2006) [#1]
Working on an app here and I wanna send files between the client and a server. The files will be anything from text files to media files. I found the following but it's to slow to use anyone have any ideas on how to speed this up? I dont wanna use ETNA so please dont suggest it :)


Client
mysocket:TSocket = CreateTCPSocket()
mysocketstream=CreateSocketStream(mysocket)
ConnectSocket(mysocket,HostIp("localhost"),8080)
mystream:TStream=ReadStream("max1.bmp")
CopyStream(mystream,mysocketstream)
CloseStream mystream
CloseStream mysocketstream
CloseSocket mysocket
Print "done"



Server
mysocket:TSocket=CreateTCPSocket()
BindSocket(mysocket,8080)
SocketListen(mysocket)
Repeat
  myreadsocket:TSocket=SocketAccept(mysocket) 
  If myreadsocket
    myreadsocketstream=CreateSocketStream(myreadsocket)
     mystream:TStream=WriteStream("test_out.bmp")
     CopyStream(myreadsocketstream,mystream)
     read=True
  EndIf
Until read=True
CloseStream(mystream)
CloseStream(myreadsocketstream)
CloseSocket(myreadsocket)
CloseSocket(mysocket)
Print "Done"


Code was posted by tonyg.


tonyg(Posted 2006) [#2]
That code should be quite quick.
How big is the file you're sending and how long does it take?


SculptureOfSoul(Posted 2006) [#3]
The default buffer size in CopyStream is 4096 bytes.
Might that have an effect on the speed of transmission? I'm not entirely clear how CopyStream actually works though (regarding the actual underlying sockets...does it just buffer those 4096 bytes and constantly try and send a packet of MSS (maximum segment size), and then when the buffer is sent it reads another 4096 bytes, etc etc)?


ckob(Posted 2006) [#4]
well the file I tested with was a 175kb jpg file it took about a minute to send locally.


ckob(Posted 2006) [#5]
hmm still cant seem to get this any faster


tonyg(Posted 2006) [#6]
This used to do megs in seconds. Are you sure it's the transfer as, to me, it looks like running the server and client on the same machine is fighting for CPU.
Tried adding a delay in the server code but it doesn't help much. Never used to happen though.
<edit> Try it with a delay 10 after the 'repeat' in the server code.


ckob(Posted 2006) [#7]
ok ill try the delay when I get home, but I also tried running it on two machines with the same results.


tonyg(Posted 2006) [#8]
...and use something other than 8080.


ckob(Posted 2006) [#9]
ok that worked adding the delay, I sent a 200 mb file in just over 1 sec from 1 pc to another on my LAN


Bremer(Posted 2006) [#10]
If using the code above, you transfer an image, then how do you go about saving it to the drive?


ckob(Posted 2006) [#11]
this part
mystream:TStream=WriteStream("test_out.bmp")
CopyStream(myreadsocketstream,mystream)

first your making the file then you copy the stream contents which would be the contents of the file your sending.


jtfrench(Posted 2008) [#12]
tonyg—

have you had any success in getting your file transfer to code to work across NATs? I'm currently working on a hole-punching technique for use with BlitzMax, and wanted to see if you (or anyone else) had tried this too.


tonyg(Posted 2008) [#13]
I only wrote the code originally to help somebody on BlitzCoder... 3 years ago.


jtfrench(Posted 2008) [#14]
You ever tried doing it across networks, or have you just never had the need to do so?


tonyg(Posted 2008) [#15]
Never needed to do it.
It really was a one-off to answer a specific question. I hadn't written anything network related before or since.