Connection Accepted But Not Recorded

Started by
3 comments, last by CrazyCamel 18 years, 1 month ago
I'm having a problem in my non-blocking program (NOT asynchronous) - when the client calls connect(), it gets accepted by the server's loop that calls accept() constantly, but accept() always returns SOCKET_ERROR or INVALID_SOCKET. Here's a snippet from the server loop, for accepting clients:
do  {
	acceptSocket=SOCKET_ERROR;
	sockaddr_in addr;
	int addrlen;
	acceptSocket=accept(serverSocket,(sockaddr*)&addr,&addrlen);
	if(acceptSocket!=SOCKET_ERROR && acceptSocket!=INVALID_SOCKET)  {
		clients.push_back(CLIENT(acceptSocket));
		AddToLog(hwnd,"Client connected.\r\n",19);
	}
} while(acceptSocket!=SOCKET_ERROR && acceptSocket!=INVALID_SOCKET);



The program never goes into the if statement, which would record the client's socket. Because it doesn't do this, the server cannot communicate with the client because it doesn't know it is connected! Why does accept() not return the correct socket? EDIT:: Using winsock 2 in MSVC++ 2005
Grab life by the cord.
Advertisement
Correct me if I'm wrong, but doesn't your code say:

acceptsocket = SOCKET_ERROR

if acceptsocket isnt SOCKET_ERROR and INVALID_SOCKET...

Try

if(acceptSocket==SOCKET_ERROR || acceptSocket==INVALID_SOCKET)
Hello?
Not exactly-
acceptSocket=SOCKET_ERROR;//resets acceptSocket to defaultacceptSocket=accept(serverSocket,(sockaddr*)&addr,&addrlen);//sets accept socket to the new client, or whatever accept returns that callif(acceptSocket!=SOCKET_ERROR && acceptSocket!=INVALID_SOCKET)//checks to see if accept socket is a new client (ie. not SOCKET_ERROR or INVALID_SOCKET)while(acceptSocket!=SOCKET_ERROR && acceptSocket!=INVALID_SOCKET);//if there was a new client accepted, check again to see if there are more clients waiting

Those are the lines you were talking about, correct?

EDIT:: Btw, this:
if(acceptSocket==SOCKET_ERROR || acceptSocket==INVALID_SOCKET)
is the opposite of this:
if(acceptSocket!=SOCKET_ERROR && acceptSocket!=INVALID_SOCKET)
Grab life by the cord.
Add an else to that if(), and log the actual error you're getting to output, which will tell you something about what's happening.

If nothing is printed, then you're hanging inside accept(), which you'll do if nobody's currently connecting and the accepting socket is blocking.

A common cause for failing to connect is to forget to use htons().
enum Bool { True, False, FileNotFound };
Ah thanks, it was WSAEFAULT, because I never initiated addrlen, and so the function didn't know what the length of addr was.
Grab life by the cord.

This topic is closed to new replies.

Advertisement