Well the serialize method appears to work, but only on smaller files.
When I try sending a file of about 100kb in size, I get an error "End of Stream encountered before parsing was completed" during the DeserializeData() on the line containing:
o = DirectCast(bf.Deserialize(mem), SendObject)
Could this be because the data is not arriving all at once in PollListen()? I tested the whole serialize/deserialize procedure directly in the form_load event instead of sockets, which does work with any size file.
[edit] Using a file with a size of 119,877 bytes, I checked the size of the byte array during the timeline of operations and came up with:
During the SerializeData() method: 240126
During the Send() method (code I did not post): 240126
m_ListenSocket.Available: 8192
After the m_ListenSocket.Receive() method: 8192
During the DeserializeData() method (before the error): 8192
In another test with a smaller sized file I came up with:
During the SerializeData() method: 13560
During the Send() method (code I did not post): 13560
m_ListenSocket.Available: 8192
After the m_ListenSocket.Receive() method: 8192
During the DeserializeData() method (before the error): 8192
A SendObject containing this file deserializes ok and I am able to get accurate data from the object.
' PollListen
' Listens for a client and receives data.
Sub PollListen()
Try
If m_Listener.Pending = True Then
' create the socket
m_ListenSocket = m_Listener.AcceptSocket
' check if the socket contains data
If m_ListenSocket.Poll(1, SelectMode.SelectRead) = True Then
' create a byte array for the socket data
Dim bData(m_ListenSocket.Available - 1) As Byte
' fill the byte array with the socket data
m_ListenSocket.Receive(bData)
' recreate the object
Dim o As SendObject = DeserializeData(bData)
End If
End If
Catch ex As Exception
Console.WriteLine(ex.Message)
RaiseEvent ErrorOccurred(ex)
End Try
End Sub
' SerializeData
' Serializes the passed object into a byte array.
Function SerializeData(ByVal obj As SendObject) As Byte()
Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim mem As New IO.MemoryStream
' serialize this object
bf.Serialize(mem, obj)
' return the serialized memory
Dim b() As Byte = mem.GetBuffer
mem.Close()
' return the serialized bytes
Return b
End Function
' DeserializeData
' Deserializes the passed Bytes() and casts it to a SendObject.
Function DeserializeData(ByVal Bytes() As Byte) As SendObject
Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim mem As New IO.MemoryStream()
' write the passed Bytes() to the memory stream
mem.Write(Bytes, 0, Bytes.Length - 1)
' seek to beginning of memory stream
mem.Seek(0, IO.SeekOrigin.Begin)
' deserialize the memory stream and cast it to a SendObject
Dim o As SendObject
o = DirectCast(bf.Deserialize(mem), SendObject)
mem.Close()
' return deserialized object
Return o
End Function
<Serializable()> _
Class SendObject
Public Sender As String
Public FileName As String
Public Data() As Byte
Sub SetFile(ByVal Path As String)
Dim oFile As New IO.FileInfo(Path)
FileName = oFile.Name
Data = IO.File.ReadAllBytes(Path)
End Sub
End Class