Archived

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

MindWipe

Receiveing text from a function - damnit!!!

Recommended Posts

Hello! For a time now I''ve had this really annoying problem. I''m making a WinSock Wrapper and I just can''t get my receive function to work. Here is how the receive function looks for the moment. Probably really stupid made. But anyway:
  
mWinsock::Receive(char *ReceiveBuffer)
{
	char rBuffer[80];

	ReceiveBuffer = new char [80];
	memset(ReceiveBuffer,0,sizeof(ReceiveBuffer));
	memset(rBuffer,0,sizeof(rBuffer));

	recv (mSocket, rBuffer, sizeof(rBuffer), 0);
	strcpy(ReceiveBuffer,rBuffer);
 
}
  
I want the function to fill a char Buffer[80] with the text it receives from recv.
  
mWinsock Connection;
char buffer[80];

Connection.Receive(buffer); <- won''t work

but:

recv (Connection.mSocket, buffer, sizeof(buffer), 0); <- works

  
How would you do it? "If it doesn''t fit, force it; if it breaks, it needed replacement anyway."

Share this post


Link to post
Share on other sites
mmmh, I could be wrong, but shouldn''t you declare
char buffer[80]; as a pointer:
char *buffer[80];

you could also try:
Connection.Receive(&buffer);

or try

char *buffer = (char*)malloc(80);

hope that helps (works)!


Yesterday we still stood at the verge of the abyss,
today we''re a step onward!

Share this post


Link to post
Share on other sites
When you do :

ReceiveBuffer = new char [80];

You make ReceiveBuffer pointing on another buffer than the one which was actually given to then function. Moreover, sizeof(aPointerToChar) won''t give you the length of the pointed string but the size of the pointer itself (4 bytes on most 32 bits systems). Anyway, you just have to do something like this :

  

mWinsock::Receive(char *ReceiveBuffer, int sizeOfBuffer)
{
memset( ReceiveBuffer, 0, sizeOfBuffer);
recv (mSocket, ReceiveBuffer, sizeOfBuffer, 0);
}

void main()
{
const int BUFFER_SIZE = 80;
char buffer[BUFFER_SIZE];
Connection.Receive( buffer, BUFFER_SIZE);
}


Share this post


Link to post
Share on other sites