I’m making an ftp server, and when i’m trying to connect to it with filezilla, the server is not accepting the connection on the passive socket. It hangs at the accept call.
Here’s a part of my code :
if ((server->pasv_sock = accept(server->sockt, (struct sockaddr*)&sin_clt,
(socklen_t*)&size_sin) == -1))
My socket is bind to a specific port and the client tries to connect with this one. Telnet is not connecting either.
If you can help me find what’s wrong, thank you 🙂
Did you remember to call
listenbeforeaccept?Remember:
socket->bind->listen->accept.Edit: Here’s some commentary on your code.
Using
getprotobynameis unnecessary, since the name is hard-coded and there’s only one IP stream protocol anyway. Use0instead ofpe->p_proto, and don’t bother callinggetprotobyname.You don’t initialize
sin.sin_port. That’s an error.This loop is a bit of a mess. It might be mostly correct, but it’s hard to tell. Let’s rewrite it so it’s obviously correct rather than not obviously incorrect.
This definitely doesn’t do what you want.
Discussion: I’ll add parentheses to the last line to show you what it actually does.
is the same as
I’m guessing that you got a compiler warning that complained about the assignment, which suggested adding parentheses… but the parentheses you added were in the wrong place. The reason the compiler was warning you was because this is a common source of errors, and it’s impossible for the compiler to know what you actually mean by that statement. You mean something more like this:
But I don’t recommend that. Easier to read and more fool-proof is to pull assignments out of if predicates,
And no, there’s no difference in the resulting assembly code; so there’s no performance difference.
There’s another problem with this line, but it’s somewhat pedantic. You shouldn’t cast
(socklen_t *) &size_sin. Instead, you should change the declaration ofsize_sinto have thesocklen_ttype to begin with. The only reason it works is becausesocklen_tis typedefed toint, but pretend you don’t know that and use the right type to begin with.Sample code:
Reccomendations: For now, try to stay away from putting side effects (
accept,bind, assignments, etc.) in conditionals. I’m not saying that it’s never okay to do this, but it looks like that’s where your problems are and it’s very easy to just move the side effects to a separate line of code, then only do the final comparison in the if condition or while condition.