I tried your way by putting it in a buffer.
Code:
// create and init a char buffer
char *buffer = malloc(BUFFSIZE);
buffer[0] = '\0';
int stop_received = 0;
int nr_bytes_recv = 0;
while (! stop_received) {
// receive data in buffer
nr_bytes_recv = recv(s, buffer, BUFFSIZE, 0);
if (nr_bytes_recv > BUFFSIZE) {
perror("Buffer too small");
exit(EXIT_FAILURE);
}
buffer[nr_bytes_recv]= '\0'; // close string
printf("Server received : %s", buffer);
if(strncmp(buffer, "upload", 6) == 0){
// echo message
rv = send(s, buffer, nr_bytes_recv,0);
if (rv < 0) {
perror("Error sending");
exit(EXIT_FAILURE);
}
do {
// receive data in buffer
nr_bytes_recv = recv(s, buffer, (BUFFSIZE-current), 0);
if(nr_bytes_recv > 0)
current += nr_bytes_recv;
} while (nr_bytes_recv > 0);
FILE *fp = fopen(buffer, "w" );
fwrite(buffer, sizeof(buffer), current, fp);
fclose(fp);
close(s);
stop_received = 1;
The results: it makes a file "upload" with the name of string commando which clients sends. As you can see in the code i use prints function to print out the buffer, this is the output of the buffer
Server received : upload
test.txt
I think the problem is the client sends 3 items in a buffer, the string "upload", filedata and filename. The fopen function takes the first item from the buffer instead of filename.