Sending a serialized object... I wished...

mariano_donati

Active member
Joined
Nov 30, 2005
Messages
41
Programming Experience
Beginner
Hi everybody, I'm triying to send a serialized object through a socket. I have this application Client/Server, wich works sending classes from and to the server. Each class represent a event, with certain info about it. For example, I have a MessageClass wich also has a few properties like Data(), IdFrom(), IdTo(). I want to send it to the server, the server analize the relevant property (in this case, IdTo() property), and redirect the class to the client wich its Id = IdTo property.
This is the important function in the server application:

Private Function DeserializeBytes(ByVal B() As Byte) As Message

Dim M As New MemoryStream(B)

Dim Formatter As New BinaryFormatter()

Return CType(Formatter.Deserialize(M), Message) :(

End Function

B() param, is a array of bytes that client sends to the server. This array contains the serialized object. At client application I do this:

Private Function GetSerializedBytes(ByVal E As Message) As Byte()

Dim Formatter As New BinaryFormatter()

Dim S As New MemoryStream()

Formatter.Serialize(S, E)

Dim BinaryReader As New BinaryReader(S)

Dim Buffer() As Byte

Buffer =
New Byte(S.Length) {}

BinaryReader.Read(Buffer, 0, S.Length)

Return Buffer

End Function


This function returns an array of bytes with the serialized object (at least I suppose so) and it's the array wich is sent from the client to the server.
When server application executes the line wich is marked with the frown face, it throw a System.Runtime.Serialization.SerializationException at mscorlib.dll.
Can you see any error here?.

I'd be really greateful if you can help me out with this.
Sorry for my english.
Regards.
 
I think, and I'm not sure about this... but I think you have to deserialize it to an object before returning it. I don't think it can deserialize and return at the same time.

But I'm not 100% sure about that.

-tg
 
Well, I keep getting the same error... I tried with DirectCast instead of CType, but nothing happens, same error appears. I'm in doubt if what I'm doing is the way for sending serialized objects through a socket. Do you think that, excluding the error, it's a right way to do that?
Thanks in advance.
This is a great forum.
Regards.
 
Just as I thought, my approaching wasn't right. The function GetSerializedBytes() returned a null array of byte. I could solve it by using the GetBuffer method of MemoryStream, wich returns a array of bytes that belongs to the stream. But, now I'm having some other trouble here. The application seems to stop when I call the DeserializeBytes() function. From here, the server application doesn't execute any other line of my code. I think that the deserialize method of the BinaryFormatter is having some kind of trouble triying to deserialize the MemoryStream, but It doesn't let me know what error is that blows the application out!, It just doesn't work anymore!.
Do you have a clue about this?
Good luck!.

----------After editing ------

I have tested it a few more times, and I still don't get it. When I run the application from the .exe file, the situation is what I metioned above. But when I run the application for debugging, then I get a System.IO.FileNotFoundException in the line that I call the deserialize method of the binaryFormatter object. The aditional info of this exception says:

No se encuentra el archivo o el nombre del ensamblado Live-Chat! o una de sus dependencias, wich in english means something like this (sorry, again, for my english):

couldn't find the file or assembly name Live-Chat or one of its dependencies.... or something like that... Does anybody recognize this error?.

I did some test application to check out if my approach to do this task was or was not right. It works fine. So, I believe that may be when I'm sending the bytes array through socket occurs some trouble, and then I can't deserialize the object.

Any help will be really welcome.

Regards!.
 
Last edited:
Well! I finally could solve the issue!. If anybody is interest in know how to do it (and how not to do it), I'm gonna tell you all how I did it. First thing is NOT TO CREATE TWO CLASSES for each application. I mean, in server application I had a class named Message and in client application I had the same class. And that was the problem. So, I did a Dll with its respective namespaces and classes and referenced it from each application, and that's it.
With all this trouble, I looked for all over the net and I find some differents and really interests options to do the same. If anyone is interested on this, then don't be shy and ask me for the code.
Regards!.
 
Back
Top