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

wxSocketBase: Exiting after reading data

Post by evstevemd »

I have wxSocketClient created with default parameters in Ctor and at wxSOCKET_INPUT I read all the data and it works fine. The problem is, it does not exit the loop and so I never get message "LOOP DONE", Here is the code that does the reading. I expected it to exit on error or when it could not read data anymore but it is not the case. Any light on this is appreciated!

Code: Select all

void ReadData()
{
    char c = 0x00;
    wxString data;

    wxPuts("Receiving data...");

    while(IsConnected() && (!Error() || LastReadCount() < 1)) {
        // read a char
        Read(&c, 1);

        if(c == '\r') continue;

        if(c == '\n' && !data.IsEmpty()) {
            wxPuts(data); //Works fine here!
            
            // clear for next iteration if any
            data.Clear();
        }
        else
        {
            if(c != '\n' && c != '\r') data.Append((wxChar)c, 1);
        }
    }
    wxLogMessage("LOOP DONE!");
}
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 »

What flags do you have set with SetFlags()? This will change the behavior of Read().

Also, out of interest, why are you subclassing wxSocketClient? I think this isn't necessary for a simple transfer.

Finally, I found some good examples of simple socket use here: http://cool-emerald.blogspot.com/2018/0 ... dgets.html that might be of interest.
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.
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 »

Are you sure that your condition in while loop is correct?
What if rewrite it in this way:

Code: Select all

while(IsConnected() && !Error())
{
Read(&c, 1);
if(LastReadCount() == 0)
{
    break;
}
...
}
 
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 10:00 am What flags do you have set with SetFlags()? This will change the behavior of Read().
//ctor

Code: Select all

wxSocketClient() //used  defaults

Code: Select all

//flags for notify in case they are necessary
SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG | wxSOCKET_CONNECTION_FLAG);
That is all I have set.
iwbnwif wrote: Fri Jun 07, 2019 10:00 am Also, out of interest, why are you subclassing wxSocketClient? I think this isn't necessary for a simple transfer.
Aha!, I was experimenting with many stuffs. Definitely in the real app, it won't be so. wxSocketClient will be just a member of the class not subclassed
iwbnwif wrote: Fri Jun 07, 2019 10:00 am Finally, I found some good examples of simple socket use here: http://cool-emerald.blogspot.com/2018/0 ... dgets.html that might be of interest.
Thank you for the link. I will check it out and let you know if I get anything out of it
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 »

Kvaz1r wrote: Fri Jun 07, 2019 10:04 am Are you sure that your condition in while loop is correct?
What if rewrite it in this way:

Code: Select all

while(IsConnected() && !Error())
{
Read(&c, 1);
if(LastReadCount() == 0)
{
    break;
}
...
}
 
Looking at it, it does the same thing only different positioning. Yours check immediately after reading, mine checks before next iteration of while loop. I don't think the condition is the issue. No?
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 »

Code: Select all

//flags for notify in case they are necessary
SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG | wxSOCKET_CONNECTION_FLAG);
That is all I have set.
You may need to set:

Code: Select all

SetFlags(wxSOCKET_NOWAIT)
For Read() not to wait for data and for LastReadCount() to return 0.
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 10:40 am

Code: Select all

//flags for notify in case they are necessary
SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG | wxSOCKET_CONNECTION_FLAG);
That is all I have set.
You may need to set:

Code: Select all

SetFlags(wxSOCKET_NOWAIT)
For Read() not to wait for data and for LastReadCount() to return 0.
Let me add that right now
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
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSocketBase: Exiting after reading data

Post by doublemax »

I know very little about sockets, but isn't a socket connection like a phone call? You don't know when the conversation is finished until one of the partners "hangs up" or if they agreed on some method to indicate it's finished? (Like saying "bye").

In the sockets sample the first byte of the message is the length of the message, so that the receiver knows when it's complete.
Use the source, Luke!
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 »

adding the flag causes it read only once and never hit the end of the loop. So the problem is even bigger now :)
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 »

doublemax wrote: Fri Jun 07, 2019 10:55 am I know very little about sockets, but isn't a socket connection like a phone call? You don't know when the conversation is finished until one of the partners "hangs up" or if they agreed on some method to indicate it's finished? (Like saying "bye").

In the sockets sample the first byte of the message is the length of the message, so that the receiver knows when it's complete.
That is correct for for some flags (see here)

Some behave differently
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?
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 »

evstevemd wrote: Fri Jun 07, 2019 10:31 am
Kvaz1r wrote: Fri Jun 07, 2019 10:04 am Are you sure that your condition in while loop is correct?
What if rewrite it in this way:

Code: Select all

while(IsConnected() && !Error())
{
Read(&c, 1);
if(LastReadCount() == 0)
{
    break;
}
...
}
 
Looking at it, it does the same thing only different positioning. Yours check immediately after reading, mine checks before next iteration of while loop. I don't think the condition is the issue. No?
It really can be checked easily ;-)
So in your case loop will works while this condition is true:

Code: Select all

(!Error() || LastReadCount() < 1)
and it will be false only when both condition will false. And I really don't know when Error/0 will return true. So you can even not check LastReadCount/0 at all because this part will evaluating only when first is false.
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:16 am It really can be checked easily ;-)
Bingo! let me check :)
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 »

@kvaz1r it does not work. No difference at all
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 »

adding the flag causes it read only once and never hit the end of the loop. So the problem is even bigger now :)
Check the program flow mentioned by Kvaz1r. I think that you need both the flag and to adjust the 'if' condition.
I know very little about sockets, but isn't a socket connection like a phone call? You don't know when the conversation is finished until one of the partners "hangs up" or if they agreed on some method to indicate it's finished? (Like saying "bye").
I also don't know much about sockets, but I think it is possible to use wxSOCKET_NOWAIT to put the speakerphone on and do something else whilst waiting for the other party to talk (or say something interesting) :wink:
In the sockets sample the first byte of the message is the length of the message, so that the receiver knows when it's complete.
The sample is a little outdated, we now have WriteMsg() and ReadMsg() to do the same thing. These shouldn't be used with wxSOCKET_NOWAIT though.
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 11:26 am Check the program flow mentioned by Kvaz1r. I think that you need both the flag and to adjust the 'if' condition.
It does not change the situation at all, after all adjustment
iwbnwif wrote: Fri Jun 07, 2019 11:26 am I also don't know much about sockets, but I think it is possible to use wxSOCKET_NOWAIT to put the speakerphone on and do something else whilst waiting for the other party to talk (or say something interesting) :wink:


Sockets seems to be least known subject to many and am not an exception ;)
In the sockets sample the first byte of the message is the length of the message, so that the receiver knows when it's complete.
The sample is a little outdated, we now have WriteMsg() and ReadMsg() to do the same thing. These shouldn't be used with wxSOCKET_NOWAIT though.
[/quote]
I will try them out later to see if there is any difference at all!
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