Kolah 11-17-2001, 09:38 AM Hail!
i found a lil chat program that connect throught ip, what i wanna do is connect to ip and read a file from the computer im connected to, is there any way to do that ? thanks
dont worry, its not a hacker program im doing hehe
ChiefRedBull 11-17-2001, 01:50 PM You will need two programs, one on each computer. Then send commands between them via winsock. Try that, and then i can explain file transfer.
Kolah 11-17-2001, 06:05 PM yeah the program i got, i simply have to put the prog on 2 diff comp and chat, also using winsock, its just the file transfer that im trying to do
Flyguy 11-18-2001, 04:52 AM It doesn't matter whether you are sending text (for chatting) or other characters (binairy stream), the priciple of transfering data is the same.
You send data and you receive data, you just have to define a protocol somehow to indicate the number of bytes you're sending the filename etc.
ChiefRedBull 11-18-2001, 07:54 AM So how far have you got? are you sending strings successfully?
If so, file transfer is very similar, you have to open the file, read it into chunks, and send them one at a time, reassemble them at the other end, and bingo!
Kolah 11-18-2001, 08:08 AM yeah i can chat easily, sending string from a textbox works good.
all i have to do is read the file and send line by line to the other program ? hehe sounds very easy :p
ChiefRedBull 11-18-2001, 01:39 PM Right, sending files is easy, only a couple of things to mention.
You will need to use some commands that can be easily distnguisehed from ordinary chat strings - for example "<font color=blue>**//BEGINFILE//**</font color=blue>" would signify, wait for it, the beginning of a file. Easy.
To send the file, you will need to
<pre><font color=red> 1.Send a begin file notification
2.Send the file in chunks of about 5k
3.Receive the chunks and store them in memory
4.Send an end of file notification
5.Reassemble the chunks in memory and write to disk
</font color=red>
</pre>
Its not that easy, so if you get stuck, just ask images/icons/wink.gif
Banjo 11-18-2001, 02:53 PM On additional thing you will need to done. Byte Stuffing. Basically, if the file itself contains the end of file marker then the transfer will be aborted prematurely. What you need to do is search the file data for the end marker and replace it with two end markers. Then at the receiving end search for two end markers together. If you find one then make it a single marker. Only terminate the transfer if there is a single marker before you delete the duplicates.
Kolah 11-18-2001, 04:41 PM **** lol
never thought it would be that hard hehe
my boss asked me to do this in 1 day, gonna be hard
anyway, thanks alot guys, ill try to work on that
Kolah 11-18-2001, 05:02 PM woudnt it be easier to just transfer the file into a richtextbox and send the text in the richtextbox ?
hehe im really dont know how ill do that and my boss wont be happy to know that i didnt made it
and i cant hardly understand the chat prog i got, was working good at work but now its complete crap at home hehe
robot313 11-18-2001, 05:41 PM If it's just text then that is real easy. Just read the text file into a variable, then send the variable.
ChiefRedBull 11-19-2001, 05:31 AM If youre having trouble understanding the sending of strings, download my Winsock tutorial in the Code Library here (http://www.visualbasicforum.com/bbs/showflat.php?Cat=&Board=CodeLib&Number=53985&page=0&view=collapsed&sb= 5&o=186&fpart=). It explains the usage of winsock, and the theory behind sending strings, as well as coming complete with a working program.
Once youve mastered (or at least understand) this process, then sending files is one step closer.
Transferring files is the same however you display them at either end. Whether you display them in a richtextbox or not, you still have to send the binary chunk by chunk. When sending small chat strings, winsock takes care of the chunking for you, but for files you have to do it yourself.
Perhaps if you were sending textfiles you could open it, read the file into a string, then send that, and as long as it wasn't too big (ie more than 10K) it should work ok.... bear in mind though you might lose some data sending this way.
divil 11-30-2001, 05:47 AM If you want to send files properly, your protocol should negotiate the filesize first, so the receiving end knows when the file is finished. This eliminates the need for EOF characters and the trouble they can cause.
Also, don't assemble the file in memory, having a 100mb string takes quite a lot of resources! I'd just stream the output to a file, unless you absolutely can't do that.
Agent 03-18-2002, 02:12 PM I have a question concerning the following:
To send the file, you will need to:
1.Send a begin file notification
2.Send the file in chunks of about 5k
3.Receive the chunks and store them in memory
4.Send an end of file notification
5.Reassemble the chunks in memory and write to diskThe number 2 one, I don't understand what you mean (and everybody else!) by chunks. I think it would be easy to split the file into a array and send it like that. How would I do that and put about 5k or so in an array element. Or am I wrong all together?
I have used some file transfer code off of a link off of vb-world but when I tried to send like larger filesizes, it messed up. Any help on that and my chunky problem?
ChiefRedBull 03-18-2002, 02:17 PM Chunk: small piece of a larger object. So yes, a 'chunk' means about 5k or so of a file.
And yes, splitting the file into an array is a good idea, although isn't if the file is too large, because it means the whole lots gotta be stored in memory.
To read a particular bit of a file, do this:
Dim b() As Byte
Open FileName For Binary As #FF
Redim b(2048)
Get#, , b()
Close #FFThis will read 2KB of data.
Sending large files via VB is complex because you have to have some sort of protocol in place to make sure no pieces are missed or corrupted during transfer.
Agent 03-18-2002, 02:45 PM although isn't if the file is too large, because it means the whole lots gotta be stored in memory.How big is a large file? I would say that a 80 MB file is large but on a 56k connection, 10MB would be large.
Dim b() As Byte
Open FileName For Binary As #FF ' That is a freefile number right?
Redim b(2048)
Get#, , b() ' How would that all go nicely into the array?
Close #FFSending large files via VB is complex because you have to have some sort of protocol in place to make sure no pieces are missed or corrupted during transfer. Do you mean some sort of handshaking routine to say "Yo, I got that chunk of data. Send me some more."? The thing is, I have never seen that where it goes into the dynamic array without the Redim Preserve. Also, what kind of problems with memory are we talking about and how can I be prepared for them? Thanks alot.
ChiefRedBull 03-19-2002, 02:38 PM IMHO a large file is anything over about 1MB for a 56k connection. It would be necessary to split it up, send it bit by bit, an wouldn't be a good idea to hold it all in memory at once.
Yes #FF is your freefile number. I usually get the FreeFile number using the FreeFil function. Like this:
FF = FreeFile
Astounding eh? :D
It would go nicely into the array, because the Get command will fill the passed buffer. By using the ReDim command on the array, we've made a buffer that is 2048 characters long. This is filled from our file. with one character in each byte. (A byte is an integer value between 0 and 255, thus nicely representing all possible ASCII chars, even binary ones)
So, you've now got an array of 2048 bytes or characters from your file, which you can send down the winsock.
Yes you want a kind of protocol that says exactly that. For large files, you'll want to do some sort of checking as well, for example, send the size of the file first. Then, when its finished, check the size that you've received is that same as you expected to receive.
And yes, for each chunk, check the size is ok, and its the correct chunk etc..
I would advise having your protocol format chunks something like this:
____________________
+ +
+ chunk size +
+ chunk number +
____________________
+ +
+ +
+ +
+ chunk data +
+ +
+ +
+ +
____________________
Using ReDim is just to change the number of elements in an array. For example
Dim myArray() As String
Private Sub Form_Load()
ReDim myArray(0 To 2)
' myarray now contains 3 elements
' set the elements
myArray(0) = "hello"
myArray(1) = "chief"
myArray(2) = "boo!"
' if I were now to resize the array like this:
' ReDim myArray(0 To 1)
' the array would now only have two elements
' but I'd lose all the data that was stored in there
' so you use the Preserve keyword, to save the data
ReDim Preserve myArray(0 To 1)
' myArray(0) would still be "hello"
' myArray(1) would still be "chief"
As for memory problems.. if you loaded a whole file of 1MB into an array at once, that would mean you'd have 1MB of memory used up. Thats quite a lot.... so you'd only want to load each chunk from the file into memory when you were about to send it.
Phew.... :p
Agent 03-19-2002, 06:43 PM I have already known about/used the freefile function and the ReDim Preserve "array-setters" before.
But, I was thinking about when inputting the file, getting like the length of the file (LOF) and where I was at in the file (?). Is there a way I can input just a certain portion of a file? Lets say I want to start input at character 40 and end at character 80. I think this is possible but I forgot;) Is this a good step?
I would then send it like this:
FileLocation----Data
What am I missing here? 'Preciate the help.
divil 03-20-2002, 02:59 AM You can use Seek to navigate to a given offset in a file.
Agent 03-20-2002, 08:46 AM I can't find a link that holds the key to a site that tells me how to use seek in conjunction with any file inputting. Any help = nice.
ChiefRedBull 03-20-2002, 08:53 AM Theres no other use of Seek than file I/O. Type Seek into your VB IDE and press F1. All the help you need.
Thinker 03-20-2002, 08:54 AM Seek only works when the file is open for random or binary. For
binary, it is the absolute byte position in the file. Seek #1, 500
positions to byte 500 for the next access.
|