Declaring a EthernetServer object in a headerfile

Hi all,

I want to put a EthernetServer object in my communication class. I tryed this:

EthernetServer Server;

And in the constructor of the communication class:

 EthernetServer server = EthernetServer(5000);

But when i try to compile i got the following error:

C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModule\CommunicationModuleTCPIP.cpp: In constructor 'CommunicationModuleTCPIP::CommunicationModuleTCPIP()': C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModule\CommunicationModuleTCPIP.cpp:12: error: no matching function for call to 'EthernetServer::EthernetServer()' C:\Program Files (x86)\Arduino\libraries\Ethernet/EthernetServer.h:14: note: candidates are: EthernetServer::EthernetServer(uint16_t) C:\Program Files (x86)\Arduino\libraries\Ethernet/EthernetServer.h:9: note: EthernetServer::EthernetServer(const EthernetServer&) C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModule\CommunicationModuleTCPIP.cpp: In constructor 'CommunicationModuleTCPIP::CommunicationModuleTCPIP(byte*, IPAddress, int, bool)': C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModule\CommunicationModuleTCPIP.cpp:17: error: no matching function for call to 'EthernetServer::EthernetServer()';

When i do this with a client object then there is no problem. Can anybody tell me if it's possible with to declare a Server object in a class header?

Many thanks i advance

As usual posting just a code snippet isn't enough to see what's going on...

I’m Sorry i kind of solved that problem (i think). I have made a pointer of the server object and when the class constructor is called is initiate the object. I have only one problem now, that is that the client is never seen by the server.

The header file:

class CommunicationModuleTCPIP : public CommunicationModule
{
   public:

	CommunicationModuleTCPIP(byte MacAdress[], IPAddress ServerIPServerIPAdress, int ServPort, bool Server);
	CommunicationModuleTCPIP();

	int Connect();
    void Send();
    void Recieve();
	void SendBuffer(char Buffer[], int Size);
	void RecieveBuffer(char Buffer[], int Size);

	byte Mac[];
	IPAddress ServerIP;
	int ServerPort;

	EthernetClient Client;
                   EthernetServer *Server;  // Pointer for server object


   private:
};

The constructor:

	Serial.begin(9600);
	delay(1000);
	char Buffer[10];

	Serial.println("Setting up server");

	// the router's gateway address:
	byte gateway[] = { 192, 168, 1, 1 };
	// the subnet:
	byte subnet[] = { 255, 255, 255, 0 };

	EthernetServer Server = EthernetServer(5000);

	//Mac = byte[] { 0x90, 0xA2, 0xDA, 0x0D, 0xA4, 0x13 };   //physical mac address
	for (int Index = 0; Index < 6; Index++) {
		Mac[Index] = MacAdress[Index];
	};

	ServerIP = ServerIPAdress; // IP Adress to our Server
	ServerPort = ServPort;

	// initialize the ethernet device
	Ethernet.begin(Mac, ServerIP, gateway, subnet);

	// start listening for clients
	Server.begin();

	Serial.println("Server setup");

	while (1) {

		// if an incoming client connects, there will be bytes available to read:
		Client = Server.available();
		if (Client == true) {
			Serial.println("client connected");
			// read bytes from the incoming client and write them back
			// to any clients connected to the server:

			RecieveBuffer(Buffer, 10);

			Serial.print(Buffer);
		}
	}

The problem is thet the server can’t see the connecting client, the line “client connected” is never printed.

The client code on the pc is using the boost library en is as follows:

		cout << "Setting up client" << endl;

		for(unsigned int i=0; i < sizeof(SendBuffer); i++){
				SendBuffer[i] = 'a';
			}

		string CommandBuffer  = "192.168.1.105";
		string PortNumber = "5000";

		boost::asio::io_service io_service;

		 tcp::resolver resolver(io_service);

		 tcp::resolver::query query(boost::asio::ip::tcp::v4(), CommandBuffer,  PortNumber);

		 tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);


		 tcp::socket socket(io_service);

		 cout << "Connecting..." << endl;
		 boost::asio::connect(socket, endpoint_iterator);
		 cout << "Connected" << endl;


		while(1){
                                                 boost::asio::write(socket, boost::asio::buffer(SendBuffer, 10));
		}

In the console of the client it says connected but the server doesin;t see the client. Doen anybody have an idea why the server doesn’t see the client? If i change the ip number that to client need to conect to in a non existing ip then connected doesn’t appear. So i guess the there is a connection but the server doen’t see it?.

Well i fixed the connection problem. But it turns out i still have te headerfile problem. In the headerfile is declare the Serverobject as a pointer like this:

 EthernetServer *ServerObject;

And in the class constructor i initialize the pointer like this:

*ServerObject = EthernetServer(5000);

This seems fine but when i want to call the begin() function i get a compiler error, this is how i call the function:

*ServerObject = EthernetServer(5000);

And the error that i get is the following:

C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModule\CommunicationModuleTCPIP.cpp: In constructor 'CommunicationModuleTCPIP::CommunicationModuleTCPIP(byte*, IPAddress, int, bool)': C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModule\CommunicationModuleTCPIP.cpp:67: error: request for member 'begin' in '((CommunicationModuleTCPIP*)this)->CommunicationModuleTCPIP::ServerObject', which is of non-class type 'EthernetServer*'

Does anybody have a idea how to solve this?

Many thanks in advance.

Don’t define data in a header file. If you need to share variables between files, put the data definition in the .c/.cpp file that owns the data, and put an external declaration for the variable in a header file which is accessed by other source files needing to access the same variable.

The code fragment you posted does not match how I would expect an instance variable to be defined and initialised. You seem to be using ordinary ‘C’ variables here - if you think these are somehow part of a class, you’re probably wrong.

@PeterH Thanks for your reply.

In the H file i added the definition (not in the class itself):

extern EthernetServer ServerObject

And in the constructor i create te object:

EthernetServer ServerObject(ServPort)

But in some member functions the ServerObject is not reconized, like the function below:

void CommunicationModuleTCPIP::RecieveBuffer(char Buffer[], int Size){

	if(IsServer){
		Client = ServerObject.available();
		if (Client.connected()) {
			Serial.println("client connected");
			 for(int i=0;i < Size; i++){
				Buffer[i] = Client.read();
			 };
		}

	}
	else{
	  for(int i=0;i < Size; i++){
		Buffer[i] = Client.read();
      };
	}
}

Which is defined as a member function in the class headerfile like this:

	void RecieveBuffer(char Buffer[], int Size)

The compiler error that i get is:
CommunicationModule\CommunicationModuleTCPIP.cpp.o: In function CommunicationModuleTCPIP::RecieveBuffer(char*, int)': C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModule/CommunicationModuleTCPIP.cpp:146: undefined reference to ServerObject’

This is strange because in a other member function (pasted in the same code) it works just fine. that function is:

void CommunicationModuleTCPIP::SendBuffer(char Buffer[], int Size){

	//Client = ServerObject.available();
   // Client.write((uint8_t*)Buffer, Size);

		if(IsServer){
		Client = ServerObject.available();
		if (Client.connected()) {
			Serial.println("client connected");
			 for(int i=0;i < Size; i++){
				Buffer[i] = Client.read();
			 };
		}

	}
	else{
	  for(int i=0;i < Size; i++){
		Buffer[i] = Client.read();
      };
	}
}

Does anybody have a idea?

If you want the EthernetServer to be a member of the class then you need to declare it as a member of the class and then define it in the class's implementation file (where the class's methods are defined). Given that you are defining it like an ordinary (non-class) 'C' variable and this presumably compiles, I guess you have not declared it as a class member.

(By the way, it's spelled 'receive'.)

If you want to discuss this any further then I suggest you post your [u]whole[/u] code, because it's a waste of time trying to guess what you've done in code you haven't shown us.

@PeterH

Thanks for your reply! After some help and with your feedback solved the problem. I did’t correctly solve declare the EthernetServer Object. I have solved it by doing the following:

In het class headerfile declare it as a varaible.

 public:

      EthernetServer ServerObject;
      // Other variables and functions that are part of the class.

And in the constructor initialize the ServerObject correctly usinng the initializer list.

CommunicationModuleTCPIP::	CommunicationModuleTCPIP(....Parameters)
	:ServerObject(5000)   <---- Initialize ServerObject
{
    //Constructor code
}

Thanks again for the comments hope that this will help others if they encounter this problem.