Computing.Net > Forums > Programming > Sending files using C Sockets HELP!

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Sending files using C Sockets HELP!

Reply to Message Icon

Name: ruthless
Date: October 16, 2004 at 00:41:32 Pacific
OS: Linux
CPU/Ram: AMD, 512
Comment:

Hey all, i've been tearing my hair out for about a week trying to get this working:

What i have is a server/client program that simply sends a file from the server to the client across a TCP socket.

I've set up the connection successfully and can send commands across from client to server for the server to parse a give back a reply.. no worries there. But the problem is trying to send a file across.

On the server side i've used:
returnVal = sendfile (sockfd, file, &offset, stat_buf.st_size);

This works every time, and sends the entire file across, binary or text, both are sent.

On the client side this is my code to recieve the file:

for( ; ; )
{
i = read(sockfd,(void *)buffer,size);
sizeSoFar += i;
if (i < 1 && errno != EINPROGRESS && errno != EWOULDBLOCK)
{
printf("Error executing read, errno = %d, i = %d\n", errno, i);
break;
}

else if(maxsize < size)
{
fwrite(buffer,i,1,ff);
continue;
}
else
{
printf("The file has been copied\n\n");
printf("Number of bytes transfered: %d\n\n",size);
fwrite(buffer,i,1,ff);
break;
}

}

This works fine SOMETIMES. Today I logged in, and was able to send any file across successfully, but all i did was kill the server/client and re-run it, and the same file that i just sent across wont recieve anymore.

After some messing around with strace, i found that the sendfile function works every time, and all bytes are sent across. On some rare occasions, and with some files but not others, the read function only returns about half the file, then loops around again to call read again, but is blocked because there is nothing to read in the socket.

What happened to the rest of the data?

After messing around a lot, i found that the way i declare buffer above makes a big difference, but i've tried every which way but nothing seems to make the file send across every time.

On top of this, when i get a file, i have to kill the client and run it again before i can send another file successfully.

Sorry for the lengthy message but if anyone could help me that would be great coz i've been trying to fix this for days with no luck..

Thanks in advance :)



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Sending files using C Sockets HELP!

create dll file using c files www.computing.net/answers/programming/create-dll-file-using-c-files/19380.html

CGI Text file on hd using c/c++/per www.computing.net/answers/programming/cgi-text-file-on-hd-using-ccper/11020.html

c programming help needed www.computing.net/answers/programming/c-programming-help-needed/10441.html