Skip to content

Instantly share code, notes, and snippets.

@vik-y
Last active August 29, 2015 14:17
Show Gist options
  • Save vik-y/ef305b179cbfc08e0c99 to your computer and use it in GitHub Desktop.
Save vik-y/ef305b179cbfc08e0c99 to your computer and use it in GitHub Desktop.
Send a file - c socket
//Works perfectly with the send written above
char recvBuff[10];
int bytesReceived = recv(new_fd, recvBuff, 10, 0);
while(bytesReceived != 0)
{
// you should add error checking here
fwrite(recvBuff, bytesReceived, 1, outputFile);
bytesReceived = recv(new_fd, recvBuff, 10, 0);
}
FILE *inputFile = fopen("testfile.txt", "r");
char sendBuffer[20];
// TODO: Check for errors here
int bytesRead = fread(sendBuffer, 1, 10, inputFile); //Reading 10 byte at a time
//10 values of 1 byte each
while(!feof(inputFile))
{
//TODO: check for errors here
send(sockfd, sendBuffer, bytesRead, 0);
printf("%s %d ", sendBuffer, bytesRead);
bytesRead = fread(sendBuffer, 1, 10, inputFile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment