Archived

This topic is now archived and is closed to further replies.

Gaiiden

ARRRRRRGGGHHHH!!!!!

Recommended Posts

OK, a few days ago i asked about sending structs with WinSock send() and recv(). The response I got was to put the struct into a char* using memcpy() and send it. Now, at the other end, how do i put it back into the struct?? I tried doing the opposite and memcpying the buffer into the struct like this: memcpy(header, lpBuff, header_size); but that didn''t work. It can''t convert the void * into the structure type (HEADER). I tried casting everywhich way but that didn''t work. Maybe I didn''t do it right. So that''s the deal. I figured since the data in the buffer was stored for the exact same struct at one end, when it came out, I could put it back into that same struct on the other end. Guess i was wrong... any suggestions?? ============================== "Need more eeenput..." - #5, "Short Circuit" ==============================

Share this post


Link to post
Share on other sites
in theory that should work, could you post your code so we can check it out. (you also need to make sure the sizes of the types are the same on each machine, maybe send over sizeof(YOURSTRUCT) to compare at the other end?)

Brought to you by: O.G.A.P.O.
+----------------------------------------+
| Surgeon General's Warning - |
| OGAPO can cause serious mental damage |
| if taken in large doses. |
+----------------------------------------+

/* Never underestimate the power of stupid people in large groups */

Share this post


Link to post
Share on other sites
Sure, here''s the code:
    
// get the size of PACKET

int packet_size = sizeof(PACKET);

// create a buffer to hold the information

char *lpBuff = new char[packet_size];

// receive data from the Server

if (recv(Port, lpBuff, packet_size, 0) == SOCKET_ERROR)
// display an error message and quit

ErrorHandle("Could not receive packet!", WINSOCK);

// copy the data from the buffer to the packet struct

memcpy(packet, &lpBuff, packet_size);

The struct PACKET is the exact same struct used on the other end, so I don''t see how they could be different in size. The error is on the memcpy() function. I tried removing the address symbol, casting both header and lpBuff, but I kept getting an error one way or another.

==============================
"Need more eeenput..."

- #5, "Short Circuit"
==============================

Share this post


Link to post
Share on other sites
This is a fairly easy thing to do. Here is some code that will do the sending in a differnet way too.



// This is the structure that is to be sent or recived
struct DATA
{
int used;
char data[64]
double hi;
}

DATA data; // object to be sent

char big_bad_buffer[8192]; // buffer for send/recv that is large enough to hold all of the structures being sent/recv

//when you want to send data
send(Port,(char*)&data,sizeof(data),0);

// and recive
recv(Port,big_bad_buffer,8192,0);

// set data object so you can handle it
data = *((DATA*)big_bad_buffer);

// then just access the data with:
data.data[4] // and so on

// or so that you don''t need to copy data data you can:
DATA* p_data = (DATA*)big_bad_buffer;

// and access the data as:
p_data->data[4] // and so on



Lots of data there

Share this post


Link to post
Share on other sites
so I can''t load in the buffer to the data structure so I can use the fields again? Like data.data or data.used?? I have to use a pointer?

==============================
"Need more eeenput..."

- #5, "Short Circuit"
==============================

Share this post


Link to post
Share on other sites
These are TWO DIFFERNET ways to access the data you recived:

no1. (the way to go if you need to store the data, ie. longer than to the next recv)
data = *((DATA*)big_bad_buffer);
data.data[4]

no2. (this is more effective if you don''t need to store the data for a long time, ie. longer than to the next recv)
DATA* p_data = (DATA*)big_bad_buffer;
p_data->data[4]

Share this post


Link to post
Share on other sites