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.
I'm using the Sockets example i just want to sent datas from the server to the connected clients every 500ms. for this i created a timer in the server class which calls following event:
void MyFrame::OnTimer(wxTimerEvent&event)
{
if (m_server->IsConnected())
{
const wxChar* buf1 = _("Test Message(less than 256 chars!)");
int len = (unsigned char)((wxStrlen(buf1) + 1) * sizeof(wxChar));
//m_server->SetFlags(wxSOCKET_WAITALL);
m_server->SetFlags(wxSOCKET_NOWAIT);
m_server->Write(buf1, len);
if (m_server->Error())
{
if (m_server->LastError() == wxSOCKET_INVADDR )
wxLogDebug(wxT("wxSOCKET_INVADDR"));
else if (m_server->LastError() == wxSOCKET_IOERR )
wxLogDebug(wxT("wxSOCKET_IOERR "));
else if (m_server->LastError() == wxSOCKET_INVOP )
wxLogDebug(wxT("wxSOCKET_INVOP "));
else if (m_server->LastError() == wxSOCKET_INVSOCK )
wxLogDebug(wxT("wxSOCKET_INVSOCK "));
else if (m_server->LastError() == wxSOCKET_INVPORT )
wxLogDebug(wxT("wxSOCKET_INVPORT "));
else if (m_server->LastError() == wxSOCKET_WOULDBLOCK )
wxLogDebug(wxT("wxSOCKET_WOULDBLOCK "));
else if (m_server->LastError() == wxSOCKET_TIMEDOUT )
wxLogDebug(wxT("wxSOCKET_TIMEDOUT "));
else if (m_server->LastError() == wxSOCKET_MEMERR )
wxLogDebug(wxT("wxSOCKET_MEMERR "));
else if (m_server->LastError() == wxSOCKET_MEMERR )
wxLogDebug(wxT("wxSOCKET_NOERROR "));
}
}
}
i get the error message wxSOCKET_INVSOCK with the code above.
When i use wxSOCKET_WAITALL it crash at the Write method.
I made no modification in the client or elsewhere in the server (without the timer methods m_timer->SetOwner(this,TIMER_ID); and m_timer->Start(500); )
The same Write function works inside each of the 3 Tests.
Hi!
In addition to what DonSixto said, I conclude that m_server is the wxSocketServer. This is used to listen for incoming connections and accept them. It is *not* the end point used for communication.
In OnServerEvent() the m_server accepts an incoming connection with a new wxSocketBase. This socket is the end point and you can write date to it, which will be received by the client. Feeding data to the server itself doesn't do anything, as it is not a connected end point. It more or less simple lurks around waiting for connections.
To do what you want, modify OnServerEvent() and store the sock pointer in an array or vector. In you timer iterate over the vector and send the data to each sock in the vector.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4
"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
Thanks for the quick help. Now my server sends without an error message, but my client OnSocketEvent event handler doesn't respond on the sent packages
That's how i establish the connection on the client side.
Hi!
So connecting the client to the server and accepting the connection on the server works, right?
Can we see the potion of code where you send from the server and receive in the client?
Besides that, m_numClients++; is pretty useless when there is only one connection . And you should use Destroy() instead of simply deleting the socket.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4
"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
Hmm..
Maybe a stupid question, but did you use SetEventHandler() on the client socket? I don't see it in your OnOpenConnection()... If no event handler is assigned, not event would be received.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4
"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
// Create the socket
m_sock = new wxSocketClient();
// Setup the event handler and subscribe to most events
m_sock->SetEventHandler(*this, SOCKET_ID);
m_sock->SetNotify(wxSOCKET_CONNECTION_FLAG |
wxSOCKET_INPUT_FLAG |
wxSOCKET_LOST_FLAG);
m_sock->Notify(true);
I overlooked that i receive the socket event exactly one time.
So in the client output window appears:
Welcome to wxSocket demo: Client
Client ready
Trying to connect (timeout = 10 sec) ...
Succeeded ! Connection established
OnSocketEvent: wxSOCKET_INPUT
Hi!
The problem seems to be that you never read anything from the socket. I guess it should still emit the event, but it doesn't. However, I used Discard() in the client on input and I was able to receive the next event.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4
"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
Actually this problem does not arise in the sample, as with each of the tests the socket gets read after sending some data.
I still wonder why this happens... I guess, no, I know that wxSocket classes could need some more work and bug fixing.
Well, glad it works at last
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4
"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda