954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Send Object via TCP (Winsock2)

I've trawled the internet searchiing for an explaination on how to do this.

I'm working on an assignment and we are told to "transmit a whole object to the server".

As far as I know, the send function of winsock2 only allows you to send data of type char, so how can you transmit an object? Is there a way to convert an object to type char and then convert it back to an object the other end?

I'm at a loss here!

kutuup
Junior Poster in Training
68 posts since Feb 2011
Reputation Points: 35
Solved Threads: 0
 

All data in a digital computer is bits and bytes, effetively 0/1 strings. Datatypes of any sort are just interpretations of these data. Your job here is to serialise your object into a string of bytes before transmittions and recreate the object after it has been transmitted. Easiest to understand is probably the following: Write all the member variables of your object into a character-string (using string stream operators or sprintf, or whatever method you are familiar with). This way you have serialised the object into char's. Transmit this string and at the receiving end read the values back into memory and create an object using those values.
You have to think about what you want to do in case your object has complex members and/or pointers to other objects, but the general approach is the same.
Obviously this method is not very efficient, but once you've done it this way you can think about ways to improve permance.
Also you can have a look at the boost serialisation library if you are interested.
http://www.boost.org/doc/libs/1_46_1/libs/serialization/doc/index.html

drkybelk
Junior Poster in Training
73 posts since May 2010
Reputation Points: 12
Solved Threads: 13
 
All data in a digital computer is bits and bytes, effetively 0/1 strings. Datatypes of any sort are just interpretations of these data. Your job here is to serialise your object into a string of bytes before transmittions and recreate the object after it has been transmitted. Easiest to understand is probably the following: Write all the member variables of your object into a character-string (using string stream operators or sprintf, or whatever method you are familiar with). This way you have serialised the object into char's. Transmit this string and at the receiving end read the values back into memory and create an object using those values. You have to think about what you want to do in case your object has complex members and/or pointers to other objects, but the general approach is the same. Obviously this method is not very efficient, but once you've done it this way you can think about ways to improve permance. Also you can have a look at the boost serialisation library if you are interested. http://www.boost.org/doc/libs/1_46_1/libs/serialization/doc/index.html

Ah, I hadn't heard or thought of this concept!

I was just thinking about it the wrong way.

Thanks! The info was really helpful :)

kutuup
Junior Poster in Training
68 posts since Feb 2011
Reputation Points: 35
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: