Send a file across the internet...

decrypt

Well-known member
Joined
Oct 6, 2003
Messages
216
Using microsofts example of the system.net.sockets (the chat program), how could you use it to send files accross the internet? Or have it so that when a user clicks a button it takes a screen shot saves it to a folder, and then sends it to the other person, once sent it deletes the file, then the other person displays it and then the program deletes it after. I know how to do everything but the picture sending. How would you do this?
 
Since sending text (as in a chat) is the same as sending any data, it isnt a problem.

Im not sure about the exact code, but all you have to do is send the data read from the images file, same as youd read from a text file and send it over the web. The socket listner on the other guys computer will write all the data recieved into one file, which would then be no different from the origination image file.
 
Do you know how to rename a file to a .txt?

(so what i have to do is rename the file to a .txt, and then send the data in the textfile to the other person, the other person will rename it to a .jpeg, or whatever, and then view it.)
 
Thanks, that helped me alot all i did after i found out to use system.io.file was this:

Code:
 Dim filee as system.io.file
fileee.Move("Image.gif", "Image.txt")
fileee.copy("i.gif", "i.txt") also works...

Now im having a bit of trouble getting the text out here is what im doing (it says it cant be converted to a string:
Code:
 Dim textedimage As String
textedimage = file.openread("Image.txt")
 
Last edited by a moderator:
How can you place a .txt file into a textbox? (once im done this the application should work :))
 
Its kind of weird can someone tell me why this is not working? Here is the code:

Code:
 client side
            fileee.Copy("D:\Image.gif", "D:\Image.txt")
            read the file and save it to a string
            Dim sr As System.IO.StreamReader ****
            sr = System.IO.File.OpenText("D:\Image.txt")
            Dim mNameArray As String read into an string
            Dim x As String
            x = sr.Read()
            mNameArray = x
            sr.Close()
            SendData("PICTURE|" & x) sends the data to the host

server side (located in the OnLineReceived Sub)
            Case "PICTURE"
                SendToClients("PIC|" & dataArray(1), sender)
                UpdateStatus(dataArray(1))

now back to the client (under the proccess comands sub...)

            Case "PIC"
                Dim oFile As System.IO.File
                Dim oWrite As System.IO.StreamWriter
                oWrite = oFile.CreateText("D:\DavidBackup\Vb.net Projects\Helper\Client\Images\Recieved.txt")
                oWrite.Write(dataArray(1))
                fileee.Copy("D:\DavidBackup\Vb.net Projects\Helper\Client\Images\Recieved.txt", "D:\DavidBackup\Vb.net Projects\Helper\Client\Images\Recieved.gif")
                PictureBox1.Image.FromFile("D:\DavidBackup\Vb.net Projects\Helper\Client\Images\Recieved.gif")
I think the problem has to be somewhere around where i put the ****

It has something to do with the reading the text...
 
Last edited by a moderator:
I need some help with this, I know its possible. Is there an example on the internet somewhere? I just need to know how to send an image across the internet that will be displayed...
 
Firstly when you say it doesnt work what exactly isnt working - the more informnation you can give then people are more likely to be able to resolve the issue and respond.

in the bit of code
Code:
Dim mNameArray As String read into an string

            Dim x As String

            x = sr.Read()

            mNameArray = x

            sr.Close()
what are you trying to do anyway? sr.Read only reads the next character from the stream - not the entire file, you should use sr.ReadToEnd if that is what you are trying to do.
If you are trying to send an image (Im guessing on this one but I cant see any reason why you would copy a gif to a file with a .txt extension - it would still be a gif and not contain any text) then you really shouldnt be using StreamReader as this inherits from TextReader and is designed to read text files.
you may be better of either using a BinaryReader of possibly just using a stream directly and reading the image into a Byte array and sending that.
You should have a look in either MSDN or the framework SDK - they both contain samples on reading and writing files as well as samples on using network streams.
 
thanks, I think a binary reader would work better, because i got everything working its just that when i converted it back to a .gif file on the other computer, it wasnt an image... Anyways i think a binary reader would be better to send accross the internet, how could i read the binary of a .gif file, and then write it once its on the other side of the server.
 
Was bored so thought Id try knocking up a simple example - then got carried away :)
anyway have a look here for a simple client / server file transfer application.
code is fairly commented and you should be able to cut and paste the relevant bits.
On the sending side all the code is behind the start button anyway.

On the server side you can probably nick the Download class and the listener class and most of the code behind the listen button as well.
 
Thanks alot :D

edit: everything works great :) Thanks alot! :)
 
Last edited by a moderator:
Back
Top