[RESOLVED] Serializing serveral objects through NetworkStream
I am currently building two applications a client and a server, where several objects have to be sent between the applications, a couple objects each way.
The applications are called "ACME Server" and "ACME Client" (yay for originality).
While I can get the first object to be transferred from the client to the server, but I can't get the second object to be transferred successfully from the server to the client, the error is occurring in the client program, so something is being sent but it isn't appropriate.
I have taken a printscreen of the error that I am receiving, which I have uploaded to http://img297.imageshack.us/img297/6602/acmeclient.jpg
All objects that are being sent are serializable (well there is an Interface called Action which I am unable to make serializable I believe due to it being an Interface).
The code that I have is this...
ACME Server
Code:
Try
Dim nStream As NetworkStream = clientSocket.GetStream
Dim comp As ComputerSpecs.Computer
Dim results As List(Of Result)
Dim serializer As New BinaryFormatter()
Dim actions As New List(Of Action)
If nStream.CanRead And nStream.CanWrite Then
'Get the client computers details
comp = CType(serializer.Deserialize(nStream), ComputerSpecs.Computer)
logComputerSpecs(comp)
'Send the client the actions to be carried out
actions = getActions() <--Error line
serializer.Serialize(nStream, actions)
'we now wait for the response to the actions
results = CType(serializer.Deserialize(nStream), List(Of Result))
logActionResults(results)
'Finally we send the list of final actions to be carried out
actions = getFinalActions()
serializer.Serialize(nStream, actions)
nStream.Close()
clientSocket.Close()
Else
ErrorLogger.logError("Unable to create full stream", "ComputerSocket.doChat")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
ACME Client
Code:
Dim nStream As NetworkStream
Dim comp As ComputerSpecs.Computer
Dim serializer As New BinaryFormatter()
Dim actions As New List(Of Action)
Dim results As New List(Of Result)
Try
tcpClient.Connect(getServerAddress, getServerPort)
nStream = tcpClient.GetStream
If nStream.CanWrite And nStream.CanRead Then
'Send the computer stats to the ACME Server
comp = New ComputerSpecs.Computer(True)
serializer.Serialize(nStream, comp)
'receive the actions to be performed
actions = CType(serializer.Deserialize(nStream), List(Of Action)) <--Error line
'perform the actions
results = performActions(actions)
'send the results back
serializer.Serialize(nStream, results)
'receive the final actions to be performed
actions = CType(serializer.Deserialize(nStream), List(Of Action))
'close the connection prior to performing the actions
nStream.Close()
tcpClient.Close()
'perform the final actions
performActions(actions)
Else
If Not nStream.CanWrite Then
Console.WriteLine("Cannot write data to this stream")
tcpClient.Close()
Else
Console.WriteLine("Cannot read data from this stream")
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
I have marked the lines where the error is occurring with "<--Error line" to try and help people to identify where the problem is occurring.
If any more information is required then let me know and I shall provide it, thank you for any help in advance (or even for reading the whole of this essay).
Satal
Re: Serializing serveral objects through NetworkStream
While I hate bumping, I am still having a problem with this and would welcome any input on how I might be able to fix this problem or be able to narrow down what would be causing the problem.
Re: Serializing serveral objects through NetworkStream
Hi all,
I thought that I would report back to say that I have figured out what the problem was.
The problem was that the Actions needed to be in a class library as originally they were just exact copies of the same classes in the project, this meant that the namespace wasn't correct when the other application was trying to deserialize the class.
Hopefully this will help anyone else who has a similar problem.
Satal :D