網路編程:流式通訊端應用資料完整包讀取問題

來源:互聯網
上載者:User

本文接著上篇分包收包問題隸屬同一個問題。今天剛遇到的問題並解決了,TCP協議傳輸,流式通訊端編程,收取網路資料時,從緩衝資料中讀取出應用程式層完整包,原來沒有單個固定頭長度的資料包取出bug表現,現在後來加入一個只有頭的報文進入,於是整個取包就出現混亂。完整性再一次無法癒合。現在重新整理取包流程。msglen = recv(s, buffer, sizeof(buffer), 0); buf4packet 為儲存完整資料包的臨時資料緩衝區

while(msglen>0){{int len,szcpy;len = (offset < sizeof(COMM_HEAD) ? sizeof(COMM_HEAD): ((COMM_HEAD*)buf4packet)->msg_len);szcpy = len - offset;szcpy = (msglen > szcpy ? szcpy:msglen);memcpy(buf4packet + offset, buffer2, szcpy);offset += szcpy;buffer2 += szcpy;msglen -= szcpy;if (offset < sizeof(COMM_HEAD)) continue;len = ((COMM_HEAD*)buf4packet)->msg_len;if (offset <len) continue;if (offset !=len){xlog_out(LOG_COM,P_FATAL,"message_recv occur an error on packet completeness analyse!\n");close(s);break;}}offset = 0;COMM_HEAD* msg_p = NULL;msg_p = message_parse(buf4packet);if (msg_p != NULL){……}……}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.