/*! * Translate a IPaddress into a meaningful string * * @param addr The address to translate * * @return A string representing the addr e.g. localhost:1099 */ wxString GdbServerWindow::getAddr(IPaddress addr) { wxString *serverAddr = new wxString(); if (addr.IPAddress().compare(_("127.0.0.1")) == 0) { serverAddr->Printf(_("localhost:%u"), addr.Service()); } else { serverAddr->Printf(_("%s:%u"), (const char *)addr.IPAddress().c_str(), addr.Service()); } return *serverAddr; }
bool wxGISLocalClientConnection::Connect(void) { if(m_bIsConnected) return true; wxString sHost(HOST); unsigned short nPort(PORT); unsigned short nTimeOut(TIMEOUT); wxGISAppConfig oConfig = GetConfig(); if(oConfig.IsOk()) { sHost = oConfig.Read(enumGISHKCU, wxString(wxT("wxGISCommon/tasks/host")), sHost); nPort = oConfig.ReadInt(enumGISHKCU, wxString(wxT("wxGISCommon/tasks/port")), nPort); nTimeOut = oConfig.ReadInt(enumGISHKCU, wxString(wxT("wxGISCommon/tasks/timeout")), nTimeOut); } //start conn IPaddress addr; addr.Hostname(sHost); addr.Service(nPort); // Create the socket wxSocketClient* pSock = new wxSocketClient(wxSOCKET_WAITALL | wxSOCKET_BLOCK | wxSOCKET_REUSEADDR); m_pSock = pSock; m_pSock->SetEventHandler(*this, SOCKET_ID); m_pSock->Notify(true); m_pSock->SetNotify(wxSOCKET_CONNECTION_FLAG|wxSOCKET_LOST_FLAG); m_pSock->SetTimeout(nTimeOut); pSock->Connect(addr, false); m_bIsConnecting = true; return CreateAndRunThreads(); }
/*! * Create server listening on socket * Subscribes to connection events with ID SERVER_ID */ void GdbServerWindow::createServer() { // Create the address IPaddress listenAddr; listenAddr.Service(shared->getGdbServerPort()); listenAddr.Hostname(_("localhost")); serverAddr = getAddr(listenAddr); closeServer(); // Create the socket serverSocket = new wxSocketServer(listenAddr, wxSOCKET_NOWAIT); // We use IsOk() here to see if the server is really listening if (!serverSocket->IsOk()) { statusTextControl->AppendText(_("ERROR: Could not create server at the specified port !\n")); return; } string bdmSerialNumber = shared->getBdmSerialNumber(); if (bdmSerialNumber.length() > 0) { if (shared->getBdmMatchRequired()) { statusTextControl->AppendText(_("Using required USBDM interface S/N = \'")+wxString(bdmSerialNumber.c_str(), wxConvUTF8)+_("\'\n")); } else { statusTextControl->AppendText(_("Using preferred USBDM interface S/N = \'")+wxString(bdmSerialNumber.c_str(), wxConvUTF8)+_("\'\n")); } } else { statusTextControl->AppendText(_("Using any suitable USBDM interface\n")); } IPaddress addrReal; if (!serverSocket->GetLocal(addrReal) ) { statusTextControl->AppendText(_("ERROR: couldn't get the address we bound to\n")); } else { serverAddr = getAddr(addrReal); wxString s; s.Printf(_("Server created @%s\n"), (const char *)serverAddr.c_str()); statusTextControl->AppendText(s); } // Setup the event handler and subscribe to connection events serverSocket->SetEventHandler(*this, SERVER_ID); serverSocket->SetNotify(wxSOCKET_CONNECTION_FLAG); serverSocket->Notify(true); serverState = listening; UpdateStatusBar(); }
void MyFrame::OnServerEvent(wxSocketEvent& event) { txtRx->AppendText(wxT("OnServerEvent: ")); wxSocketBase *sockBase; switch (event.GetSocketEvent()) { case wxSOCKET_CONNECTION: txtRx->AppendText(wxT("wxSOCKET_CONNECTION\n")); break; default: txtRx->AppendText(wxT("Unexpected event !\n")); break; } // Accept new connection if there is one in the pending // connections queue, else exit. We use Accept(false) for // non-blocking accept (although if we got here, there // should ALWAYS be a pending connection). sockBase = sock->Accept(false); if (sockBase) { IPaddress addr; if (!sockBase->GetPeer(addr)) { txtRx->AppendText(wxT("New connection from unknown client accepted.\n")); } else { txtRx->AppendText(wxString::Format(wxT("New client connection from %s:%u accepted \n"), addr.IPAddress(), addr.Service())); } } else { txtRx->AppendText(wxT("Error: couldn't accept a new connection \n")); return; } sockBase->SetEventHandler( *this, SOCKET_ID); sockBase->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); sockBase->Notify(true); numClients++; SetStatusText(wxString::Format(wxT("%d clients connected"),numClients), 1); }
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; }
// 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; }
void wxGISLocalServerConnection::OnSocketEvent(wxSocketEvent& event) { switch(event.GetSocketEvent()) { case wxSOCKET_INPUT: //send event to advisers wxLogDebug(wxT("wxGISNetServerConnection: INPUT")); break; case wxSOCKET_OUTPUT: wxLogDebug(wxT("wxGISNetServerConnection: OUTPUT")); //ProcessNetMessage(); break; case wxSOCKET_CONNECTION: wxLogDebug(wxT("wxGISNetServerConnection: CONNECTION")); break; case wxSOCKET_LOST: wxLogDebug(wxT("wxGISNetServerConnection: LOST")); { IPaddress addr; if (!m_pSock->GetPeer(addr)) { wxLogMessage(_("User #%d is disconnected"), m_nUserId); } else { wxLogMessage(_("User #%d from %s:%d is disconnected"), m_nUserId, addr.IPAddress().c_str(), addr.Service()); } m_bIsConnected = false; //send bye to the app wxNetMessage msgin(enumGISNetCmdBye, enumGISNetCmdStUnk, enumGISPriorityHigh); PostEvent(new wxGISNetEvent(m_nUserId, wxGISNET_MSG, msgin)); //as connection is lost we destroy itself Destroy(); } break; default: break; } }
void wxGISLocalNetworkPlugin::OnTCPServerEvent(wxSocketEvent& event) { switch(event.GetSocketEvent()) { case wxSOCKET_INPUT: wxLogError(_("wxGISLocalNetworkPlugin: Unexpected wxSOCKET_INPUT in wxSocketServer")); break; case wxSOCKET_OUTPUT: wxLogError(_("wxGISLocalNetworkPlugin: Unexpected wxSOCKET_OUTPUT in wxSocketServer")); break; case wxSOCKET_CONNECTION: { // Accept new connection if there is one in the pending // connections queue, else exit. We use Accept(false) for // non-blocking accept (although if we got here, there // should ALWAYS be a pending connection). wxSocketBase* sock = m_listeningSocket->Accept(false); IPaddress addr; if (!sock->GetPeer(addr)) { wxLogError(_("wxGISLocalNetworkPlugin: cannot get peer info")); } else { wxLogMessage(_("wxGISLocalNetworkPlugin: Got connection from %s:%d"), addr.IPAddress().c_str(), addr.Service()); } wxGISLocalServerConnection* pSrvConn = new wxGISLocalServerConnection(); pSrvConn->SetSocket(sock); m_pNetService->AddConnection(pSrvConn); } break; case wxSOCKET_LOST: wxLogError(_("wxGISLocalNetworkPlugin: Unexpected wxSOCKET_LOST in wxSocketServer")); break; } }