read binary using winsock

hey all. i tried to write a binary object to a file and then read it out and it worked fine. But when i try to use sockets in order to send the object over a socket its failing giving nothing in return. i'm using the localhost so the client/server are on the same environment.
The packet class is as follows:
1
2
3
4
5
6
7
8
9
10
class PlayerLocation
{
public:
	char pName[8];
	char actionType[10];
	int x;
	int y;
	int z;
	int mapID;
};

for sending the data i'm doing this:
1
2
3
4
5
6
7
8
9

	PlayerLocation pl;
	strcpy(pl.actionType,"RUNNING");
	strcpy(pl.pName,"Muster");
	pl.x=6;
	pl.y=0;
	pl.z=11;
	pl.mapID=10;
        send(sock,(char*)&p,sizeof(p),0);

and for receiving i'm using this:
1
2
                PlayerLocation packet;
		sizeOfData=recv(sock,(char*)&packet,36,0);

I know the size sent and i'm using it exactly when receiving but its not working can someone help. tnx in advance...
Last edited on
Google MSDN winsock.
There's a lot of preparation needed to call send() and recv() that you're just not doing.
Ya i know that. I only wrote the send and recv function to explain how am i doing it and I did send the data as text before and worked successfully. but when sending it binary its not received.
hi didn't you make a mistake in your code?
Maybe it is enough just to replace p by pl?
send(sock,(char*)&pl,sizeof(pl),0);
its a post typing mistake but not my error. But i found the error tnx alot for your help.
If it still doesn't work, try to send the file where you store the object first.
After that try to reconstruct the object on the receiving side.
By the way, the correct way to send or store objects is serialization/unserialization.
The approach of direct memory reading is possible but works for simple objects only.
Oh, okay, then.
Don't send the binary contents of a class over a network. If you need to do something like this, first serialize the contents and then send them. Your method breaks very easily.
tnx for the reply the old problem was solved, it was nothing found in the code above but a different thing. my problem is serialization now, i posted another post concerning serialization where i have an std:string in a class that needs to be converted to binary in order to send it or save it to hd. so is there a free easy to use serialization library i can use?
I think Boost has a serialization library.
i'm searching for something other than boost library. I saw s11n but are there any other serialization library?
Topic archived. No new replies allowed.