deriving wxEvtHandler help

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
narkotico
In need of some credit
In need of some credit
Posts: 1
Joined: Tue Jan 23, 2007 3:18 pm

deriving wxEvtHandler help

Post by narkotico » Tue Jan 23, 2007 3:41 pm

Hi,

I can't get events work (this test program compiles and link, but when running event are not trhown).

main.cpp

Code: Select all

#include "networkmanager.h"
#include <conio.h>

int main()
{
    NetworkManager* net = new NetworkManager();
    while(!kbhit())
    {
        Sleep(1);
    }
    delete net;
    return 0;
}
networkmanager.h

Code: Select all

#ifndef NETWORKMANAGER_H_INCLUDED
#define NETWORKMANAGER_H_INCLUDED
//------------------------------------------------------------------
#include <wx/wx.h>
#include <wx/socket.h>
#include <iostream>
//------------------------------------------------------------------
#define WX_SERVER_PORT     8000
//------------------------------------------------------------------
class NetworkManager : public wxEvtHandler
{
  DECLARE_EVENT_TABLE()
  public:
    NetworkManager();
    ~NetworkManager();

    void OnEvent(wxSocketEvent& event);
  private:
    wxSocketServer *server;
};
//------------------------------------------------------------------
networkmanager.cpp

Code: Select all

#include "networkmanager.h"
//------------------------------------------------------------------
BEGIN_EVENT_TABLE(NetworkManager, wxEvtHandler)
    EVT_SOCKET(wxID_ANY, NetworkManager::OnEvent)
END_EVENT_TABLE()
//------------------------------------------------------------------
NetworkManager::NetworkManager() : wxEvtHandler()// : wxThread(wxTHREAD_JOINABLE)
{
    wxIPV4address addr;
    addr.Service(WX_SERVER_PORT);
    server = new wxSocketServer(addr);

    server->SetEventHandler(*this);
    server->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG | wxSOCKET_CONNECTION_FLAG |
           wxSOCKET_LOST_FLAG | wxSOCKET_INPUT | wxSOCKET_OUTPUT |
          wxSOCKET_CONNECTION | wxSOCKET_LOST);
    server->Notify(true);

    std::cout << "NetworkManager listen at port: " << WX_SERVER_PORT << std::endl;
}
//------------------------------------------------------------------
NetworkManager::~NetworkManager()
{
    if(server)
    {
        server->Close();
        delete server;
        server = NULL;
    }
}
//------------------------------------------------------------------
void NetworkManager::OnEvent(wxSocketEvent& event)
{
	switch (event.GetSocketEvent())
 	{
   	case wxSOCKET_CONNECTION:
   		printf("wxSOCKET_CONNECTION");
   		break;
   	case wxSOCKET_INPUT:
   		printf("wxSOCKET_INPUT");
   		break;
   	case wxSOCKET_OUTPUT:
   		printf("wxSOCKET_OUTPUT");
   		break;
   	case wxSOCKET_LOST:
   		printf("wxSOCKET_LOST");
   		break;
   	}
}
//------------------------------------------------------------------
Whats wrong? any help?

Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor » Wed Jan 24, 2007 7:15 am

As said in [1] but it seems to be true for all plateforms, you must call wxSocketBase::Initialize() before all asynchroneous socket manip.


[1] : http://www.wxwidgets.org/wiki/index.php ... h_wxMSW.29
What is little and green, witch go up and down ??
Yoda playing with the force.

Post Reply