Thread: [C++] Sending variable amount of data over Winsock

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    5

    [C++] Sending variable amount of data over Winsock

    Hello

    Client:
    Code:
    ....
    string cmd = "dir c:\filequeue > ";
    string outputFilePath;
    outputFilePath.append(getTempPath());
    outputFilePath.append("\\test.txt");
    cmd.append(outputFilePath);
    system(cmd.c_str()); // dir c:\filequeue > %temp%\test.txt
    
    string content = get_file_contents(outputFilePath.c_str());
       
    send(s, content.c_str(), content.length(), 0);
    I'm executing the "dir" command to get a listing of Folders/files of one Folder. Then I read the Output of the file and send it over winsock to the Server.

    Now, the Problem is, I don't know how I can handle the recv properly, cause I have to set the buffer size without knowing, how much data is actually transfered. Sometimes maybe no files are in c:\filequeue, sometimes a 100k.


    So I tried to make recv as a Loop:

    Server:
    Code:
    ...
    while (rc != SOCKET_ERROR)
    {
         printf("\n#");
         gets(buf); //please no discussion about gets, I will Change this later ;)
         if (strcmp(buf, "ls") == 0){
              send(connectedSocket, "LIST", 4, 0);
              do{
                   rc = recv(connectedSocket, recvBuf, 255, 0);
                   recvBuf[rc] = '\0';
                 cout << recvBuf;
              } while (rc > 0);
          }     
    }
    now it works, but as the recv blocks, it will never leave the Loop, even when the Transfer is finished.

    What should I do?

    I believe I could make unblocking sockets, but that's a bit complicated. Isn't there an easier solution, with malloc'ing the buffer or a Signal when to leave the recv Loop?

    I am very new to C/C++, so if possible, explain it dummy-proof
    Last edited by netik; 10-31-2013 at 05:04 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    [Note: this thread is misplaced, as it is really a windows programming problem, not a C++ problem].


    Communicate between client and server using some protocol. One way is, before sending your data, to send information about the length of the data to follow. Another way is to send the data and then send some marker (which will not be in the data). There are many alternatives.

    It is still necessary to detect error conditions (such as a socket closed at the other end). With care, you can even make use of such things.

    Unrelated to your problem, but you would probably be better off using windows API calls to get information about files in a directory. That way, you can be selective about the information you send, not have to parse the directory listing as such, and you will be less vulnerable to configuration changes (for example, the user employing a different command line processor).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    5
    Hi Grumpy, thank you for your answer, and sorry for th emisplaced thread.

    You are right, I wanted to do it with Windows API later, but right now, I can live with the Standard dir Output of Windows.

    So, you would kind of send the size of the data that is going to be sent to the server first and then allocate a variable with that size? So, something like:

    char sizeBuffer[50]
    recv incoming data size to sizeBuffer)
    malloc new buffer
    recv data to new dataBuffer

    Does that work?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That's one way. It would be unusual that the size information would require 50 bytes, but that will do.

    As I said, there are many options.

    Just remember that the code reading from the socket will need to copy with untoward things (such as data it receives not being in the form it expects).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sending a structure variable through MPI in C
    By shrek in forum C Programming
    Replies: 8
    Last Post: 07-01-2011, 09:57 AM
  2. Global variable vs parameter 'sending'.
    By MipZhaP in forum C++ Programming
    Replies: 16
    Last Post: 04-03-2011, 09:58 AM
  3. Copying constant amount of data
    By TriKri in forum C++ Programming
    Replies: 16
    Last Post: 07-12-2008, 06:32 AM
  4. storing an unknown amount of variable
    By sreetvert83 in forum Game Programming
    Replies: 5
    Last Post: 09-17-2005, 05:42 PM
  5. winsock, sending c++ style strings
    By b00l34n in forum Networking/Device Communication
    Replies: 17
    Last Post: 05-06-2004, 07:41 PM