Buffer Buffer in boost :: asio

Basically, I write a simple program using the boost socket library ... I have two programs - a client and a server. the server expects a connection from the client, and when it finds it, the client sends a message to the server, and the server prints it, this works when the client first requests it, but after a while a strange template starts saying that our server was there and I used the client program twice, by doing:

./client localhost name message
./client localhost name test

0f output will be the server first:
name: message
but then it will be displayed
name: testage

I don’t know why this is happening, but I know that it should be a server, because each client sends a packet regardless of whether the server just prints it ... I think this has something to do with the socket buffer is not reset or something like that...

anyway heres source code: client.cpp
http://pastebin.com/hWpLNqnW

server.cpp
http://pastebin.com/Q4esYwdc

+4
source share
1 answer

The read_some call on the server returns the number of bytes read. You must use this value and use it to zero buffer completion. Something like that:

 int len = connection.read_some(boost::asio::buffer(buf), error); buf[len] = '\0'; 

In the first message, the buffer may have been initialized with zeros. The next time, however, it will contain the same contents as the previous iteration. Note that calling strcpy(buf,""); ends with the first byte buf being zero.

+6
source

Source: https://habr.com/ru/post/1381993/


All Articles