read binary using winsock

Mar 3, 2009 at 11:20pm
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 Mar 3, 2009 at 11:21pm
Mar 4, 2009 at 3:36am
Google MSDN winsock.
There's a lot of preparation needed to call send() and recv() that you're just not doing.
Mar 4, 2009 at 9:30am
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.
Mar 4, 2009 at 1:17pm
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);
Mar 4, 2009 at 1:45pm
its a post typing mistake but not my error. But i found the error tnx alot for your help.
Mar 4, 2009 at 2:20pm
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.
Mar 4, 2009 at 3:17pm
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.
Mar 4, 2009 at 3:28pm
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?
Mar 4, 2009 at 3:33pm
I think Boost has a serialization library.
Mar 4, 2009 at 3:37pm
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.