Jump to content
  • Advertisement
Sign in to follow this  
CrazyCamel

Connection Accepted But Not Recorded

This topic is 4485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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
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
Ah thanks, it was WSAEFAULT, because I never initiated addrlen, and so the function didn't know what the length of addr was.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!