Page 1 of 2
wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 9:30 am
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!");
}
Re: wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 10:00 am
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.
Re: wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 10:04 am
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;
}
...
}
Re: wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 10:29 am
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
//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
Thank you for the link. I will check it out and let you know if I get anything out of it
Re: wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 10:31 am
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?
Re: wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 10:40 am
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:
For Read() not to wait for data and for LastReadCount() to return 0.
Re: wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 10:50 am
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:
For Read() not to wait for data and for LastReadCount() to return 0.
Let me add that right now
Re: wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 10:55 am
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.
Re: wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 11:00 am
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
Re: wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 11:04 am
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
Re: wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 11:16 am
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:
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.
Re: wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 11:18 am
by evstevemd
Kvaz1r wrote: ↑Fri Jun 07, 2019 11:16 am
It really can be checked easily
Bingo! let me check
Re: wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 11:21 am
by evstevemd
@kvaz1r it does not work. No difference at all
Re: wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 11:26 am
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)
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.
Re: wxSocketBase: Exiting after reading data
Posted: Fri Jun 07, 2019 11:40 am
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)
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!