Archived

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

WebsiteWill

recvfrom buffer

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

}sInfo;
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, 
                   sizeof(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.

Ben


[ 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