写在前面:前一篇文字已经把相关的东西叙述了,这里再把ClientSession这部分加上
一.数据的发送,如下:
int ClientSession::sendData(std::string message){
int rc = 1;
char buf[50];
int offset = 0;
offset += sprintf(buf, "%04d", message.size());
offset += sprintf(buf+offset, "%s", message.c_str());
if(writable && (rc = ::send(sockfd, buf, offset, MSG_DONTWAIT)) != offset
&& errno == EAGAIN){
printf("[HighconcurrentClient::sendData] send EAGAIN, modify writable status");
writable = false;
rc = 1;
}
return rc;
}
二.数据的接收,如下:
void ClientSession::recvData(){
int lenExpected = 0;
int lenRead = 0;
int rc = 0;
int nReadable = 0;
char buf[BUFFERSIZE] = {0};
if((lenRead = ::recv(sockfd, buf, 4, MSG_DONTWAIT)) != 4){
printf("[HighconcurrentClient::recvData] recv bytes is not equal to 4\n");
}
lenExpected = getBufLength(buf);//前4个字节定义了接收信息的长度
if((lenRead = ::recv(sockfd, buf, lenExpected, MSG_DONTWAIT)) < lenExpected){
printf("[HighconcurrentClient::recvData] recv bytes less than expected\n");
}
printf("[HighconcurrentClient::recvData] client->%d recv data is %s\n", sockfd, buf);
DownstreamHandler::handle(this, buf, lenExpected);
}
~未完待续
转载请注明出处:山水间博客,http://blog.csdn.net/linyanwen99/article/details/8315868