wxSocketBase::Unread usage 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
zobbo
Experienced Solver
Experienced Solver
Posts: 58
Joined: Sat Aug 18, 2007 4:41 am

wxSocketBase::Unread usage

Post by zobbo »

Hi,

I'm trying to use SocketBase::Unread for testing purposes. What I do is read the data from the socket via wxSocketClient and then unread it so it can be read again. The trouble is, it will only unread if my buffer is less than what is going to be read. For this test I expect 51 bytes, but if my buffer is 51, then Unread fails to work. I set it to 50 and it works, but obviously it chops off a byte. I don't set any socket flags so it will default to wxSOCKET_NONE.

Code: Select all


char* Unreadtest::inputTest_ = new char[100];

void Unreadtest::OnSocketEvent(wxSocketEvent &event)
{

int len = 50; //reading 51 causes unread to fail
int num =0;

std::cout << "UnreadTest: OnSocketEvent\n";
	
switch(event.GetSocketEvent())
  {
    case wxSOCKET_INPUT:
    {   
	server_->Read(inputTest_,len);
	num = server_->LastCount();
		
	server_->Unread(inputTest_,num);
	inputTest_[len] = '\0';
	break;
	}
	default: break;
  }

std::cout << "\nUnreadTest: buffer length is " << len << "\n";
std::cout << "\nUnreadTest: read num length is " << num << "\n";
std::cout << "\nUnreadTest: Read " << inputTest_ << "\n";

	loop.Exit();
}
The output I get for len=51 is:

UnreadTest OnServer Event Fired
UnreadTest: wxSOCKET_CONNECTION
UnreadTest: OnSocketEvent
UnreadTest: buffer length is 51
UnreadTest: read num length is 51
UnreadTest: Read 200 123-PC TEST1 Version 1.1b1,, S0, posting OK

The output for len=50 is:

UnreadTest OnServer Event Fired
UnreadTest: wxSOCKET_CONNECTION
UnreadTest: OnSocketEvent
UnreadTest: buffer length is 50
UnreadTest: read num length is 50
UnreadTest: Read 200 123-PC TEST1 Version 5.7b1,, S0, posting OK
UnreadTest: OnSocketEvent
UnreadTest: buffer length is 50
UnreadTest: read num length is 50
UnreadTest: Read 200 123-PC TEST1 Version 5.7b1,, S0, posting OK


So, I'm wondering why that is? Although I know it is 51 bytes for this test, I won't know what length the input will be exactly each time, so it is a problem.

Cheers,

Zobbo
Zobbo

Wxwidgets 2.9.0, Visual C++ Express 2005, Windows Vista x64 Home Premium
JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway »

Hi Zobbo,
So, I'm wondering why that is? Although I know it is 51 bytes for this test, I won't know what length the input will be exactly each time, so it is a problem.
I think that whenever you read less than the amount of data that is available from the socket, it will fire another wxSOCKET_INPUT event.
So if you send 51 bytes and read 50, it will send you a second event (as you have 1 more byte from the other side you haven't seen).
But if you read send 51 and read 51, a second event will not fire, even if you unread the data until more data comes in from the other side.

Another way to think of it: generally, unread is used because a partial message from the other side has arrived and there's no where to put it, so it's pushed back into the socket and until more data from the other side arrives to finish processing the partial component.


Hope that's clear...

Jim
OS: Vista SP1, wxWidgets 2.8.7.
zobbo
Experienced Solver
Experienced Solver
Posts: 58
Joined: Sat Aug 18, 2007 4:41 am

Post by zobbo »

JimFairway wrote:Hi Zobbo,

Another way to think of it: generally, unread is used because a partial message from the other side has arrived and there's no where to put it, so it's pushed back into the socket and until more data from the other side arrives to finish processing the partial component.


Hope that's clear...

Jim
Thanks Jim. Yes, that makes perfect sense. I guess my understanding was a bit off. I thought Unread caused the new event, but it's because the buffer hasn't been fully read. I have found another way to test which I'm happy with.

Cheers,
Zobbo

Wxwidgets 2.9.0, Visual C++ Express 2005, Windows Vista x64 Home Premium
Post Reply