# Cinder-Asio's TcpCleint strange behavior

**URL:** <https://discourse.libcinder.org/t/cinder-asios-tcpcleint-strange-behavior/1896>\
**Category:** Uncategorized\
**Created:** [December 11, 2021, 7:45pm UTC](https://discourse.libcinder.org/t/cinder-asios-tcpcleint-strange-behavior/1896 "2021-12-11T19:45:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kino](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/kino/32/465_2.png) [@Kino](https://discourse.libcinder.org/u/Kino)\
**Post date:** [December 11, 2021, 7:45pm UTC](https://discourse.libcinder.org/t/cinder-asios-tcpcleint-strange-behavior/1896/1 "2021-12-11T19:45:40Z")

</div>

Hi all,

I posted this on the issues page of the Cinder-Asio github repo but since that repo looks inactive, I thought to ask it here as well and also to help others not to face the same issue in the future for whatever it’s worth. So here it goes:

I just wanted to point out a very strange behavior I faced when making a simple messaging system with a libcinder app as a TCP Client and a node.js TCP server on LAN:

Modifying the TcpClient sample, I removed the `mSession->close()` line from the end of the `onRead` method because unlike the sample I wanted to keep my connection alive with the server for many hours. I replaced that line with a “mSession-\>read()” because I wanted the client to keep listening of course and I needed it there because I didn’t necessarily write back to server after I read a buffer so I couldn’t just keep the `mSession->read()` at the end of the `onWrite()` method. I still kept that though so that the client could keep listening after every time it wrote a message to the sever. But as my code evolved I noticed that sometimes the client read an empty buffer in `onRead()` whereas it should have read a message. I played around with the code and realized that removing `mSession->read()` from `onWrite` mitigated the bug! Should that be the case? Why would having 2 `read()` calls on the session conflict each other? Is that an expected result or was I doing something wrong? On the whole, why do we need to say `mSession->read()` every time and can’t we have a client that is constantly listening/reading?

---

<div class="post-metadata">

**Author:** ![Kino](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/kino/32/465_2.png) [@Kino](https://discourse.libcinder.org/u/Kino)\
**Post date:** [December 11, 2021, 7:48pm UTC](https://discourse.libcinder.org/t/cinder-asios-tcpcleint-strange-behavior/1896/2 "2021-12-11T19:48:28Z")

</div>

Here’s the final relevant bits of code (the commented line actually messes things up)

```auto
void BoomerangRecorderApp::onConnect( TcpSessionRef session )
{
	warningToConsole( "Connected" );
	mIsClientConnected = true;
	mSession = session;
	mSession->connectCloseEventHandler( [&]() {
		warningToConsole( "Disconnected" );
		mIsClientConnected = false;
	} );
	mSession->connectErrorEventHandler( &BoomerangRecorderApp::onError, this );
	mSession->connectReadCompleteEventHandler( [&]() {
		warningToConsole( "Read complete" );
	} );
	mSession->connectReadEventHandler( &BoomerangRecorderApp::onRead, this );
	mSession->connectWriteEventHandler( &BoomerangRecorderApp::onWrite, this );
	mSession->read();
	writeMessage( "FROM_CINDER_hello_" + toString( mPlayerIndex ) + "\n" );
}

void BoomerangRecorderApp::onRead( ci::BufferRef buffer )
{
	mSession->read();

	string response = TcpSession::bufferToString( buffer );
	mResponseChunk += response;
	warningToConsole( "On Read buffer - response chunk is : " + mResponseChunk + "---" );
	while( mResponseChunk.find('\n') != string::npos) {
		processMessage( mResponseChunk );
		if( mResponseChunk.length() > mResponseChunk.find( '\n' ) + 1 ) {
			warningToConsole( "More messages found!!" );
			mResponseChunk = mResponseChunk.substr( mResponseChunk.find( '\n' ) + 1 );
		}
		else {
			mResponseChunk = "";
		}
	}
}

void BoomerangRecorderApp::onWrite( size_t bytesTransferred )
{
	//mSession->read();
}

```

---

<div class="post-metadata">

**Author:** ![Kino](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/kino/32/465_2.png) [@Kino](https://discourse.libcinder.org/u/Kino)\
**Post date:** [December 11, 2021, 7:51pm UTC](https://discourse.libcinder.org/t/cinder-asios-tcpcleint-strange-behavior/1896/3 "2021-12-11T19:51:31Z")

</div>

I also had to add a `mSession->read()` to `onConnect` as you can see to initiate the listening but I don’t think that has anything to do with problem. It more looks like a conflict between the two read() calls on `onRead()`and `onWrite()`
