简体   繁体   English

TCP IP通讯

[英]TCP IP Communication

I have made a client program using boost::asio in C++ which connects to a TCP/IP server. 我使用C ++中的boost :: asio制作了一个客户端程序,该程序连接到TCP / IP服务器。 When I send a command say "llist" it will return 当我发送命令说“ llist”时,它将返回

? "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. 问题是当我使用“ \\ n”检查是否不起作用时。

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

What can be the problem? 可能是什么问题?

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] ). 您没有给出发生问题的详细信息,但是您可能会猜测,您当前的阅读方式将删除有效字符(即, reply[1] )。

You can instead use boost::asio::read_until to read an entire line : 您可以改为使用boost::asio::read_until读取整行:

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

and treat an empty line as the end of the transmission. 并将空行视为传输的结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM