One error is that
int bytesRead = CFReadStreamRead(readStream, bufr,strlen((char*)bufr));
should be
int bytesRead = CFReadStreamRead(readStream, bufr, sizeof(bufr));
The last parameter of CFReadStreamRead
is the capacity of the read buffer and determines the maximum number of bytes read. strlen((char*)bufr)
is the length of the string currently in the buffer. You should also NULL-terminat the string in bufr
before printing it.
With this modification, your program might work with short strings. But there will be problems as soon as you try to send/receive larger amounts of data.
A socket write can write less bytes than you asked it to, and a socket read can return less bytes than you requested.
Have a look at the Stream Programming Guide which describes how to register the socket streams with the runloop and handle stream events asynchronously.