Question about using wxSocketBase::Peek() Topic is solved

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.
Post Reply
00061205
Knows some wx things
Knows some wx things
Posts: 41
Joined: Mon Jun 16, 2008 3:43 am
Location: Beijing, China

Question about using wxSocketBase::Peek()

Post 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();
}
Attachments
未标题-1.jpg
Regards,

00061205
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post 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
00061205
Knows some wx things
Knows some wx things
Posts: 41
Joined: Mon Jun 16, 2008 3:43 am
Location: Beijing, China

Post 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:
Regards,

00061205
FireMail
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Jun 10, 2005 8:34 am
Location: Austria
Contact:

Post 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
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GB/CS/CM/IT !d++ s+:-- a-- C++++$ UBL*++++$ P--- L++++$ !E-- !W+++$? !N-- !o K--? w++()$ !O M$ !V !PS? !PE? !Y? !PGP !t !5 !X R+++ tv++ !b? DI D++ G e+++ h++ r++ y+
------END GEEK CODE BLOCK------
00061205
Knows some wx things
Knows some wx things
Posts: 41
Joined: Mon Jun 16, 2008 3:43 am
Location: Beijing, China

Post by 00061205 »

Thanks a lot, FireMail. :D
Regards,

00061205
sago
Earned a small fee
Earned a small fee
Posts: 18
Joined: Tue Oct 21, 2008 7:59 am

wxYield called recursively

Post 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.
Post Reply