Csocket OnReceive接收数据部分(解决接收数据不全的问题+获取时间+将数据写入文本文档)
生活随笔
收集整理的這篇文章主要介紹了
Csocket OnReceive接收数据部分(解决接收数据不全的问题+获取时间+将数据写入文本文档)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
void CDebugDlg::OnReceive()
{BYTE cbDataBuffer[1024];BYTE buff;int len,a,leng;CString b,c,d;CString str_HEX;leng = m_ClientSocket->Receive(&cbDataBuffer, 1024);//接收數據的字節數for (a = 0; a <leng; a++)//此循環解決了接收信息不全的問題{ len = m_ClientSocket->Receive(&buff, sizeof(buff));b.Format("%c", buff);str_HEX = Ascii2Hex(CString(b));c += CString(str_HEX);} //獲取時間CString msg;SYSTEMTIME t;GetLocalTime(&t);msg.Format(_T("[%d-%d-%d-%d:%d:%d] "), t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond);m_output += CString(msg) + "\r\n";CString str;//ipCString strportValue;unsigned int port;//端口m_ClientSocket->GetPeerName(str, port);//獲取IPstrportValue.Format("%u", port);//數據類型轉換m_output += CString(str) + ":" + CString(strportValue) + ":" + CString(c) + "\r\n";UpdateData(FALSE);//將接收的數據寫入文本文檔ofstream outfile;outfile.open("d://sensor.txt", ios::binary | ios::app | ios::in | ios::out);outfile << msg << str << ":" << strportValue << " ";int i;int length = c.GetLength();for (i = 0; i < length; i++){outfile << c[i] << " ";}outfile << endl;outfile.close();
}
接收數據部分仍然存在一些小問題尚未解決…
總結
以上是生活随笔為你收集整理的Csocket OnReceive接收数据部分(解决接收数据不全的问题+获取时间+将数据写入文本文档)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 平台下基于Qt 的电子地图的
- 下一篇: mplayer参数说明