Sending/Recv'ing a Struct through WinSock
I''ve been playing with WinSock and I can''t seem to find a way to send a complete struct filled with data to the server client.
Could anyone give me a rough example of how this would be done? I realize Send needs you to pack things into a character buffer and then it''s sent but Im not sure how you take that character buffer using recv() and then retrieving the data from that buffer.
Can anyone give me an example or explain how to do this?
There are many ways of doing this, here is but just one Generic Serialization. I have reimplemented this using templates and have been writing an article that describes the tempate idiom for serilization.
HTH,
Dave Dak Lozar Loeser
The WM_NULL message performs no operation. An application sends the WM_NULL message if it wants to post a message that the recipient window will ignore. - MSDN
HTH,
Dave Dak Lozar Loeser
The WM_NULL message performs no operation. An application sends the WM_NULL message if it wants to post a message that the recipient window will ignore. - MSDN
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
"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
Does the class contain any STL container? If not, determine the size of the entire object via sizeof(). Pass the address of the object and its size to send(), recv(), etc.
Kuphryn
Kuphryn
//client side
struct MyObj
{
//Object data
int Age;
char Name;
};
MyObj Person;
Person.Age = 25;
strcpy(Person.Name,"Bob");
MyObj *ptr;
ptr = &Person
long MsgLength;
MsgLength = sizeof(MyObj); //get the size of your obj
MsgLength = htonl(MsgLength); //convert size for network
send(Socket,(char*)&MsgLength,sizeof(long),0); //send size of obj
MsgLength = ntohl(MsgLength); //convert size back for local pc
send(Socket,(char*)&ptr,MsgLength,0); //send obj
/************************************************************
//ServerSide
struct MyObj
{
//Object data
int Age;
char Name;
};
MyObj Person;
MyObj *ptr;
ptr = &Person
long MsgLength;
recv(Socket,(char*)&MsgLength,sizeof(long),0); //recv() size of obj
MsgLength = ntohl(MsgLength); //convert size of obj
recv(Socket,(char*)&ptr,MsgLength,0); //recv() obj
struct MyObj
{
//Object data
int Age;
char Name;
};
MyObj Person;
Person.Age = 25;
strcpy(Person.Name,"Bob");
MyObj *ptr;
ptr = &Person
long MsgLength;
MsgLength = sizeof(MyObj); //get the size of your obj
MsgLength = htonl(MsgLength); //convert size for network
send(Socket,(char*)&MsgLength,sizeof(long),0); //send size of obj
MsgLength = ntohl(MsgLength); //convert size back for local pc
send(Socket,(char*)&ptr,MsgLength,0); //send obj
/************************************************************
//ServerSide
struct MyObj
{
//Object data
int Age;
char Name;
};
MyObj Person;
MyObj *ptr;
ptr = &Person
long MsgLength;
recv(Socket,(char*)&MsgLength,sizeof(long),0); //recv() size of obj
MsgLength = ntohl(MsgLength); //convert size of obj
recv(Socket,(char*)&ptr,MsgLength,0); //recv() obj
It may be just me, but I don''t think converting of to be transmitted data is neccesary
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.
So you have to send the size of a data packet and then follow that up with a pointer to the object itself?
Any chance you can explain why you need to do that? Just for curiousity sake.
Any chance you can explain why you need to do that? Just for curiousity sake.
Well it''s a stream, if you don''t know how big the ''packet'' will be you just don''t know how big it will be ![](smile.gif)
Also, the classes name should probably send as well, or at least an identifier.
But I still think converting data to network order is useless, it doesn''t matter for the data as far as I know.
![](smile.gif)
Also, the classes name should probably send as well, or at least an identifier.
But I still think converting data to network order is useless, it doesn''t matter for the data as far as I know.
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement