您好,我正在尝试创建一个 VNC 客户端。现在,我正在创建将用于查看远程用户屏幕的 GUI。这是到目前为止我的代码,我正在使用 WxWidgets 3.2.2
#include <wx/wx.h>
#include <wx/socket.h>
#include <iostream>
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnConnect(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnSocketEvent(wxSocketEvent& event);
private:
wxSocketServer m_server;
wxSocketBase* m_clientSocket;
bool m_connectionReceived;
wxDECLARE_EVENT_TABLE();
};
enum
{
ID_CONNECT = 1,
ID_EXIT
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_CONNECT, MyFrame::OnConnect)
EVT_MENU(ID_EXIT, MyFrame::OnExit)
EVT_SOCKET(wxID_ANY, MyFrame::OnSocketEvent)
wxEND_EVENT_TABLE()
bool MyApp::OnInit()
{
MyFrame* frame = new MyFrame("wxWidgets Network App", wxPoint(50, 50), wxSize(800, 600));
frame->Show(true);
SetTopWindow(frame);
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(nullptr, wxID_ANY, title, pos, size),
m_server(), // Initialize without specifying flags
m_clientSocket(nullptr),
m_connectionReceived(false)
{
// Create a menu
wxMenu* menuFile = new wxMenu;
menuFile->Append(ID_CONNECT, "Connect", "Connect to the client");
menuFile->Append(ID_EXIT, "Exit", "Exit the application");
wxMenuBar* menuBar = new wxMenuBar;
menuBar->Append(menuFile, "File");
SetMenuBar(menuBar);
CreateStatusBar();
SetStatusText("Waiting for a connection...");
if (!m_server.Ok()) {
wxLogError("Failed to set up the server.");
}
if (!m_server.WaitForAccept(wxSOCKET_WAITALL, -1)) {
wxLogError("Failed to wait for incoming connections.");
}
}
void MyFrame::OnConnect(wxCommandEvent& event)
{
if (!m_connectionReceived) {
if (m_clientSocket) {
m_connectionReceived = true;
SetStatusText("Connection received.");
SetTitle("CONNECTION RECEIVED");
}
}
}
void MyFrame::OnExit(wxCommandEvent& event)
{
Close(true);
}
void MyFrame::OnSocketEvent(wxSocketEvent& event)
{
if (event.GetSocket() == &m_server) {
if (event.GetSocketEvent() == wxSOCKET_CONNECTION) {
m_clientSocket = m_server.Accept();
if (m_clientSocket) {
m_connectionReceived = true;
SetStatusText("Connection received.");
SetTitle("CONNECTION RECEIVED");
}
}
}
}
IMPLEMENT_APP(MyApp)
我收到以下错误:
Severity Code Description Project File Line Suppression State
Error (active) E0289 no instance of constructor "wxSocketServer::wxSocketServer" matches the argument list VNC_GUI_MAIN C:\Users\User\source\repos\VNC_GUI_MAIN\main.cpp 50
Severity Code Description Project File Line Suppression State
Error C2512 'wxSocketServer::wxSocketServer': no appropriate default constructor available VNC_GUI_MAIN C:\Users\User\source\repos\VNC_GUI_MAIN\main.cpp 50
我试图解决这个问题,但我一直没能成功构建它。
这里有什么问题的提示吗?您的服务器正在侦听哪个端口?代码中哪里指定的?
你正在像这样初始化wxSocketServer:
m_server(), // Initialize without specifying flags
快速的互联网搜索显示它有这样的构造函数:
wxSocketServer (const wxSockAddress &address, wxSocketFlags flags=wxSOCKET_NONE)
所以你必须指定一个包含端口的监听地址。
也许这就是你想要的:
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(nullptr, wxID_ANY, title, pos, size),
m_server(nullptr),
m_clientSocket(nullptr),
m_connectionReceived(false)
{
wxIPV4address addr;
addr.AnyAddress(); // listen on all adapters
unsigned short localListenPort = 64000; // listen on port 64000
addr.Service(localListenPort);
m_server = new wxSocketServer(localListenPort);
我认为更大的问题是,当您声明的目的是创建 VNC 客户端时,为什么需要构造服务器对象。除非 VNC 协议要求客户端接受返回的连接,否则可能不需要。