C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-02-2009, 05:57 AM   #1
Registered User
 
Join Date: May 2008
Location: Australia
Posts: 192
Dynamic Arrays and recv()

Hello everyone. I'm trying to write a socket class for fun and I'm just wondering what the best solution to my problem would be...

Basically I'm trying to write the socket classes Read() function, but i want the user to be able to specify how many bytes to read (which will be parsed the recv()'s len parameter). So, to make it efficient, I want to dynamically allocate memory for an array the same size as the user specifies to read.

The problem is (I'm assuming) since its dynamically allocated (therefore the memory is allocated at run-time) I cannot compile it because the compiler says "Undeclared Identifier", etc.

Here's my code snippet:

Code:
char * Socket::Read(int sockfd, int len)
{
	// Default read and buffer length/size if
	// user does not specify read amount.
	int bufSize = 2048;

	if (len)
		char * buf = new char[len+1];
	else
		char * buf = new char[bufSize];

	if (!sockfd)
	{
		if (!len)
			recv(this->sockfd, buf, bufSize, 0);
		else
			recv(this->sockfd, buf, len, 0);
	}

	else
	{
		if (!len)
			recv(sockfd, buf, bufSize, 0);
		else
			recv(sockfd, buf, len, 0);
	}

	return buf;
}
Thank you for any help you can provide
__________________
Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
pobri19 is offline   Reply With Quote
Old 07-02-2009, 06:51 AM   #2
Registered User
 
Join Date: Oct 2008
Posts: 452
Code:
	if (len)
		char * buf = new char[len+1];
	else
		char * buf = new char[bufSize];
You're declaring it only for the if or for the else. Afterwards, the buf variable is no longer available, as it's outside of its scope. Try declaring buf before the if and assigning it in the if only.
EVOEx is offline   Reply With Quote
Old 07-02-2009, 07:38 AM   #3
Registered User
 
Join Date: May 2008
Location: Australia
Posts: 192
Quote:
Originally Posted by EVOEx View Post
Code:
	if (len)
		char * buf = new char[len+1];
	else
		char * buf = new char[bufSize];
You're declaring it only for the if or for the else. Afterwards, the buf variable is no longer available, as it's outside of its scope. Try declaring buf before the if and assigning it in the if only.
Wow... I can't believe I didn't see that... I feel like an idiot. Thanks for your help.
__________________
Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
pobri19 is offline   Reply With Quote
Old 07-02-2009, 08:06 AM   #4
Registered User
 
Join Date: May 2008
Location: Australia
Posts: 192
Hmm I have another question whilst I'm here. Since I'm returning buf for a different function to process the array, what should I do about deleting the memory? Because I need to use that array, and deleting it in the function that processes it is a really ugly way of doing it. I want things to be organized within the class.

Edit: Never mind I took a different approach.

Code:
// Reads len amount of data from the socket and stores the data in a parsed buffer
// Returns 0 on failure or number of bytes read on success
int Socket::Read(int sockfd, char * recv_buf, int len, int flags)
{
	if (!len)
		return 0;

	int recv_len = recv(sockfd, recv_buf, len, flags);

	if (recv_len == SOCKET_ERROR)
		return 0;

	return recv_len;
}
__________________
Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Last edited by pobri19; 07-02-2009 at 08:46 AM.
pobri19 is offline   Reply With Quote
Old 07-02-2009, 10:00 AM   #5
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,379
Why are you passing the sockfd to the method? That sort of defeats the purpose of having a Socket class in the first place doesn't it?

IMHO, a socket class looks like this:

Code:
class SocketBase
{
public:
    SocketBase( int sockfd )
        : mFd( sockfd )
    {
    }

    virtual ~SocketBase()
    {
        close( mFd );
    }
};

class Socket : public SocketBase
{
public:
    Socket( int sockfd )
        : SocketBase( sockfd )
    {
    }

    int Send( const char *data, int len )
    {
        return send( mFd, data, len, 0 );
    }

    int Receive( char *data, int len )
    {
        return recv( mFd, data, len, 0 );
    }
};
Of course you'd probably have a convenience constructor for Socket which creates the socket itself and connects to a port. At any rate the point is for the socket to own the fd so that it can close it when it gets destroyed. It's pointless to just wrap the socket API without also encapsulating the resource
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is online now   Reply With Quote
Old 07-02-2009, 10:24 AM   #6
Registered User
 
Join Date: May 2008
Location: Australia
Posts: 192
Quote:
Originally Posted by brewbuck View Post
Why are you passing the sockfd to the method? That sort of defeats the purpose of having a Socket class in the first place doesn't it?

IMHO, a socket class looks like this:

Code:
class SocketBase
{
public:
    SocketBase( int sockfd )
        : mFd( sockfd )
    {
    }

    virtual ~SocketBase()
    {
        close( mFd );
    }
};

class Socket : public SocketBase
{
public:
    Socket( int sockfd )
        : SocketBase( sockfd )
    {
    }

    int Send( const char *data, int len )
    {
        return send( mFd, data, len, 0 );
    }

    int Receive( char *data, int len )
    {
        return recv( mFd, data, len, 0 );
    }
};
Of course you'd probably have a convenience constructor for Socket which creates the socket itself and connects to a port. At any rate the point is for the socket to own the fd so that it can close it when it gets destroyed. It's pointless to just wrap the socket API without also encapsulating the resource
That's how I'm currently doing it, I just didn't want to force any restrictions so I'm giving the option of either using the classes socket or a user defined socket.
__________________
Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
pobri19 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 07:20 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22