Results 1 to 2 of 2

Thread: [RESOLVED] sending files through sockets

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    198

    Resolved [RESOLVED] sending files through sockets

    Hi, I need to send a file through sockets connection. Right now, I'm able to send an image, wich is really an object with type of image, and then I use the Save method of Bitmap object to physically put the image file into the other pc. So, I'm not really sending the image file, but an image object that on other side becomes a file. I thought about one approach I could do to get it done. I read byte by byte of the file and put them into an array, serialize it and send it to the server. At server I deserialize the object but... how do I do to get file from that array of bytes?

    Any help will be really appreciate.
    Thanks.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    198

    Re: sending files through sockets

    Well I got it!!, these are the functions I made to do this:



    Public Function GetFileBytes(ByVal FileName As String) As Byte()

    Dim IOStream As New FileStream(FileName, FileMode.Open)
    Dim Length As Integer = IOStream.Length
    Dim BytesStream() As Byte = New Byte(Length) {}
    Dim BytesRead As Integer


    Do Until BytesRead >= Length

    BytesRead = IOStream.Read(BytesStream, 0, Length)

    BytesRead += BytesRead

    Loop

    IOStream.Close()

    Return BytesStream

    End Function

    Public Shared Sub SaveFile(ByVal Bytes() As Byte, ByVal SaveName As String)

    If Not Directory.Exists("C:\Archivos recibidos") Then

    Directory.CreateDirectory("C:\Archivos recibidos")

    End If

    Dim IOStream As New FileStream("C:\Archivos recibidos\" & SaveName, FileMode.Create)
    Dim Length As Integer = Bytes.Length

    Do Until IOStream.Position >= Length

    IOStream.Write(Bytes, 0, Length)

    Loop

    End Sub


    First function get the bytes of specific file. Second one, is done to write those bytes into a new filestream and get finally the physical file.

    I hope someone can find it useful at least.
    Regards.1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width