Click here to Skip to main content
13,002,399 members (63,965 online)
Rate this:
Please Sign up or sign in to vote.
See more:
How to pass and retrieve data in structure type from client and server using winsock2 in c++?
Posted 7-Oct-12 6:31am
aasikRaja 12-Oct-12 1:17am
could you elaborate ur qn that what data you wish to retrieve..
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

The simplest solution would be

if your struct is like

struct MyStruct
int a;
int b;

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

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

and at recieving side;
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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:

typedef struct
 long int a;
 short b;
 long int c;

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:[^]

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.
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?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Just use a xml format.

char* data = "<name>MY_NAME</name><age>MY_AGE</age><address>MY_ADD</address>";

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[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170626.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100