Esempio n. 1
0
bool wxGISLocalNetworkPlugin::CreateListenSocket(void)
{
	IPaddress LocalAddress; // For the listening
	LocalAddress.Service(m_nPort); // port on which we listen for clients

	bool bIsAddrSet = false;
	if(m_sAddr.IsEmpty())
    {
		bIsAddrSet = LocalAddress.AnyAddress();
    }
	else
    {
		bIsAddrSet = LocalAddress.Hostname(m_sAddr);
    }

	if(!bIsAddrSet)
	{
		wxLogError(_("wxGISLocalNetworkPlugin: Invalid address - %s"), m_sAddr.c_str());
		return false;
	}

	m_listeningSocket = new wxSocketServer(LocalAddress, wxSOCKET_WAITALL|wxSOCKET_REUSEADDR);
    m_listeningSocket->SetEventHandler(*this, TCP_SERVER_ID);
    m_listeningSocket->SetNotify(wxSOCKET_CONNECTION_FLAG);
    m_listeningSocket->Notify(true);
    if (!m_listeningSocket->IsOk())
    {
		wxLogError(_("wxGISLocalNetworkPlugin: Could not listen at the specified port! Port number - %d"), m_nPort);
        //wxLogError(wxString(_("Cannot bind listening socket")));
        return false;
    }

    wxLogMessage(_("Server listening at port %d, waiting for connections"), m_nPort);

    return true;
}
Esempio n. 2
0
// frame constructor
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(390, 280), 
  wxDEFAULT_FRAME_STYLE ^ wxRESIZE_BORDER)
{
// set the frame icon
SetIcon(wxICON(sample));

#if wxUSE_MENUS
// create a menu bar
wxMenu *fileMenu = new wxMenu;

// the "About" item should be in the help menu
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(Minimal_About, "&About\tF1", "Show about dialog");

fileMenu->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");

// now append the freshly created menu to the menu bar...
wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(fileMenu, "&File");
menuBar->Append(helpMenu, "&Help");

// ... and attach this menu bar to the frame
SetMenuBar(menuBar);
#endif // wxUSE_MENUS

#if wxUSE_STATUSBAR
// create a status bar just for fun (by default with 1 pane only)
CreateStatusBar(2);
SetStatusText("TCP server using wxWidgets");
#endif // wxUSE_STATUSBAR
txtRx = new wxTextCtrl(this, ID_TXTRX, wxT(""), wxPoint(5, 5), 
        wxSize(365, 125), wxTE_MULTILINE);

// Create the address - defaults to localhost:0 initially
IPaddress addr;
addr.AnyAddress();
addr.Service(3000);
txtRx->AppendText(wxString::Format(wxT("Creating server at %s:%u \n")
    ,addr.IPAddress(), addr.Service()));

// Create the socket
sock = new wxSocketServer(addr);

// We use IsOk() here to see if the server is really listening
if (!sock->IsOk()){
	txtRx->AppendText(wxString::Format(wxT("Could not listen at the specified port !\n")));
	return;
}

IPaddress addrReal;
if (!sock->GetLocal(addrReal)){
	txtRx->AppendText(wxString::Format(wxT("ERROR: couldn't get the address we bound to. \n")));
}
else{
	txtRx->AppendText(wxString::Format(wxT("Server listening at %s:%u \n"),
		addrReal.IPAddress(), addrReal.Service()));
}

// Setup the event handler and subscribe to connection events
sock->SetEventHandler( *this, SERVER_ID);
sock->SetNotify(wxSOCKET_CONNECTION_FLAG);
sock->Notify(true);
numClients = 0;
}