Write to a file from a socket?

This is a discussion on Write to a file from a socket? within the Windows Programming forums, part of the Platform Specific Boards category; i am having a hard to trying to write a .exe such as calc.exe from a recv Buffer to the ...

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

    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
    303
    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,853
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    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,059
    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, 01:46 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21