🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

send()/recv() structures

Started by
4 comments, last by Gaiiden 23 years, 8 months ago
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" ==============================

Drew Sikora
Executive Producer
GameDev.net

Advertisement
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
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"
==============================

Drew Sikora
Executive Producer
GameDev.net

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
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"
==============================

Drew Sikora
Executive Producer
GameDev.net

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 *.

This topic is closed to new replies.

Advertisement