Linuxc/C ++ programming BASICS (26) heartbeat Information Processing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.