![]() |
| | #1 | |
| Registered User Join Date: May 2008 Location: Australia
Posts: 192
| Dynamic Arrays and recv() 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;
}
__________________ Quote:
| |
| pobri19 is offline | |
| | #2 |
| Registered User Join Date: Oct 2008
Posts: 452
| Code: if (len) char * buf = new char[len+1]; else char * buf = new char[bufSize]; |
| EVOEx is offline | |
| | #3 | ||
| Registered User Join Date: May 2008 Location: Australia
Posts: 192
| Quote:
__________________ Quote:
| ||
| pobri19 is offline | |
| | #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:
Last edited by pobri19; 07-02-2009 at 08:46 AM. | |
| pobri19 is offline | |
| | #5 |
| Senior software engineer 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 );
}
};
__________________ "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 | |
| | #6 | ||
| Registered User Join Date: May 2008 Location: Australia
Posts: 192
| Quote:
__________________ Quote:
| ||
| pobri19 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|