Find the answer to your Linux question:
Results 1 to 2 of 2
Hello! I have a very disturbing issue. When I download a file from a FTP server, and read this information from a socket descriptor I put char by char in ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    10

    Reading a file byte after byte from a socket corrupts it

    Hello!
    I have a very disturbing issue. When I download a file from a FTP server, and read this information from a socket descriptor I put char by char in a file with the same name on localhost. Unfortuanetly, the local file has its size increased for a reason I can't seem to figure it out.
    So here is what I do when I put the bytes in my local file:
    Code:
            do
    	{
    		i = 0;
    		bzero(buffer_data, 512);
    		n = read(sockdatad, buffer_data, 512);
    		if (n < 0) 
    			error("ERROR reading from socket");
    		if(n > 0)
    			while(i < n)
    			{
    				fputc(buffer_data[i], f);
    				i++;					
    			}
    	}	
    	while(n);
    So I basically append each chunk of 512 bytes in my file until my socket runs out of bytes. This is pretty straightforward.
    When I look at a text file it seems to have one more \n at the end of the file.
    When I try to open a binary file, it's all messed up because of this "tiny" problem.

    I don't know if I made myself clear, but ask me whatever you find uninteligible.

    Any help will be highly appreciated!

    Thank you in advance!

  2. #2
    Linux User
    Join Date
    Mar 2008
    Posts
    287
    In ur do, if u get there 1 more time than data elements available, the bzero sets the buffer to \0 (EOString). read recognizes it says result is 0 neither <0 or >0. Conseequently neither "if" is considered. Do u possibly write within the following while?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...