Jump to content
  • Advertisement


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


recvfrom buffer

This topic is 5540 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In the call to recvfrom I have to pass a pointer to a buffer that I want filled with the actual information that is recieved. What kind of buffer should this be. Right now the stuff I am sending is a pointer to a structure similar to this one which will be enclosed inside of each packet class I create.
struct S
   int iType;
   int iSize;
   //other info sent here

Each packet class has a virtual function SendOn that is written like below. I''ll probably just derive this from the base class later on since the code will be the same for all objects. Right now it''s virtual and inside each class packet because I need to make sure that the version of SendOn is the version that corresponds to the actual packet type being sent.
void PacketGeneric::SendOn(const struct sockaddr_in toAddress,
		           const int socket)
	toAddress.sin_family = AF_INET;
	toAddress.sin_port = htons(iToPort);
	toAddress.sin_addr = inet_addr(sToIPAddress);
	memset(&(client_addr.sin_zero), ''\0'', 8);

	int iBytesSent = 0;

	S* infoPointer;
	infoPointer = &this->sInfo;

	while (iBytesSent < iSize)
		   iBytesSent = iBytesSent + sendto(socket,
                   infoPointer, SizeOfData(), 0, toAddress, 
So the above passes a pointer to a memory location and a size and it should correctly send the information contained inside of the structure. So when the information gets to the other side and a call to recvfrom is made, what kind of data will I be recieving? Can I simply pass recvfrom a pointer to my PacketGeneric object, examine the type of packet from there and then cast it farther into the correct packet type? Or so I simply get the size of the data from recvfrom and allocate enough memory for it, place the data into that memory and then find a way to access the type and finally cast it to the correct packet type? Thanks, Webby

Share this post

Link to post
Share on other sites
This should be the only message struct you ever use

short size
short type
char data[32768]

If you''re using TCP you have to fully buffer your messages when they come in. You then simply check bytes 0 and 1 ([0]*256 + [1] = size) of the buffer to determine the size of the message then memcpy(msgstruct,buffer,4+sizeofmessage);

Now msgstruct contains the data divided into the variables. You then just use bytecode for all your messages and handle them based on type.


[ IcarusIndie.com | recycledrussianbrides.com ]

KalvinB - (to Jessika) do you accept Jesus as your lord and savior

Jessika - Sure I can accept all forms of payment.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!