Jishnu U Nair
Jishnu U Nair

Reputation: 520

TCP IP Communication

I have made a client program using boost::asio in C++ which connects to a TCP/IP server. When I send a command say "llist" it will return

? "llist"
l1 32x32 Video "Video L1"
l2 512x512 Video "Audio L2"

Each sentence end by a new line character and at end of the transmission it will have an extra new line character. So my listening function reads two characters from the socket

 len=_socket->read_some(boost::asio::buffer(reply,sizeof(reply)),error);// char reply[2];

The problem is when I check for "\n" using if it doesn't work.

  if(reply[0]!='\n')
    if(reply[1]!='\n')
       str.insert(i,1,reply[0]); //std::string str;

What can be the problem?

Upvotes: 0

Views: 264

Answers (1)

Sander De Dycker
Sander De Dycker

Reputation: 16243

You haven't given much details of what's going wrong, but at a guess, your current way of reading will drop valid characters (ie. what's in reply[1]).

You can instead use boost::asio::read_until to read an entire line :

boost::asio::read_until(socket, buffer, '\n');

and treat an empty line as the end of the transmission.

Upvotes: 3

Related Questions