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 listen
before accept
?
Remember: socket
-> bind
-> listen
-> accept
.
Edit: Here's some commentary on your code.
struct protoent *pe;
struct sockaddr_in sin;
int sock;
struct sockaddr_in sin_clt;
int size_sin;
if ((pe = getprotobyname("TCP")) == NULL)
perro("getprotobyname");
sock = xsocket(AF_INET, SOCK_STREAM, pe->p_proto);
Using getprotobyname
is unnecessary, since the name is hard-coded and there's only one IP stream protocol anyway. Use 0
instead of pe->p_proto
, and don't bother calling getprotobyname
.
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
You don't initialize sin.sin_port
. That's an error.
while (bind(sock, (const struct sockaddr*)&sin,
(socklen_t)sizeof(sin)) == -1 && server->port2 <= 65535)
sin.sin_port = htons(server->port2++);
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.
if (listen(sock, 1) == -1)
perror("listen");
size_sin = sizeof(sin_clt);
if ((server->pasv_sock = accept(sock, (struct sockaddr*)&sin_clt,
(socklen_t*)&size_sin) == -1))
This definitely doesn't do what you want.
Discussion: I'll add parentheses to the last line to show you what it actually does.
if ((server->pasv_sock = accept(...) == -1))
is the same as
if ((server->pasv_sock = (accept(...) == -1)))
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:
if ((server->pasv_sock = accept(...)) == -1)
But I don't recommend that. Easier to read and more fool-proof is to pull assignments out of if predicates,
server->pasv_sock = accept(...);
if (server->pasv_sock == -1)
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 of size_sin
to have the socklen_t
type to begin with. The only reason it works is because socklen_t
is typedefed to int
, but pretend you don't know that and use the right type to begin with.
Sample code:
int port, sock, r, csock;
struct sockaddr_in saddr, caddr;
socklen_t caddrlen;
// This is a simpler way to get a TCP socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) abort();
// Loop over available ports, and bind to one
// (I'm not sure if this is the best way to do this)
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = INADDR_ANY;
for (port = 10000; port < 65536; ++port) {
saddr.sin_port = htons(port);
r = bind(sock, (struct sockaddr *) &saddr, (socklen_t) sizeof(saddr));
if (!r)
break;
}
if (r) abort();
// Listen and accept a connection
r = listen(sock, 1);
if (r < 0) abort();
caddrlen = (socklen_t) sizeof(caddr);
csock = accept(sock, (struct sockaddr *) &caddr, &caddrlen);
if (csock < 0) abort();
// You don't want to listen for more connections
close(sock);
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.
// Both of these are correct.
// The bottom one is obviously correct.
// Correctness is not always good enough.
// Being obviously correct is important!
if ((p->x = func()) == NULL)
...
p->x = func();
if (p->x == NULL)
...