I am trying to create a TCPsocket app with Code:Blocks 20.03, wxWidgets 3.15, and mingw64 on Windows 10. with Formbuilder 3.10.1 as the GUI designer I have studied and built the socket sample with success. Now I would like to create a TCPsocket server in the app I am currently trying to create. This is my first attempt to use sockets, and my first attempt to create a custom event. The Server class is a Subclass of SocketTCPserver, which is generated by wxFormBuilder in the main GUI header and source file.
Because I used Formbuilder to create my GUI, I cannot use an Event Table. So, I searched for a solution and found the one below creating the least amount of errors. I am not sure what causes the warning: "EVT_SOCKET" redefined, and the error: 'EVT_SOCKET' was not declared in this scope.
I hope someone can help me understand and advise on how to create custom events in my situation.
below is the code I used:
#include "Server.h"
enum
{
// id for sockets
SERVER_ID = 100,
SOCKET_ID
};
#define EVT_SOCKET(SOCKET_ID, OnSocketEvent) \
wxDECLARE_EVENT(EVT_SOCKET, wxSocketEvent);
Server::Server( wxWindow* parent ) : SocketTCPserver( parent )
{
Bind(EVT_SOCKET, &Server::OnSocketEvent, this);
}
I received the messages below from building:
||=== Build: Debug in ClientServer (compiler: GNU GCC Compiler) ===|
F:\Data\__C \wxApps\Socket\Client Server\Server.cpp|22|warning: "EVT_SOCKET" redefined|
F:\SDKs\wx315\include\wx\socket.h|439|note: this is the location of the previous definition|
F:\Data\__C \wxApps\Socket\Client Server\Server.cpp||In constructor 'Server::Server(wxWindow*)':|
F:\Data\__C \wxApps\Socket\Client Server\Server.cpp|28|error: 'EVT_SOCKET' was not declared in this scope|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 2 second(s)) ===|
CodePudding user response:
You shouldn't redefine EVT_SOCKET already defined by wxWidgets. Moreover, the type of the event you want to handle, and so should use with Bind(), is wxEVT_SOCKET.
Finally, I'd recommend using a dedicated library for any non-trivial network code rather than using wxSocket at all.
CodePudding user response:
After many searches with google, I got it working.
I removed:
#define EVT_SOCKET(SOCKET_ID, OnSocketEvent) \
wxDECLARE_EVENT(EVT_SOCKET, wxSocketEvent);
Instead of an event table, I tried Bind() to no avail. Then I found a really good explanation for Connect().
Below 2 command work:
Connect(SERVER_ID, wxEVT_SOCKET, wxSocketEventHandler(Server::OnServerEvent));
Connect(SOCKET_ID, wxEVT_SOCKET, wxSocketEventHandler(Server::OnSocketEvent));
I now have a working server. The client should now be no problem.
I love learning :-)
