I have a client server application in which I need to transmit a user defined object from Client to Server using TCP connection. My object is of the following structure:
class Conversation
{
private string convName, convOwner;
public ArrayList convUsers;
public string getConvName()
{
return this.convName;
}
public string getConvOwner()
{
return this.convOwner;
}
}
Please help me how to transmit this object at from client and again de-serialize it into appropriate object at server side.
As answered, you should make your object serializable. Once you did that with the
Serializableattribute, you can use the famousBinaryFormatterto convert your object into abytearray.You can find many examples out there for using the
BinaryFormatter, just use your favorite search engine. Here’s a short example:As for your class, it includes two private fields. I can’t see where you set values for them, so I changed your code a bit, so that they can be set in the constructor. In addition, I added the needed
Serializableattribute:Now let’s put it all together, and see your class serialized and then deserialized, in a Console Application:
The result is:
And a final note:
I’d use the generic
Listinstead of the outdatedArrayList, unless you’re bound to .NET 1.*.