🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Send a file using winsock (VB)

Started by
1 comment, last by -Fruz- 22 years, 4 months ago
I keep on struggling with this "custom" code of mine! Please help! This is what I did:
  
Private Function SEND(FileName As String)''Put the binary code of a file into a string
Dim FileData
Open FileName For Binary As #1 '' Opens the file for reading the binaries
FileData = Input(LOF(1), 1) '' Inserts the binaries into the variable
Close

'' This is my REAL problem
'' As you see it''s suposed to send chuncs of data. It sends (suposed to) 1000 binarys in one chunck
'' What is wrong???

Dim Data
Dim i As Byte
For i = 0 To Int(Len(FileData) / 1000) + 1 '' Sets how many "packages"
SENDFILE.SendData Mid(FileData, ((i * 1000) + 1), 1000) '' Suposed to send the chunck
DoEvents '' Finnish the send prosess
Next i '' Next chunck
End Function  
That was the send event. This is my "get" event:
  
Dim Data
GETFILE.GetData Data
FileData = FileData & Data
'' When this part is done, put the binaries into a file
Open FileName For Binary As #1 '' Opens the file (creates it)
Put #1, 1, FileData '' Put''s in the data
Close  
Can you help me? I don''t see at all why this doesn''t work! JFK
Advertisement
Data might need to be a bytearray (Dim Data() as byte)
and you;re sending it a s a string as well...
www.persistentrealities.com for Inline ASM for VB, VB Fibre, and other nice code samples in C++, PHP, ASP, etc.<br/>Play Yet Another Laser Game!<br/>
First of all, you''re doing a whole lot of things wrong here..

1.) You''re trying to read in the file as a string. Not good. Read it into a byte array, aka, an array of bytes.

2.) Why would you want to read in the entire file in one large Input statement, and then proceed to go into another time consuming for loop, just to send the data? Stream the data as you read it..meaning, read in 1000 bytes from the file at a time, each time a chunk is read in, send it, and keep doing it until you hit file.EOF. Otherwise, in large files, your program (and the recieving client) will have to wait until your computer reads in the entire file (which may take some time), and for it to then read that huge string of data and send it.

3.) Never use "as #1", in other words, never use a constant number. What if you want multiple file processes going on at once? Using your code, it will always use #1 for the file instance, which will cause collision and errors. Instead, use an integer set to freefile().

  Dim inFF as integerinFF = FreeFile()Open path for binary as #inFF    ..Close #inFF  

This topic is closed to new replies.

Advertisement