So, I wrote a small server program where when I run it I specify a file in the command line arguments... then when a client connects to that server the server starts sending the file to the client.
Once the file is received by the client the connection is terminated and both programs close down. It works perfectly. There is just one thing I got to work by mainly fiddling around. I needed a way to know when the file was done transmitting so I could stop receiving packets. The way I did this is:
Code:
while(1){
drec = recv(sockfd, buf, PSIZE, 0);
if(drec <= 0){
break;
}
fwrite(buf, 1, drec, f);
}
I'm pretty sure recv() is a blocking function unless otherwise specified. If that's the case, then why is it that recv() returns 0 if none is received? I thought it waits there until it does receive something... meaning drec couldn't = 0 and exit the loop... however it does get 0 then exits the loop.
If it helps the code which I'm using on my server side to send the file is:
Code:
while(!feof(f)){
dread = fread(buf,sizeof(char),PSIZE,f);
dsent = send(newfd, buf, dread, 0);
}
could it be that drec is really -1 and exiting the loop that way... since the server cut the connection... i really don't know...