0

Well, I am writing a multithreaded FTP server and now I am trying to implement data transfer abortion. This is a part of my code managing data transfer abortion.

ABORCommand(); //sends message with code 226 (1st message)
pthread_join(threads, &ret);  //waits for the thread processing data transfer

sprintf(buffer, "226 Transfer complete - %d bytes copied.\r\n", data_comm->num_bytes);
Send(); //this just sends the message in buffer to the client (2nd message)
WriteToLog("transfer complete", "226");

close(data_comm->clie_sock);   //closes the data connection
delete data_comm;  //this deletes instance of the class taking care of data transfer
  1. My problem is that the two clients I am testing (Nautilus and gftp client) behave differently when aborting data transfer.
  2. What I am trying to achieve is to be able to abort data transfer process with no errors using different clients.
  3. The behaviour of the clients depends on the messages I send them.
  4. When I abort the transfer with Nautilus, it work fine, no errors, no leaks. But with gftp client, the client recieves the first message and then closes control connection. Then the server sends the second message to the client. After that, the server is supposed to recieve something from the client, but when it happens, I get an error and a ton of mem leaks. I know that reading from closed socket gives error but it shouldn't crash the whole program.
  5. I tried to make the abortion work for the gftp client and the only (partial) solution was to send only the second message (well, I think it doesn't matter which one I send the point is to send just one of them). I didn't get any errors after that and no mem leaks. The only problem left was that the client still closed the control connection. The server then closed the connection on it's end, because the client apparently disconnected (even the gftp client wrote "Disconnecting from site localhost"), but after the server closes it, gftp writes "Error: Remote site localhost disconnected. Will reconnect in 30 seconds".
  6. The solution however had a side effect. The Nautilus client stopped working. The client recieved the message ,but was still waiting for another one (or so I think), because I sent him just one message.
  7. My question is how is it possible to make it work for both clients? The only idea I have is that maybe the reply code is wrong. Please, tell me where the problem lies.

Thanks

  • 1
    From the school of "Making Code Maintainable through Comments": `WriteToLog(".."); // writes to log` – Kerrek SB Jun 10 '13 at 09:53
  • Your question is very hard to read. Can you rewrite it with several parts ? Roughly something like that : 1) the context 2) what you are trying to do (high level) 3) how you are trying to do it 4) what is the problem 5) what you tried so far to solve the problem. This way, you'll have far more people to answer you ! Thanks, I'll take a look later. – Offirmo Jun 10 '13 at 12:11
  • Isn't it the client which should send the ABOR command and not the answer ? Is the code you show the server's code or the client ? Or mixed ? – Offirmo Jun 10 '13 at 15:02
  • Thanks for your replies. It's server's code. I managed to get rid of the memory leaks so far but I still can't make the server compatible with both clients. Nautilus needs 2 answers to ABOR and gftp only one... – user2274361 Jun 10 '13 at 18:31

1 Answers1

0

Ok, first you are true :

I get an error and a ton of mem leaks. I know that reading from closed socket gives error but it shouldn't crash the whole program

So yes :

  • you should be resistant to errors : systematically checks for return codes and handle them correctly
  • you should not have memory leaks : use smart pointers so that unused memory blocks are freed

And also, in a general manner :

  • your server should never expect clients to behave correctly. On the contrary, it should assume that clients are buggy or even hostile (security). So you should have timeouts if clients don't answer.
  • if a client doesn't respect the specification but should still work (big market share) then you must find a way to recognize this client and behave accordingly.

After that, I don't understand your problem very well. So see my comments and I will update this answer.

Offirmo
  • 18,962
  • 12
  • 76
  • 97