wxSocketClient and wxAppConsole

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Schala
In need of some credit
In need of some credit
Posts: 9
Joined: Mon Jul 16, 2012 8:11 am

wxSocketClient and wxAppConsole

Post by Schala »

Hello. I'm trying to make a client program for the Hotline protocol and I'm starting out by simply getting it to send out its identification to the server, and then receive the server confirmation. For this supposed trivial stage of my program I wanted to use CLI instead of a GUI for simplicity. I've hardcoded "localhost" as the connection address, with the Hotline server port, 5500. The client is to send out "TRTPHOTL" followed by 16-bit integer values 1 and 2, to the server. If successful, the server should send back "TRTP" followed by two 16-bit integer values of 0. I've got my copy of Cross Platform GUI Programming with wxWidgets to help me out with the socket a bit, but being as this is a CLI, I'm rather confused on how to get the program to handle events such as what to do when connected, and wait til the server sends back data. What I have so far is as follows (note I haven't gotten around to doing OnExit() just yet, feel free to comment out the declaration):

Code: Select all

#include <wx/app.h>
#include <wx/socket.h>

class HLClientApp: public wxAppConsole
{
public:
  int OnExit();
  int OnRun();
  void OnSocketEvent(wxSocketEvent & event);
  
  DECLARE_EVENT_TABLE()
};

enum
{
  ID_Socket = 1
};

BEGIN_EVENT_TABLE(HLClientApp, wxAppConsole)
  EVT_SOCKET(ID_Socket, HLClientApp::OnSocketEvent)
END_EVENT_TABLE()

wxIMPLEMENT_APP(HLClientApp)

int HLClientApp::OnRun()
{
  wxIPV4address addr;
  addr.Hostname("localhost");
  addr.Service(5500);
  
  wxSocketClient * sc = new wxSocketClient()
  sc->SetEventHandler(*this, ID_Socket);
  sc->SetNotify(wxSOCKET_INPUT | wxSOCKET_CONNECTION | wxSOCKET_LOST);
  sc->Notify(true);
  sc->Connect(addr);
}

void HLClientApp::OnSocketEvent(wxSocketEvent & event)
{
  wxSocketBase * sock = event.GetSocket();
  char inbuf[9], outbuf[] = {'T', 'R', 'T', 'P', 'H', 'O', 'T', 'L',
    0x00, 0x01, 0x00, 0x02};
  switch(event.GetSocketEvent())
  {
    case wxSOCKET_CONNECTION:
      sock->Write(outbuf, sizeof(outbuf));
      break;
    case wxSOCKET_INPUT:
      sock->Read(inbuf, sizeof(inbuf));
      break;
    case wxSOCKET_LOST:
      sock->Close();
  }
}
I do warn you now, this is my first attempt at wxWidgets CLI event handling, so this is likely the incorrect way, which is what I'm concerned about and why I'm here asking for help. Also beware that I haven't tried compiling this just yet, so for all I know it is error-prone.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Re: wxSocketClient and wxAppConsole

Post by Auria »

Hi,

first, which wx version is this? wx 2.8 does not support event handling when making console apps, so if you wish to make console apps with events a first step is to upgrade to wx 2.9
"Keyboard not detected. Press F1 to continue"
-- Windows
Schala
In need of some credit
In need of some credit
Posts: 9
Joined: Mon Jul 16, 2012 8:11 am

Re: wxSocketClient and wxAppConsole

Post by Schala »

Ah right, sorry. I'm using 2.9.4.
Post Reply