Input Available Logs in wxWidgets socket example with for-loop in Client.cpp OnTest2 function

  c++, mingw, windows, wxwidgets

I am beginning to learn about sockets in wxWidgets, and run on Windows 10 (latest version), CodeBlocks 20.03 with mingw2 (gnu 11.2), and wxWidgets 3.15.
The socket sample compiles and run OK.
Because I will be sending many wxStrings (up to 7000 in one session) from the client to the server, I added a for-loop in the client.cpp OnTest2 function (see below).

Where the single transfer of the original socket sample runs OK every time, the adapted client.cpp OnTest2 function always logs Input-Available events at various locations.
For some reason "Input-Available" is logged on all sends.
The Server receives everything, so I am not worried that much about the future content.

Still, these Input-Available Log messages should not happen, I think.
I read somewhere that calling wxThread::Yield() prior to calling WaitForRead() would help, but it did not.

Is something wrong in my "simple" approach of adding a for-loop?
I hope someone can help me understand why these Input-Available log message appear,
and help me find a solution.

Client OnTest2 with for-loop running 10 times

void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event))
{
  // Disable socket menu entries (exception: Close Session)
  m_busy = true;
  UpdateStatusBar();

  m_text->AppendText(_("n=== Test 2 begins ===n"));

for(int i = 0; i < 10; i++)
{
    // Tell the server which test we are running
    unsigned char c = 0xCE;
    m_sock->Write(&c, 1);

    // Here we use ReadMsg and WriteMsg to send messages with
    // a header with size information. Also, the reception is
    // event triggered, so we test input events as well.
    //
    // We need to set no flags here (ReadMsg and WriteMsg are
    // not affected by flags)

    m_sock->SetFlags(wxSOCKET_WAITALL);

    wxString s = "Yes I like to test sockets!";

    const wxScopedCharBuffer msg1(s.utf8_str());
    size_t len  = wxStrlen(msg1) + 1;
    wxCharBuffer msg2(wxStrlen(msg1));

    m_text->AppendText(_("Sending the string with WriteMsg ..."));
    m_sock->WriteMsg(msg1, len);
    m_text->AppendText(m_sock->Error() ? _("failed !n") : _("donen"));
    m_text->AppendText(_("Waiting for an event (timeout = 2 sec)n"));

    // Wait until data available (will also return if the connection is lost)
    m_sock->WaitForRead(2);

    if (m_sock->IsData())
    {
      m_text->AppendText(_("Reading the string back with ReadMsg ..."));
      m_sock->ReadMsg(msg2.data(), len);
      m_text->AppendText(m_sock->Error() ? _("failed !n") : _("donen"));
       m_text->AppendText(_("Comparing the two buffers ..."));
      if (memcmp(msg1, msg2, len) != 0)
      {
        m_text->AppendText(_("failed!n"));
        m_text->AppendText(_("Test 2 failed !n"));
      }
      else
      {
        m_text->AppendText(_("donen"));
        m_text->AppendText(_("Test 2 passed !n"));
      }
    }
    else
      m_text->AppendText(_("Timeout ! Test 2 failed.n"));

    m_text->AppendText(_("=== Test 2 ends ===n"));
  }
  m_busy = false;
  UpdateStatusBar();
}

Server Log: (displays below 10 times. All transfers seem to be OK)

OnSocketEvent: wxSOCKET_INPUT
15:52:35: === Test 2 begins ===
15:52:35: Got "Yes I like to test sockets!" from client.
15:52:35: Sending the data back
15:52:35: === Test 2 ends ===

Client Log OK (8x)

=== Test 2 begins ===
Sending the string with WriteMsg ...done
Waiting for an event (timeout = 2 sec)
Reading the string back with ReadMsg ...done
Comparing the two buffers ...done
Test 2 passed !
=== Test 2 ends ===

Client Log with input available (in second and fourth send)

=== Test 2 begins ===
Sending the string with WriteMsg ...done
Waiting for an event (timeout = 2 sec)
15:52:35: Input available on the socket (2x in the fourth)
Reading the string back with ReadMsg ...done
Comparing the two buffers ...done
Test 2 passed !
=== Test 2 ends ===

And below the last (10th)

15:52:35: Input available on the socket
15:52:35: Input available on the socket
15:52:35: Input available on the socket
15:52:35: Input available on the socket
15:52:35: Input available on the socket
15:52:35: Input available on the socket
15:52:35: Input available on the socket

Source: Windows Questions

LEAVE A COMMENT