Hello, I have a program that is like a ftp server - it sends and receives files over unix sockets.

It works fine when I transfer text files, but as soon as I want to receive binary files, it's not working properly.

Files are transferred correctly, I can verify that with diff.

The problem is-- after the file is sent then reading the next command (like put, get or list) from user gives some garbage and then the user input

But after the server reads the garbage line, the next command is executed correctly again.

The server code http://pastebin.com/AazSQU1L
client code http://pastebin.com/gYNegySh

you can use nc (netcat) instead of the client when uncommenting line 85 from the server
usage g [file name] or l for directory listing

the important part of the transfer function might be this:

while(1){
    if(feof(fp)){
        printf("done sending file, last slice length: %d\n", readcount);
        break;
    }

    readcount = fread(string, 1, BUFLEN, fp);
    out(client_fd, string, readcount);

}

Hope you can give me some advice with that

What is this

out(client_fd, string, readcount);

Ooops, found your posted code..

You could help us out with a few remarks in your code...Also I noticed many malloc's

char* string = malloc(BUFLEN);

But no free's.

Now i fixed the mallocs without free, but the problem persists

the new server code http://pastebin.com/niJmPgWB

it shouldn't be because i'm sending raw data since the recieved file is ok

The problem is that printing binary data messes up the console.

The problem is that printing binary data messes up the console.

Well duh...Try printing the data as hex values using the format specifier %x.

Foobar - are you still having a problem? My problem is similar except I am using threads instead of forking a child - although that most certainly isnt contributing to not being able to transfer binary data...
my guess is that the socket is on blocking mode and since hte EOF is not being read correctly for binary files, its waiting for input and therefore the next command is foobar...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.