Thread: Write to a file from a socket?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    Write to a file from a socket?

    i am having a hard to trying to write a .exe such as calc.exe from a recv Buffer to the file thats created with CreateFile , here is my loop

    Code:
    while(recv(sock,Buffer,sizeof(Buffer),0) > 0)
    {
    			
       printf("%s\n","writing contents to file");
       WriteFile(f,Buffer,sizeof(Buffer),&written,0);
       Sleep(50);
    }
    does anyonr know the correct way?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    the third parameter or WriteFile should be the result of recv

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    Code:
     int amount;
    			while(1)
    			{
    				amount = recv(sock,Buffer,sizeof(Buffer),0);
    			
    				if(amount > 0)
    				{
    					printf("%s %i\n","Bytes Recieved", amount);
    			        WriteFile(f,Buffer,amount,&written,0);
    				}
    				else
    				{
    					break;
    				}
    			}
    still cant get it working
    Last edited by Anddos; 03-24-2009 at 08:43 AM.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I suggest you check all the returns if things go wrong.

    This will happen in every app you write, so learning to diagnose these types of issues is vital.



    Tell us what this returns (not complied, off the top of my head...)

    Code:
    int iRet=0,iErr=0;
    
    
    iRet=WriteFile(f,Buffer,amount,&written,0);
    //WriteFile returns 0 for success
    if(iRet)
    {
         //error occured in WriteFile()
         iErr=GetLastError();
         printf("WriteFile failed with %d and GLE returned %d.",iRet,iErr);
    }
    //but we should also check if the right amount was writen to file. 
    else if(amount!=written)
    {
        //WriteFile did not write the correct amount to the file
        iErr=GetLastError();
         printf("WriteFile wrote wrong amount %d of %d [GLE returned %d].",amount,written,iErr);
    }
    //else success
    EDIT:
    I also wonder what will happen if amount == sizeof(Buffer) as you will not have a null terminator.

    Probably nothing in a complier (and 99.99% of the time in release) but I would be inclined to use (sizeof(Buffer)-1) in recv()
    Last edited by novacain; 03-27-2009 at 11:50 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by novacain View Post
    I also wonder what will happen if amount == sizeof(Buffer) as you will not have a null terminator.

    Probably nothing in a complier (and 99.99% of the time in release) but I would be inclined to use (sizeof(Buffer)-1) in recv()
    Why do you need a nul-terminator in the binary buffer? It is not a string and it is not passed to any function expecting string. So why do you need it?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by vart View Post
    Why do you need a nul-terminator in the binary buffer? It is not a string and it is not passed to any function expecting string. So why do you need it?
    You are right but...

    I don't see a declaration, so we don't know it is a byte array.

    recv() lists the param as a char* so I assumed an inexperienced (with sockets) coder would use a char array.

    As I said '99.99%' and 'inclined'.

    I see no harm in being overly careful or teaching people to write robust code (but I write mission critical assest protection systems so I tend to be overly cautious).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Quote Originally Posted by Anddos View Post
    i am having a hard to trying to write a .exe such as calc.exe from a recv Buffer to the file thats created with CreateFile , here is my loop

    Code:
    while(recv(sock,Buffer,sizeof(Buffer),0) > 0)
    {
    			
       printf("%s\n","writing contents to file");
       WriteFile(f,Buffer,sizeof(Buffer),&written,0);
       Sleep(50);
    }
    does anyonr know the correct way?
    You really should post the code for both the client and server in order for anyone to provide a reasonable solution. Issues may be found on one or both sides.

    But anyway, I wrote a very basic file transfer util and my approach was to first transfer the file information (which includes the file size) to the destination host. I used the following structure:

    Code:
    typedef struct {
        CHAR  szName[MAX_PATH + 1];
        ULARGE_INTEGER ulFileSize;
      
    } REQUIREDFILEINFO;
    My file data receive is as follows:

    Code:
    REQUIREDFILEINFO  fReqFileInfo;
    
    while(fReqFileInfo.ulFileSize.QuadPart > 0 && ((iLen = recv(iClient, szBuffer, 4096, 0)) > 0))
    {
            if(!WriteFile(handle, szBuffer, iLen, &dwWrite, NULL)) break;
            fReqFileInfo.ulFileSize.QuadPart -= iLen;
    }
    After the file information is transferred to the destination, the file data is then transferred in 4K chunks.

    As Alex31 would say, this issue has been discussed (and solved) many times on the Win32 newsgroups.
    Last edited by BobS0327; 03-29-2009 at 06:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM