Sending files
BlitzMax Forums/BlitzMax Programming/Sending files
| ||
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. |
| ||
That code should be quite quick. How big is the file you're sending and how long does it take? |
| ||
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)? |
| ||
well the file I tested with was a 175kb jpg file it took about a minute to send locally. |
| ||
hmm still cant seem to get this any faster |
| ||
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. |
| ||
ok ill try the delay when I get home, but I also tried running it on two machines with the same results. |
| ||
...and use something other than 8080. |
| ||
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 |
| ||
If using the code above, you transfer an image, then how do you go about saving it to the drive? |
| ||
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. |
| ||
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. |
| ||
I only wrote the code originally to help somebody on BlitzCoder... 3 years ago. |
| ||
You ever tried doing it across networks, or have you just never had the need to do so? |
| ||
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. |