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);
Connection Accepted But Not Recorded
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: 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.
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)
acceptsocket = SOCKET_ERROR
if acceptsocket isnt SOCKET_ERROR and INVALID_SOCKET...
Try
if(acceptSocket==SOCKET_ERROR || acceptSocket==INVALID_SOCKET)
Hello?
Not exactly-
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)
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().
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 };
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement