客戶端:轉換爲整數字符緩衝區在插座
char buf[1024];
int packetAmt = fileSize/PACKETSIZE;
cout<<"Number of packet amounts: "<<packetAmt<<endl;
// send packet amount to serv
ibytessent=send(s,itoa(packetAmt,buf,10),sizeof(buf),0);
服務器端:
char szbuffer[1024];
if((ibytesrecv = recv(s1,szbuffer,sizeof(szbuffer),0)) == SOCKET_ERROR)
throw "Receive error in server program\n";
cout << "This is the number of packets sent: " << szbuffer << endl;
packetAmt=atoi(szbuffer);
cout << "This is the number of packets sent: " << packetAmt<< endl;
的整數字符數組轉換成整數的發送的字符串不匹配後返回。但是,打印字符串將返回正確的值。我也嘗試過使用strtol函數。將整數轉換爲char數組時會發生錯誤嗎?
你得到的數字與你的期望值有多接近? –
我期待288,但我得到了120。 – Sebastien
這真的很奇怪,特別是因爲你同時顯示字符串和轉換後的值;這意味着「atoi」完全被打破,我拒絕相信。必須比你展示的代碼更多。 –