Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to send structure pointer from client to server using TCP/IP socket program.
I got a guidance to type cast my pointer and send.
But it's not working.
I think I don't know the actual step to type cast structure pointer.

If you know, Please give me an idea to do it..

Thank you.
Posted

It depends on the content of your structure but you need to serialize/deserialize your data, probably field by field. If both your client and server have the same "endian-ness" this is fairly simple, but if they are different then you may need to use the xtonx() functions as described here[^].
 
Share this answer
 
Casting the pointer is ... calling "dog" a cat.
You can actually call "dog" a cat, but it will not start barking.

Casting the pointer is ... pointless: a pointer contain just an address of the memory of your local process space. Once you transfered that address to the other machine (or process or whatever) what could it do with it?

You have to send the data (not the pointers) and reconstruct the pointers on the other side, hence "serialization" is needed.
It is time to stop and learn something more on that concept.
Not something that can fit a "quick answer".
 
Share this answer
 
Hi,

I believe you are using winsock2 for socket programming,if you are using send function call in the server this is the sample code by me:

struct myStruct {
        int a,b;
    }myStruct1;
    void* myPtr = &myStruct1;
    send(client,(char*)myPtr,sizeof(myStruct1),0);




In client side code you can typecast it back to the required struct type.
 
Share this answer
 
Comments
Richard MacCutchan 27-Jul-11 8:51am    
That will be fine as long as both client and server are big-endian or little-endiam. If they are different then you will get some interesting results.
hakz.code 27-Jul-11 9:06am    
Ya true,I was just going through this page to know more :http://www.cse.iitk.ac.in/users/dheeraj/cs425/lec17.html.

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