Reputation: 1085
How to tell what type of object is being sent over a socket if the server side code looks like this
NetworkStream stream = socket.GetStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream,objectToBeSent);
Upvotes: 0
Views: 336
Reputation: 1038830
On the client I suppose you are going to read the data sent by the server and deserialize it back:
object sentObject = formatter.Deserialize(stream);
Type objectType = sentObject.GetType();
For this to work you will obviously need to include the assembly containing the serialized type on the client.
Oh and bare in mind that the BinaryFormatter class uses a non-interoperable format. This means that if you use for example different versions of the .NET frmaeowrk on the client and server this might not work. If this is the case you should use some interoperable data format protocol to exchange information between a server and a client.
Upvotes: 1
Reputation: 19635
The only way you can know what type of object is being sent over is if there is some metadata sent in the message to indicate what it is. The serialization pattern should be known to both endpoints so that each can serialize and unserialize appropriately. This is (or should be) part of the protocol you've defined for your socket communications.
Upvotes: 3