Sending pointers?

Started by
8 comments, last by zackriggle 21 years, 3 months ago
Will pointers hold true if you send them via WinSock? Example: class s { char* name; char* dat_; } s myclass; myclass.name = new char [15]; strcpy(myclass.name, "Hello!"); send(connsock, (char*)&myclass, sizeof(myclass), 0) ... When the class gets to the other side, will [myclass.name] still hold "Hello!"? Or will it hold a bad pointer? Also, how are references affected? ================== My (soon-to-be) Cherished Cookie of Appreciation: -- MattB - for WinSock advice --
Advertisement
That won''t work. The addresses that the pointers hold are only valid within the original process''s address space. Once the data gets to the other end, the addresses are meaningless (that is, they point to garbage). To get around this, you need to send each member individually.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
You''ll need to serialize the data in your classes...

Take a look at this article Why Pluggable Factories Rock My Multiplayer World



Dave "Dak Lozar" Loeser
Dave Dak Lozar Loeser
"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."--anonymous
You can''t send pointer through the sockets. A pointer on one mechine (the sender) is pointing to a specified memory area. The pointer itself is a number. So when the receiver is using this ptr it points to the same mem area as on the sender machine. But this mem area is probably allocated to the other application. This area doesn''t belong to the receiver application (and if it does, the memory allocation is not the same as on the sender mechine). So the OS will forbid using that pointer. You need to pass the value to which the pointer points, but not the pointer itself.
and how do I go about doing this?
One way to do it is to create an object/structure for every object/class to hold the values that are going to be transfered. Before sending the data you access access the pointers and copy the data those pointers are pointing to in the data structure marked for this object. Then you simply send the data structure.

On the other end you receive the data structure and copy those values to an object that needs it.

To make this efficient, don''t go around copying everything you posibly can, but think about what''s needed on the other side to make it work. The less you send the better.
Applied to strings, this would mean
send(sckMySocket, szMyString, strlen(szMyString) + 1, 0);

It is strlen(...) + 1 to take care, the '\0' is also send because how could the other computer now where the string ends?



[edited by - BlackLight on December 25, 2002 7:21:40 AM]
just think of the savings on bandwidth if we could send only pointers to the data... hehe
hrhr, make some check if remote host is 127.0.0.1 / localhost and do send some nasty pointers... =)
There are a large number of articles that discuss serialization of class/structure data - just do a search on google.

But, I do have some (hopefully) helpful hints. For sending serialized class data that contained strings or list of strings I established a protocol for this serialized string data. Basically, when the class serializes the data it “knows” how to pack the data up and when the serialized data is unpacked on the receiving end, the class “knows” how to unpack this serialized data. So, for strings I placed a DWORD of the size and then the BYTES for the string followed (minus the null termination character).

Here’s an example, say we have a class that represents the login message and the data in that class was:

class CLoginMessage
{
...
DWORD m_MessageID; // 0x01 – could be anthing…
string m_UserName;
string m_PassWord;
...
};

So, when this goes out on the wire, the serialized message might look like the following
(UserName = user and PassWord = pass

0100000004000000user04000000pass

It’s pretty easy to see the data in the above example of the serialized data.

List of strings contain a DWORD value that represents the number of strings in the list and then each string is described as above. What would you need a list of strings for? Maybe you have a listbox that gets its data from the server. For example, lets say we have a list of users in a guild that looks like this:


class CGuildListMessage
{
...
DWORD m_MessageID; // 0x100
stringlist m_GuildNames;
...
};

So, when I open up a GUI item that displays the list of guild members the data coming back from the server might look like the following:

000100000200000009000000Kressilac09000000Dak Lozar
Again, it should be easy to see how the data is laid out.

There are other tricks that you can perform on the serialized data such as compression or encryption or both...



Dave "Dak Lozar" Loeser
Dave Dak Lozar Loeser
"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."--anonymous

This topic is closed to new replies.

Advertisement