Send a file across the internet...

decrypt

Junior Contributor
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 isn't a problem.

I'm not sure about the exact code, but all you have to do is send the data read from the image's file, same as you'd read from a text file and send it over the web. The socket listner on the other guy's 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:

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

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

Visual Basic:
 '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:
I need some help with this, I know it's 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 doesn't work what exactly isn't 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
Visual Basic:
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 (I'm guessing on this one but I can't 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 shouldn't 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 it's just that when i converted it back to a .gif file on the other computer, it wasn't 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 it's on the other side of the server.
 
Was bored so thought I'd 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.
 
Back
Top