Sign in to follow this  
CrazyCamel

Connection Accepted But Not Recorded

Recommended Posts

CrazyCamel    122
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

Share this post


Link to post
Share on other sites
cherryhouse    100
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)

Share this post


Link to post
Share on other sites
CrazyCamel    122
Not exactly-

acceptSocket=SOCKET_ERROR;
//resets acceptSocket to default

acceptSocket=accept(serverSocket,(sockaddr*)&addr,&addrlen);
//sets accept socket to the new client, or whatever accept returns that call

if(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)

Share this post


Link to post
Share on other sites
hplus0603    11356
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().

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this