Here is the majority of the reading and sending from the client side. Again, the server only gets four letters of the destination file.
Code:
//CLIENT
// Get the file size
fseek (fpRead , 0 , SEEK_END);
fileSize = ftell(fpRead);
rewind (fpRead);
// Allocate memory to contain the whole file.
buffer = (char*)malloc(fileSize);
// Read the file into the buffer.
fread(buffer,1,fileSize,fpRead);
//Write to socket.
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
Server code:
Code:
//SERVER
//Parse fileSize received from socket
fileSize = atoi(command);
//Allocate space for incoming file
buffer = (char*) malloc (fileSize);
//Read in file from socket
n = read(newsockfd,buffer,sizeof(buffer));
if (n < 0)
error("ERROR reading from socket"
//Write buffer to file
fpWrite=fopen("recieved.txt", "ab+");
fwrite(buffer,1,fileSize,fpWrite);
fflush(fpWrite);
My file is always corrupt or truncated. Any ideas?
EDITED: Oh, I forgot to take into account '\0'. I added 1 to my mallocs, and it worked flawlessly. Thanks everyone!