views:

330

answers:

2

Hi! I need some help understanding a peculiar issue I'm having when using asio.

I've a client -server app, with a c++ client(using boost asio) sending 2 byte hearbeat (say, every second) to a server(written in java) (and receiving lots of data as well).

for a quite a few minutes the server correctly receives the 2 byte HeartBeat, but after that the server's 'read' complains abt a 0 byte read, and closes the connection (which I guess is correct for a blocking read). The client however always prints out that it's been transferring the correct amount.

I've experimented with almost all variants of the 'write' family of functions. are all of them implemented in terms of 'write_some' and does that mean that this behavior is expected?

I must be making some mistake in my usage, basically I'm looking for something within asio that guarantees a write ( at least a byte) . please help me figure out where I'm going wrong(and if any further info is reqd.)... any advice, most appreciated! thanks!

+1  A: 

If it's sockets, you can't "guarantee a write"; what if the network is down, the cable yanked out, the switch is on fire, or the power is out worldwide and your computer happens to be the only one running on batteries?

That said, it sounds as if you have some kind of buffering/emptying issue perhaps, check over your read code to make sure it really consumes all data that appears.

A 0-byte read is not an error, look over that code again, check for any error status flags on the socket(s) and so on. A read can fail with a "AGAIN"-status, which really means you should try again.

unwind
Hi Unwind, thanks for your reply.The 'read' is in the java server , and going by the code, it seems to be like the classic 'select' followed by a ('blocking') 'read', ( I thought EAGAIN was only if non-blocking was set) . all it's other java clients have no problem.I'm a bit confused on 0 byte read on a 'normal' blocking read call ( all the n/w books and docs mention read<=0 as a eof/failure ).. while a blocking write can be short(and not tfr all the bytes in one go) , can it ever be sending a 0 tcp payload?
read returning 0 is pretty much a standard way of saying "the other end closed the tcp connection". Verify it with e.g. a network monitor like wireshark.
nos
A: 

strace the applications at both ends. It will show any error codes that are returned by read(), write() etc. Use strace -f if the application is multithreaded.

The advantage of this approach is that all applications - java, c++, python appear the same in an strace, so it's easy to spot bad behaviour.

In this case, it would probably show that the tcp connection ended (gracefully).

Matthew Herrmann