我正在為我和我的伙伴創建一個個人遠程檔案資源管理器。在大多數情況下,它按預期作業,它列舉給定目錄并將其發送回服務器查看。問題是如果緩沖區接收到超過 46 個位元組,它會將剩余的緩沖區覆寫為奇怪的 ASCII 字串。
客戶端發送的內容 - C:\Users\Test\NTUSER.DAT{53b39e88-18c4-11ea-a811-000d3aa4692b}.TM.blf 大小:69
服務器看到的內容 - C:\Users\Test\NTUSER.DAT{53b39e88-18c4-1■?_y·? 大小:46
兩個程式的默認緩沖區大小都是 512 位元組
編輯:在您詢問 FileManager 是個人代碼之前,它對客戶端所做的唯一事情就是收集字串中的路徑以供客戶端發送到服務器。
客戶端發送代碼
//Gets Path From Server(recvbuf&recvbuflen buffer size = 512)
std::string fldr = GetReply(ConnectSocket, recvbuf, recvbuflen);
std::cout << "Reply: " << fldr << "\n";
//Gets Directory Struct using given path from server
FileManager::Directory* dir = f.GetDir(fldr);
//Sends initial size to iterate through on server side
send(ConnectSocket, std::to_string(dir->TotalList.size()).c_str(), dir->TotalList.size(), 0);
//dir->TotalList holds a vector of strings that contain paths in directory
for (std::string rV : dir->TotalList) {
//Sleep for 75ms, without it the data would be sent to fast
Sleep(75);
//Sends the elements in TotalList
//had to use sizeof(rV) because (int)strlen would overwrite the buffer fsr
send(ConnectSocket, rV.c_str(), sizeof(rV), 0);
std::cout << "Sent: " << rV << " Size: " << (int)strlen(rV.c_str()) << "\n";
}
服務器接收代碼
while (1) {
//Send Command, memset(sndbuf, 0, sizeof(sndbuf) is called before loop
std::cout << "\n\nEnter Folder: "; std::cin.getline(sndbuf, sizeof(sndbuf));
//DirList is a string vector holding all the previous paths that clients sent
//CheckIfExists iterates through the vector and checks if sndbuf is a valid path
//if false it restarts loop
if (CheckIfExists(sndbuf, DirList)) {
//if true sends path, again if I used (int)strlen(sndbuf)
//instead of sizeof(sndbuf) it would cause it to overwrite
//the buffer on client side, again idky
send(cSock, sndbuf, sizeof(sndbuf), 0);
//zeros out the buffer for the replys from client
memset(recvbuf, 0, sizeof(recvbuf));
//Display reply loops recv and clears then adds new elements to vector
DisplayReply(cSock, recvbuf, recvbuflen, &DirList);
break;
}
else
std::cout << "Folder Doesn't Exist...";
}
服務器顯示回復代碼
void DisplayReply(SOCKET cSock, char* recvbuf, int recvbuflen, std::vector<std::string>* list) {
//Gets the # of paths client has
int r = std::stoi(GetReply(cSock, recvbuf, recvbuflen));
std::cout << "Size: " << r << "\n";
//Again zeroing the buffer, then clearing the vector
memset(recvbuf, 0, sizeof(recvbuf)); list->clear();
for (int i = 0; i < r; i ) {
//Sleep for 75ms so all data sends properly
Sleep(75);
//Recieve replys from client, GetReply returns string
//Using any other variable doesnt work either, all caps out at 46 bytes
//ex const char* or char*
std::string rV = GetReply(cSock, recvbuf, recvbuflen);
std::cout << rV << " Size: " << (int)strlen(rV.c_str()) << "\n";
//Add path to vector using vector pointer
list->push_back(recvbuf);
}
}
服務器獲取回復碼
std::string GetReply(SOCKET cSock, char* recvbuf, int recvbuflen) {
int iResult = 0;
do {
iResult = recv(cSock, recvbuf, recvbuflen, 0);
if (iResult > 0) {
return (std::string)recvbuf;
}
else {
printf("recv failed with error: %d\n", WSAGetLastError());
return "";
}
} while (iResult > 0);
}
uj5u.com熱心網友回復:
您很可能將沒有的緩沖區歸零。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/520781.html
上一篇:與UDP中的不同埠通信
