views:

56

answers:

1

I'm currently working on a server. I know that the client side is working (I can connect to www.google.com on port 80, for example), but the server is not functioning correctly. The socket has socket()ed, bind()ed, and listen()ed successfully and is on an accept loop. The only problem is that accept() doesn't seem to work. netstat shows that the server connection is running fine, as it prints the PID of the server process as LISTENING on the correct port. However, accept never returns. Accept just keeps running, and running, and if i try to connect to the port on localhost, i get a 10061 WSACONNREFUSED. I tried looping the connection, and it just keeps refusing connections until i hit ctrl+c. I put a breakpoint directly after the call to accept(), and no matter how many times i try to connect to that port, the breakpoint never fires.

Why is accept not accepting connections? Has anyone else had this problem before?

Known:

[breakpoint0]
if ((new_fd = accept(sockint, NULL, NULL)) == -1) {
   throw netlib::error("Accept Error"); //netlib::error : public std::exception
}
else {
   [breakpoint1]
   code...;
}

breakpoint0 is reached (and then continued through), no exception is thrown, and breakpoint1 is never reached. The client code is proven to work. Netstat shows that the socket is listening.

If it means anything, i'm connecting to 127.0.0.1 on port 5842 (random number). The server is configured to run on 5842, and netstat confirms that the port is correct.

A: 

You could try using select and see if that one triggers. Because it seems as though accept never receives any connection:

fd_set mySet;
FD_ZERO(&mySet);
FD_SET(sockint, &mySet);
timeval zero = { 0, 0 };
int sel = select(0, &mySet, NULL, NULL, &zero);
if (FD_ISSET(sockint, &mySet)){
    // you have a new caller
    SOCKET newSocket = accept(sockint, NULL, NULL);
}
else{
    // still waiting
    <print>("Still waiting for connection.."); // whatever printfunction you use..
}
Default