Page 1 of 1

Question about using wxSocketBase::Peek()

Posted: Thu Jul 24, 2008 12:33 pm
by 00061205
A message box showed up when i run my app. No errors show when compile and link.
I just want to check wether there are more data in socket to be read after "sock->Read(cBuf,sizeof(cBuf)-1);".

Code: Select all

void netdbgFrame::OnSocketEvent(wxSocketEvent& event)
{
    wxString s = _("");
    char cBuf [16];
    char cPeekBuf[16];
    wxString wxstrBuf;
    wxSocketBase *sock = event.GetSocket();
    sock->SetFlags(wxSOCKET_WAITALL);

    switch (event.GetSocketEvent())
    {
    case wxSOCKET_INPUT      :
        memset( cBuf, 0, 16 );
        sock->Read(cBuf,sizeof(cBuf)-1);
        sock->Peek(cPeekBuf,sizeof(cPeekBuf));
        if (sock->LastCount()!=0)
        {
            strtemp+=cBuf;
        }
        else
        {
            strtemp+=cBuf;
            wxstrBuf=wxString(strtemp.data(), *wxConvCurrent);
            s.Append(wxstrBuf);
        }
        break;
    case wxSOCKET_LOST       :
        s.Append(_("wxSOCKET_LOST\n"));
        break;
    case wxSOCKET_CONNECTION :
        s.Append(_("wxSOCKET_CONNECTION\n"));
        break;
    default                  :
        s.Append(_("Unexpected event !\n"));
        break;
    }
    C_TextRev->AppendText(s);

    UpdateStatusBar();
}

Posted: Thu Jul 24, 2008 3:00 pm
by mc2r
FireMail had a similiar error with wxSocketClient and wxSocketServer in this thread. FireMail mentions you can message him and he will provide more details.

-Max

Posted: Fri Jul 25, 2008 1:25 am
by 00061205
mc2r wrote:FireMail had a similiar error with wxSocketClient and wxSocketServer in this thread. FireMail mentions you can message him and he will provide more details.

-Max
wow, :shock:

Posted: Fri Jul 25, 2008 7:48 am
by FireMail
hi there,

there was a good entry in the wxBook.

wxSockets do have flags that define how they act (blocking GUI while they receive data, receive all data at once and so on).
My problem was that i only used a single thread including GUI and sockets. You need now to imagine that without "blocking GUI"-flag the receive (or peek in your case) function calls wxYield not to block the GUI.

So you have two ways for solving that problem:
1) you stay at single thread application using blocking flag for your socket (i did not choose that method)
2) you launch the socket in a second thread (wxThread - i've chosen this method).

Ad 1) with blocking flag wxYield is not called and the GUI freezes until data arrives
Ad 2) in a second thread wxYield is called, but it does not matter because there is no GUI.


when you choose the 2nd method (second thread), be aware that you must not call GUI functions from the second thread! you need to call events because wx is not thread safe.


hope i could help you

Posted: Fri Jul 25, 2008 1:29 pm
by 00061205
Thanks a lot, FireMail. :D

wxYield called recursively

Posted: Mon Nov 03, 2008 9:39 am
by sago
FireMail wrote:hi there,

there was a good entry in the wxBook.

wxSockets do have flags that define how they act (blocking GUI while they receive data, receive all data at once and so on).
My problem was that i only used a single thread including GUI and sockets. You need now to imagine that without "blocking GUI"-flag the receive (or peek in your case) function calls wxYield not to block the GUI.

So you have two ways for solving that problem:
1) you stay at single thread application using blocking flag for your socket (i did not choose that method)
2) you launch the socket in a second thread (wxThread - i've chosen this method).

Ad 1) with blocking flag wxYield is not called and the GUI freezes until data arrives
Ad 2) in a second thread wxYield is called, but it does not matter because there is no GUI.


when you choose the 2nd method (second thread), be aware that you must not call GUI functions from the second thread! you need to call events because wx is not thread safe.


hope i could help you
Hi FireMail !

I have the same problem as you. But I use wxTCPServer and wxTCPConnection classes, which use wxSocketBase class. So the flag is wxSOCKET_WAITALL (only return when it has read or written ALL the data, but the GUI does not block). So I have tried to use threads but the problem always appears.

Here is my post for this problem:
http://forums.wxwidgets.org/viewtopic.p ... ht=wxyield

Thx in advance.