Large File transfer error in Socket in C -
i m building code transferring file server.c client.c program . during run time not working . m sending file in chunks of 512 bytes . source file 784 kb has 88000 names in it.
this program works correctly on 127.0.0.1 not working when connect 2 computers on lan/wifi .
i m not including complete file function m going transfer/recieve file
help me find error in code.
if u test between 2 computers .
if u guys have better code file sending , recieving please post ,see need control out of function after transfer done . appreciate .
i m using linux following purpose & gcc compiltion
server.c function
void filesender(char fs_name[],int ncfd) { char sdbuf[length]; file *fs=fopen(fs_name,"r"); if(fs==null) { perror("\n[server] file not found on server directory"); exit(1); } bzero(sdbuf, length); int fs_block_sz; while((fs_block_sz = fread(sdbuf, sizeof(char), length, fs))>0) { if(send(ncfd, sdbuf, fs_block_sz, 0) < 0) { puts("\n[server] error: sending file failed"); exit(1); } bzero(sdbuf, length); } puts("\n[server] file sended successfull"); }
client.c
void filereciever(int id,int ncfd) { char revbuf[length]; //buffer variable hold recieved data char *fr_name; char fid=id; asprintf(&fr_name,"result%c.txt",fid); file *fr = fopen(fr_name, "a"); if(fr == null) { printf("[server] file %s cannot opened.\n", fr_name); } else { bzero(revbuf, length); int fr_block_sz = 0; while((fr_block_sz = recv(ncfd, revbuf, length, 0)) > 0) { int write_sz = fwrite(revbuf, sizeof(char), fr_block_sz, fr); if(write_sz < fr_block_sz) { puts("\n[server] file write failed"); } bzero(revbuf, length); if (fr_block_sz == 0 || fr_block_sz != 512) { break; } } if(fr_block_sz < 0) { if (errno == eagain) { puts("\n[server] error: recv() timed out"); } else { printf("\n[server] error: recv() failed due errno = %d", errno); } } printf("[server] file received server!\n"); fclose(fr); }}
you should check result of send()
for tcp socket, send()
put data os's internal queue, may sometime fill part of requested size. supposed handle such incomplete send , retry later.
edit: op disclosed more information in comments, seems problem not (yet) related network, seems there bug choosing file send, , need post relevant code further diagnose.
Comments
Post a Comment