c++ win recv读取全部读取不全笔记
//发送HTTP请求并且接受响应
string QuickHttp::httpDataTransmit(int isSocFd) {
//接受服务器返回的信息
string info;//接受的信息
char ch;//每次接受的信息
int rlength = 0;//接受数据的总大小
int rlen = recv(isSocFd, &ch, 1, 0);//每次接受的数据大小
rlength += rlen;
while (rlen != 0 && rlen != SOCKET_ERROR) {
info += ch;
rlen = recv(isSocFd, &ch, 1, 0);//每次接受的数据大小
rlength += rlen;
}
//编码转换 防止在控制台显示乱码
char *pszBuffer = new char[info.length() + 1];
wchar_t *pszWideBuffer = new wchar_t[(info.length() + 1) * 2];
memset(pszWideBuffer, 0, (info.length() + 1) * 2);
memset(pszBuffer, 0, info.length() + 1);
MultiByteToWideChar(CP_UTF8, 0, info.c_str(), info.length(), pszWideBuffer,
(info.length() + 1) * 2);//将unicode编码,转换为宽字节
WideCharToMultiByte(CP_ACP, 0, pszWideBuffer, wcslen(pszWideBuffer), pszBuffer, info.length() + 1, NULL,
NULL);//将宽字节,转换为控制台编码
info = pszBuffer;
delete[] pszBuffer;
delete[] pszWideBuffer;
return info;
}