Socket testing using loopback not reliable? 
Author Message
 Socket testing using loopback not reliable?

Hi,

I've got a weird problem while testing my app. I'm creating a socket
based app using CAsyncSocket, and I need my server to send out a
couple of messages one after the other. The server code looks
something like this:

//accepting connection already done, it works fine

//data is a user-defined structure whose size is 100
m_socSr->Send(&data,sizeof(data));

//... do some processing to change a few data elements

m_socSr->Send(&data,sizeof(data));

//... do some processing to change a few data elements

m_socSr->Send(&data,sizeof(data));

That is, I'm sending three packets across, one after the other. My
client is already set up and ready to read.

Here's the problem: If I just run both app's simultaneously, my client
gets only the first packet correctly, the rest is garbage.

If on the other hand, I debug my server app (letting client run
normally), and step over the send statements manually, my client gets
all three packets fine!

If I debug and step into my client app and let the server app run
normally, I get only one packet correctly. Also, my client OnReceive()
gets called twice for every client->Send (for a total of 6 times). The
first time client->Receive() shows the correct size (100) and receives
the packet fine, the next time it shows a size of 12, the third time
it shows 100 but what's received is garbage, fourth again a size of
12, fifth 100 but garbage, sixth 12.

When I debug both my client and server and step over both, this is
what happens: I set a breakpoint on client->OnReceive() and on
server->Send()

I step over the first server->Send(). client->OnReceive() gets called,
and I read the packet correctly. Then OnReceive gets called again,
Receive() shows 12 bytes received (which I did not send and therefore
have no idea where it came from).

I then step over the second server->Send() statement with the same
results, and then the third. This way, I manage to receive all 3
packets correctly (along with 3 12bytes messages which I ignore).

Neither server->Send() nor client->Receive() returns with a
SOCKET_ERROR return value.

The important thing is that when both client server are running
normally without breakpoints, I get only one packet.

I'm really confused over this strange behavior. I'm using the loopback
address for testing (127.0.0.1) and my own port number (70). OS is
Win98, and development environment is VC++ 6.

I think it's something to do with the fact that the server sends 3
packets consecutively and maybe that's too much for the client to
handle or something, because it receives them fine if I step over the
statements in the server. Is this some sort of limitation with
loopback testing?

Can anyone help me out? Any pointers will be appreciated.

Regards,
Mustafa Lokhandwala



Thu, 21 Oct 2004 03:45:37 GMT  
 Socket testing using loopback not reliable?
Make sure you memset your data variable to 0 between writes.  Also, try
adding \n to the end of each message that you send.

Abe


Quote:
> Hi,

> I've got a weird problem while testing my app. I'm creating a socket
> based app using CAsyncSocket, and I need my server to send out a
> couple of messages one after the other. The server code looks
> something like this:

> //accepting connection already done, it works fine

> //data is a user-defined structure whose size is 100
> m_socSr->Send(&data,sizeof(data));

> //... do some processing to change a few data elements

> m_socSr->Send(&data,sizeof(data));

> //... do some processing to change a few data elements

> m_socSr->Send(&data,sizeof(data));

> That is, I'm sending three packets across, one after the other. My
> client is already set up and ready to read.

> Here's the problem: If I just run both app's simultaneously, my client
> gets only the first packet correctly, the rest is garbage.

> If on the other hand, I debug my server app (letting client run
> normally), and step over the send statements manually, my client gets
> all three packets fine!

> If I debug and step into my client app and let the server app run
> normally, I get only one packet correctly. Also, my client OnReceive()
> gets called twice for every client->Send (for a total of 6 times). The
> first time client->Receive() shows the correct size (100) and receives
> the packet fine, the next time it shows a size of 12, the third time
> it shows 100 but what's received is garbage, fourth again a size of
> 12, fifth 100 but garbage, sixth 12.

> When I debug both my client and server and step over both, this is
> what happens: I set a breakpoint on client->OnReceive() and on
> server->Send()

> I step over the first server->Send(). client->OnReceive() gets called,
> and I read the packet correctly. Then OnReceive gets called again,
> Receive() shows 12 bytes received (which I did not send and therefore
> have no idea where it came from).

> I then step over the second server->Send() statement with the same
> results, and then the third. This way, I manage to receive all 3
> packets correctly (along with 3 12bytes messages which I ignore).

> Neither server->Send() nor client->Receive() returns with a
> SOCKET_ERROR return value.

> The important thing is that when both client server are running
> normally without breakpoints, I get only one packet.

> I'm really confused over this strange behavior. I'm using the loopback
> address for testing (127.0.0.1) and my own port number (70). OS is
> Win98, and development environment is VC++ 6.

> I think it's something to do with the fact that the server sends 3
> packets consecutively and maybe that's too much for the client to
> handle or something, because it receives them fine if I step over the
> statements in the server. Is this some sort of limitation with
> loopback testing?

> Can anyone help me out? Any pointers will be appreciated.

> Regards,
> Mustafa Lokhandwala



Thu, 21 Oct 2004 04:54:56 GMT  
 Socket testing using loopback not reliable?

Quote:

> I think it's something to do with the fact that the server sends 3
> packets consecutively and maybe that's too much for the client to
> handle or something, because it receives them fine if I step over the
> statements in the server. Is this some sort of limitation with
> loopback testing?

> Can anyone help me out? Any pointers will be appreciated.

> Regards,
> Mustafa Lokhandwala

Your problem is not related to loopback testing but to your
misunderstanding of sockets.  Throughout your description you referred
to receiving packets.  Socket code does not receive packets.

Your code must accept whatever number of bytes it gets from Receive and
reassemble your application-specific "packets" based on knowledge of the
data structure being sent.  The number of bytes you will get from each
call to Receive is unpredictable and unrelated to the number of bytes
that were passed to Send.

--
Scott McPhillips [VC++ MVP]



Thu, 21 Oct 2004 08:04:01 GMT  
 Socket testing using loopback not reliable?
Hi,

Yup, figured it out. I was erroneously assuming that
client->OnReceive() would get called for each server->Send().
Unfortunately, TCP does not guaratee that packet boundries will be
preserved. OnReceive() was reading one packet, plus a part of the next
one, which was getting ignored by my app.

Thanks a lot!

Regards,
Mustafa Lokhandwala

Quote:


> > I think it's something to do with the fact that the server sends 3
> > packets consecutively and maybe that's too much for the client to
> > handle or something, because it receives them fine if I step over the
> > statements in the server. Is this some sort of limitation with
> > loopback testing?

> > Can anyone help me out? Any pointers will be appreciated.

> > Regards,
> > Mustafa Lokhandwala

> Your problem is not related to loopback testing but to your
> misunderstanding of sockets.  Throughout your description you referred
> to receiving packets.  Socket code does not receive packets.

> Your code must accept whatever number of bytes it gets from Receive and
> reassemble your application-specific "packets" based on knowledge of the
> data structure being sent.  The number of bytes you will get from each
> call to Receive is unpredictable and unrelated to the number of bytes
> that were passed to Send.



Thu, 21 Oct 2004 15:43:45 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. socket doesn't work in loopback mode

2. Find or Search not reliable in VS97

3. TRACE is not being reliable

4. PPP - authentication not reliable

5. Serializing: Works FileStream or SoapFormatter not reliable?

6. reliable message passing using pthreads?

7. FTP and sockets (not using WinInet)?

8. Socket programming: Using IP_ADD_MEMBERSHIP is not defined???

9. FTP and sockets (not using WinInet)?

10. test test test test

11. multicast loopback control

12. Can you bind to Loopback I/F only?

 

 
Powered by phpBB® Forum Software