Basically I am writing a simple program using the boost socket library… I have two programs a client and a server. the server waits for a connection from the client and when it finds one the client sends the server a message and the server prints out, this works the first time the client queries the server but after a while an strange pattern begins lets say our server was running and I used the client program two times by executing:
./client localhost name message
./client localhost name test
the output 0f the server would first be:
name: message
however next it would display
name: testage
I don’t know why this is happening but I know it must be the server, because the the clients each send a packet independently the server just prints it out… I’m thinking that this has something to do with the socket buffer not being flushed or something of that nature…
anyway heres the sourcecode:
client.cpp
http://pastebin.com/hWpLNqnW
server.cpp
http://pastebin.com/Q4esYwdc
The
read_somecall in the server returns the number of bytes read. You should use that value and use it to null terminate the buffer. Something along these lines:In the first message, the buffer may have been initialized with zeros. The next time, though, it would contain the same contents as the previous iteration. Note that the
strcpy(buf,"");call only ends up setting the first byte ofbufto zero.