Preface: the previous article describes related things. Here we add the clientsession part.
1. Data Transmission:
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;
}
Ii. Data Reception:
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); // the first four bytes define the length of the received information
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 );
}
~ To be continued
Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/8315868