Archived

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

Gaiiden

send()/recv() structures

Recommended Posts

Hey all, How the hell do you go about sending structures using send() and recv() with WinSock?? For instance, I want to send a struct PACKET with its fields but I get an error on compilation which says it can''t convert parameter 2 (the struct) to a char *. Here''s how I set it up: recv(Port, header, sizeof(header), 0); The header is the struct I want to send (or receive). I tried casting (a long shot, I knew) and that didn''t work, or I just didn;t do it right. Can you send structures? Casue if not, that would really ruin my day.... ============================== "Need more eeenput..." - #5, "Short Circuit" ==============================

Share this post


Link to post
Share on other sites
What about something like...

        

PACKET Packet;
int iSize = sizeof(PACKET);
// Fill in the packet info


char* lpBuffer = new char[iSize];
memcpy(lpBuffer, &Packet, iSize);
send(Port, lpBuffer, iSize, 0);



Mightn't this work?

I'm not really sure what the best method is for retrieving variable-size packets, but I would gather recv()ing into a buffer large enough to hold any packet size, then doing tests on header info, would be the best way.


MatrixCubed


Edited by - MatrixCubed on October 29, 2000 2:32:17 PM

Share this post


Link to post
Share on other sites
Thanks. Shouldn''t it be possible to reverse the operation on the other end and memcpy() the data in the buffer back into the structure?

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

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

Share this post


Link to post
Share on other sites
You should be able to... though if you have variable-sized packets, it might be a bit difficult without knowing the size or type of the incoming packets.

I figure using what (I guess) could be called "packet pairing"... packet A is a header, including such things as index, timestamp, packet type, size, and other identifier data. Packet B is the actual data associated with A. That way, you know what packets are incoming, and how to sort them. Whether or not this method is efficient or reliable remains to be seen.


MatrixCubed

Share this post


Link to post
Share on other sites
Thanks MatrixCubed, that''s exactly what I''m doing. I have both structures (HEADER and PACKET) defined for both programs (client and server) so they are both the same size. And the HEADER includes a field for the size of the incoming packets. Good to know I''m on the right track

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

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

Share this post


Link to post
Share on other sites
You have:
recv(Port, header, sizeof(header), 0);

recv(Port, (char *)(&header), sizeof(header), 0);
should work. (Take the address of the header and cast that pointer to a char *.

Share this post


Link to post
Share on other sites