Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help to build network class

Status
Not open for further replies.

weeb0

Programmer
Feb 1, 2006
2
CA
Hi everyone,

I'm currently working a Network class. For the moment, I have a class containing the following functions :

Code:
class cNetwork
{
	cnetwork();
	cnetwork(int port);
	~cnetwork();
	void send_to(char *buffer,int length);
	int receive_from(char *buf,int buf_size);
	void bind_to(void);
	void wait_connection(void);
};


I think it should be a good thing to separate the server functions of the generic ones ("send_to"/"receive_from") and I should add a "connect_to" one.

When I have a server, if I accept a connection, a new socket number is created, so I need to have 2 socket variable in the class. It's not needed when I have to use a client connection. So, I would like to build the following inheritance of class:

parent : cNetwork (containing the socket and receive_from / send_to functions)

childrens :
cNetwork_server (containing the bind, accept...)
cNetwork_client (containing the connect and other usefull functions to search the dns etc...)


This mean the cNetwork_server class need to access to the socket variable of the parent class. Is this a good practice to use ?
Can you see another way to do this ? Any advice ???

Here the code of the class to be sure everybody can understand what I would like to do:

Code:
class cNetwork
{
public:
    cNetwork();
    ~cNetwork();
    void send_to(char *buffer,int size);
    int receive_from(char *buffer,int size);
protected:
    int socket;
};

class cNetwork_server : cNetwork
{
    /* I must use the variable socket of the parent class withing the functions of this class */
    cNetwork_server();
    ~cNetwork_server();
    void bind_to();
    void wait_for_connection();
};

class cNetwork_client : cNetwork
{
    /* I must use the variable socket of the parent class withing the functions of this class */
     cNetwork_client();
     ~cNetwork_client();
     void connect_to();
};

thank you for your precious help!
 
I haven't done much network programming, but the network classes I've seen usually had a CSocket base class containing all the low level socket functions. Then they usually had CInputSocket & COutputSocket or CClientSocket & CServerSocket which derived from or contained the CSocket base class.

You might also provide a server constructor that takes the max number of connections. Then it can create a thread pool with that many server theads to accept the connections.

Your void connect_to() function should take a parameter for the IP address or DNS name and the port of the machine to connect to. You should also return a bool indicating if it connected properly or if it failed.

You should also consider a way to switch from TCP to UDP...

I'd have the send/receive functions like this:
Code:
int Send( const void* pData, int dataSize, unsigned int flags = 0 );
int Receive( void* pBuffer, int bufSize, unsigned int flags = 0 );
 
OK!

but, is it a good practice to access the variable of the parent ? I know it's possible to do it, but we can use global variable and this is a very bad habit!

Thank you.
 
If you're only reading the base class variable, there usually isn't much of a problem (as long as you're sure it's initialized to a proper value). If you're writing to it, then it might be better to handle that in the base class. It also depends if your application is multi-threaded or not.
Personally, I tend to access base class variables only if it's absolutely necessary, which in this case it probably is.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top