wxSocketBase: Exiting after reading data 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.
Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 357
Joined: Tue Jun 07, 2016 1:07 pm

Re: wxSocketBase: Exiting after reading data

Post by Kvaz1r »

In that case the only thing I can propose - provide SSCCE for reproducing behaviour.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: wxSocketBase: Exiting after reading data

Post by evstevemd »

Kvaz1r wrote: Fri Jun 07, 2019 11:51 am In that case the only thing I can propose - provide SSCCE for reproducing behaviour.
I don't do anything peculiar than information I have provided. All I do is:
1. create wxSocketClient passing no argume.
2. Set Notify flags
3. Write to Socket
4. In response to Socket read event I just call the method we have been discussing.

Nothing peculiar as you can see!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
iwbnwif
Super wx Problem Solver
Super wx Problem Solver
Posts: 282
Joined: Tue Mar 19, 2013 8:52 pm

Re: wxSocketBase: Exiting after reading data

Post by iwbnwif »

evstevemd wrote: Fri Jun 07, 2019 12:21 pm 4. In response to Socket read event I just call the method we have been discussing.
If you are responding to a wxSOCKET_INPUT event, then you can simply use Read(buffer, len) to read all available bytes and return. I am pretty sure that Read is guaranteed to get something if a wxSOCKET_INPUT event has occurred.

The following works for me in both Windows and Linux.

Code: Select all

void MainFrame::OnSocketEvent(wxSocketEvent& event)
{
    wxLogMessage("OnSocketEvent: ");
    wxSocketBase* sockBase = event.GetSocket();

    // Process the event.
    switch (event.GetSocketEvent())
    {
        case wxSOCKET_INPUT:
        {
            wxLogMessage("wxSOCKET_INPUT\n"); 

            char buf[256];
            
            // Read the message
            wxUint32 lenRd = sockBase->Read(buf, 256).LastCount();

            if (!lenRd) 
            {
                wxLogMessage("Failed to read any data.\n");
                return;
            }
            
            else 
            {
                wxLogMessage("Read %d bytes.\n", lenRd);
            }

            wxLogMessage("Rx: %s \n", wxString::FromUTF8(buf, lenRd));
            break;
        }
      ...
wxWidgets 3.1.2, MinGW64 8.1.0, g++ 8.1.0, Ubuntu 19.04, Windows 10, CodeLite + wxCrafter
Some people, when confronted with a GUI problem, think "I know, I'll use Eclipse RCP". Now they have two problems.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: wxSocketBase: Exiting after reading data

Post by evstevemd »

iwbnwif wrote: Fri Jun 07, 2019 1:03 pm
evstevemd wrote: Fri Jun 07, 2019 12:21 pm 4. In response to Socket read event I just call the method we have been discussing.
If you are responding to a wxSOCKET_INPUT event, then you can simply use Read(buffer, len) to read all available bytes and return. I am pretty sure that Read is guaranteed to get something if a wxSOCKET_INPUT event has occurred.

The following works for me in both Windows and Linux.

Code: Select all

void MainFrame::OnSocketEvent(wxSocketEvent& event)
{
    wxLogMessage("OnSocketEvent: ");
    wxSocketBase* sockBase = event.GetSocket();

    // Process the event.
    switch (event.GetSocketEvent())
    {
        case wxSOCKET_INPUT:
        {
            wxLogMessage("wxSOCKET_INPUT\n"); 

            char buf[256];
            
            // Read the message
            wxUint32 lenRd = sockBase->Read(buf, 256).LastCount();

            if (!lenRd) 
            {
                wxLogMessage("Failed to read any data.\n");
                return;
            }
            
            else 
            {
                wxLogMessage("Read %d bytes.\n", lenRd);
            }

            wxLogMessage("Rx: %s \n", wxString::FromUTF8(buf, lenRd));
            break;
        }
      ...
That sounds great except I don't know buffer size. May be I have to make it the biggest possible. I will try that option too!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: wxSocketBase: Exiting after reading data

Post by evstevemd »

iwbnwif wrote: Fri Jun 07, 2019 1:03 pm
evstevemd wrote: Fri Jun 07, 2019 12:21 pm 4. In response to Socket read event I just call the method we have been discussing.
If you are responding to a wxSOCKET_INPUT event, then you can simply use Read(buffer, len) to read all available bytes and return. I am pretty sure that Read is guaranteed to get something if a wxSOCKET_INPUT event has occurred.

The following works for me in both Windows and Linux.

Code: Select all

void MainFrame::OnSocketEvent(wxSocketEvent& event)
{
    wxLogMessage("OnSocketEvent: ");
    wxSocketBase* sockBase = event.GetSocket();

    // Process the event.
    switch (event.GetSocketEvent())
    {
        case wxSOCKET_INPUT:
        {
            wxLogMessage("wxSOCKET_INPUT\n"); 

            char buf[256];
            
            // Read the message
            wxUint32 lenRd = sockBase->Read(buf, 256).LastCount();

            if (!lenRd) 
            {
                wxLogMessage("Failed to read any data.\n");
                return;
            }
            
            else 
            {
                wxLogMessage("Read %d bytes.\n", lenRd);
            }

            wxLogMessage("Rx: %s \n", wxString::FromUTF8(buf, lenRd));
            break;
        }
      ...
This solves my issue.
Thanks everyone who helped!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Post Reply