Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How to pass and retrieve data in structure type from client and server using winsock2 in c++?
Posted
Comments
aasikRaja 12-Oct-12 1:17am    
could you elaborate ur qn that what data you wish to retrieve..

The simplest solution would be

if your struct is like

C++
struct MyStruct
{
int a;
int b;
};


At the sender side.
C++
MyStruct myObject;
myObject.a = 10;
myObject.b = 20;


send( SOCKET, (char*)&myObject, sizeof(MyStruct), 0);



and at recieving side;
C++
MyStruct myObjectRecv;

recv( SOCKET, (char*)&myObjectRecv, sizeof(MyStruct), 0 );


Now you can say that myObjectRecv is a clone of myObject.


Limitation of this approach is , it will produce errorneous results if your structure contains pointers.
 
Share this answer
 
I understand you have asked about WinSock and C++ specifically, but still, there are some issues to consider:

Merely "understanding" the structure is insufficient. It is necessary to consider the following:

1) Data alignment. For example, if you have:

C++
typedef struct
{
 long int a;
 short b;
 long int c;
} SOME_STRUCT;


depending on compiler options, the bus width of the target CPU (e.g. 64/32/16/8 bits etc) SOME_STRUCT may end up different in size - on Windows it could be 10 or 12 bytes depending on the compiler option. This is because the compiler may leave a gap of 2 bytes after 'b' if struct members are aligned on 4-byte boundaries.

All compilers have data alignment pragmas and options. Visual studio for example, calls them "struct member alignment". So it is possible to have this different even if you use the same platform on both ends and then you could run into problems.

2) Probably not an issue unless you consider a mulitude of platforms supported by Windows CE. Then you should consider the endianness of data (as client and server may use different model). For example, client may send 4-byte integers with most significant byte first, and server may interpret them as 4-byte quantities with least significant byte first. See:

http://en.wikipedia.org/wiki/Endianness[^]

3) If you are using exactly the same platform on both ends, and you have compiled both server and client code with the same struct alignment, then you can indeed send your struct as a stream of bytes and on the other end simply interpret the received data as the same structure.

4) It is a valid and common method to define a communications protocol using C data structs provided you have specified the alignment rules and endianness of data.
 
Share this answer
 
v4
Comments
Member 9404971 8-Oct-12 0:04am    
i have used same structure packing on both client and server sides and sending it as byte streams.. but i couldn't get the data on server side which is send from client side..
michaelmel 8-Oct-12 1:33am    
Do you get any data?
Just send and receive as a stream of bytes. As long as both ends of the socket understand the structure of the data you should have no problems.
 
Share this answer
 
Comments
michaelmel 7-Oct-12 20:49pm    
This is partially incorrect. Explanation in my solution.
Richard MacCutchan 8-Oct-12 3:18am    
Nothing incorrect about this, I have used it many times.
Member 9404971 8-Oct-12 0:05am    
am sending it as the stream of bytes only.. but i couldn't get the correct data in server side...
Richard MacCutchan 8-Oct-12 3:19am    
That is not an explanation of your problem. Please edit your original question and show the code you are using to send and to receive.
This link might be useful http://johnnie.jerrata.com/winsocktutorial/[^]
 
Share this answer
 
Just use a xml format.
XML
<name>MY_NAME</name>
<age>MY_AGE</age>
<address>MY_ADD
</address>

C++
char* data = "<name>MY_NAME</name><age>MY_AGE</age><address>MY_ADD</address>";
write(SOCKET_NAME,data,strlen(data));

sent it as a byte stream. and other side just parse it using a xml parser.
Here is some example of xml parser.
Using MSXML to read XML documents[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900