question about wxSocketBase::Read

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
mario
Earned a small fee
Earned a small fee
Posts: 11
Joined: Fri Jan 20, 2006 3:06 am
Contact:

question about wxSocketBase::Read

Post by mario » Sat Jan 21, 2006 4:21 am

the documentation for Read says
wxSocketBase& Read(void * buffer, wxUint32 nbytes)
This function reads a buffer of nbytes bytes from the socket.

Code: Select all

       char ch[32]={" "};
  ...
        m_socket->Read(ch,32);
if i send a string longer then the amount im readingi get erronious char's after 32 depending on the compile ive seen string with a length between 33-36(i also tried with a much large buffer but given the same "problem" it also showed erronious char's

after a long session of guess and check i tried

Code: Select all

     
       char ch[32]={" "};
  ...
        m_socket->Read(ch,31);

i get the appropriate response of 32 char's long in the string when i go to use it ..... is this an error in wxCode or an error in Documentation. or am i just in need of a refresher course.

(if you need more code id be glad to post it but i felt it wasnt really relavent for the question)

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Sat Jan 21, 2006 11:08 am

You Provide a buffer, that Read can read into,
you also handover the size of this buffer.
With wxSocketBase::LastCount you can figure out,
how many Bytes were read.

and your chars probably come from forgetting to
"end" your buffer with an '\0' after you read data to it.
buffer[mysocket->LastCount()]='\0';

jazz
Experienced Solver
Experienced Solver
Posts: 73
Joined: Thu Jun 09, 2005 12:37 am
Contact:

Post by jazz » Wed Jun 14, 2006 2:27 am

phlox81 wrote:and your chars probably come from forgetting to
"end" your buffer with an '\0' after you read data to it.
buffer[mysocket->LastCount()]='\0';
I'm getting this problem myself and I'm not quite sure this is correct.

Anywayz, here's some code I'm using to prove a point:

Code: Select all

char t_buf[MAX_PACKET];
sck.Read(t_buf, 10);
t_buf will then contain 10 bytes of the data received plus an 11th byte of random value followed by a NULL terminator. wxSocketBase::LastCount() does indeed return 10, but if I was to do a simple strlen(t_buf) it would return 11, meaning that the Read() function stuffed the NULL terminator into the 12th byte. Is it me, or is this just 'bad'?

Now for the kicker. It only does it if the count of bytes read is less than what is in the input queue. To prove this I sent 27 bytes of data to the socket, the first two 10 byte reads behaved as above with the extra eroneous byte, but the 3rd attempt was fine.

I'm going to implement phlox81's work-around but I seriously think this is a bug with how the Read() function handles NULL termination.

For reference, this should work around the problem:

Code: Select all

char buffer[MAX_PACKET+1];
sck.Read(buffer, MAX_PACKET);
buffer[mysocket->LastCount()] = '\0';
[INSERT LAME SIG HERE]

Post Reply