extra characters getting transferred while doing file transfer using sockets

Sep 18, 2012 at 5:48am
I am transferring a file from client to server using C++ socket programming on linux. I have successfully transferred the file. The only problem is some extra characters are also getting transferred at the end of the file. How can I deal with this..??

Plz reply now if possible..thanks in advance
Regards,
Aditi

This is file new.txt. You can see in the end some garbage values are getting printed. This is file received at server side :(

//file new.txt
#include"stdio.h"
int main()
{
int wc = 0;
FILE *fp;
int c;
fp = fopen("mee.txt","r");
while(1)
{
c = fgetc(fp);
if(c == EOF)
break;
else
{
if( c == ' ')
wc = wc + 1;
}
}
printf("Number of words are %d\n",wc);
fclose(fp);
return 0;
}
q
Last edited on Sep 18, 2012 at 6:02am
Sep 18, 2012 at 6:35am
problem resolved. I had used fseek function as follows.
fseek(f,0,SEEK_END);
to calculate size of the file f is pointing to at client side as:
lsize = ftell(f);
After which i was initializing my buffer as :
filebuffer = (char *)malloc(sizeof(char) * (lsize));
which is incorrect bcoz lsize-1 is actual size of file since SEEK_END points to end of file
Instead,
filebuffer = (char *)malloc(sizeof(char) * (lsize-1));
is right

:)

Regards,
Aditi

Sep 18, 2012 at 6:50am
Instead,
filebuffer = (char *)malloc(sizeof(char) * (lsize-1));
is right
Nope, that's not right. Allocating less memory than required cannot be the solution.

You didn't show the code that actually transfers the data.
Sep 18, 2012 at 7:56am
FILE *f;
long lsize;
size_t result;
f = fopen("new.txt","rb+");
if(f == NULL)
{
fputs("File error",stderr);
exit(1);
}
fseek(f,0,SEEK_END);
lsize = ftell(f);
rewind(f);
filebuffer = (char *)malloc(sizeof(char) * (lsize-1));
if(filebuffer == NULL)
{
fputs("Memory error",stderr);
exit(2);
}
result = fread(filebuffer,sizeof(char),lsize-1,f);
if(result != lsize-1)
{
fputs("Reading error",stderr);
exit(3);
}
fclose(f);
newClient.SendData(filebuffer, strlen(filebuffer), 0);

This is client side code.. If i pass lsize as argument to fread here then some extra special characters are printed at server side. SendData is a function which uses send() inside.
Sep 18, 2012 at 9:24am
The error is is what i thought:

newClient.SendData(filebuffer, strlen(filebuffer), 0);

fread() doesn't append a '\0' at the end of the read data, but strlen() needs the final 0.
Replace strlen(filebuffer) with lsize and you're done. And remove the -1


Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
Sep 21, 2012 at 9:39am
thanks....now got my concepts right....
so what u said has to be along with
filebuffer[lsize] = '\0'; .......after fread()
:)
Regards,
Aditi
Sep 21, 2012 at 10:29am
so what u said has to be along with
filebuffer[lsize] = '\0'; .......after fread()
No, that would cause a crash!

if you want to you have to write:
filebuffer = (char *)malloc(sizeof(char) * (lsize+1)); // Note +1 for the extra 0


What I tried to state was that you should change SendData to:
newClient.SendData(filebuffer, lsize, 0);
Sep 24, 2012 at 4:13am
ohk..
Topic archived. No new replies allowed.