Home > Software engineering >  Sockets Only Receives 46 Bytes of Data
Sockets Only Receives 46 Bytes of Data

Time:10-21

I am creating a personal remote file explorer for me and my buddy to mess around with. For the most part it works as intended, it enumerates through the given directory and sends it back for the server to see. The problem is that if the buffer receives more than 46 bytes, it will overwrite the remaining buffers into weird ASCII strings.

What Client Sends - C:\Users\Test\NTUSER.DAT{53b39e88-18c4-11ea-a811-000d3aa4692b}.TM.blf Size: 69

What Server Sees - C:\Users\Test\NTUSER.DAT{53b39e88-18c4-1■◄_y·⌂ Size: 46

The Default Buffer Size for both programs are 512 bytes

EDIT: Before you ask FileManager is personal code, only thing it does for client is gather paths in string for client to send to server.

Client Send Code

        //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";
        }

Server Recv Code

        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...";
        }

Server DisplayReply Code

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);
    }
}

Server GetReply Code

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);
}

CodePudding user response:

You most likely zeroed the buffer where you didnt have too.

  • Related