Sign in to follow this  

select fails

This topic is 4836 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've got an FD_SET to hold all of my client sockets, but when I call select it fails. Do you have any idea what could cause this? I don't think I'm doind anything out of the ordinary. Tera_Dragon

Share this post


Link to post
Share on other sites
By fails I mean that it returns SOCKET_ERROR.
As for code, I'm not really sure about what to give so here's the call to select and a bit more:


WaitForSingleObject(mutexHandle, INFINITE);

tempSet = masterSet;

ReleaseMutex(mutexHandle);

if(tempSet.fd_count == 0)
{
return true;//NULL;
}

timeval waitTime;
waitTime.tv_sec = 0;
waitTime.tv_usec = 0;

error = select(tempSet.fd_count, &tempSet, NULL, NULL, &waitTime);

if( error == SOCKET_ERROR)
{
cout << "Error in select\n";
Shutdown();
return false;
}



Hope that this makes more sense

Share this post


Link to post
Share on other sites
Call WSAGetLastError() to get the error code of what went wrong, then look up that error code on MSDN to find out exactly what is wrong.

You did call WSAStartup(), didn't you?

Share this post


Link to post
Share on other sites
I've checked to see if it is empty, and if so then miss the select. But I just called Sleep for 10 seconds, long enough for me to get a clientto connect, and all was fine....

Edit: just found out that the error message was only delayed...duh

Share this post


Link to post
Share on other sites
The error code is 10038, WSAENOTSOCK. This means that I tried to perform a socket operation on something that is not a socket, or a member of my fd_set is not vaild when I pass it to the select function.
I still have no idea. Everything looks ok. But just incase that I am not setting the client sockets correctly here is my client acceptance code:


DWORD WINAPI NetworkServer::acceptingProcedure_Static(LPVOID lpParameter)
{
((NetworkServer*)lpParameter)->acceptingProcedure();
return 0;
}

void NetworkServer::acceptingProcedure()
{
for(;;) // run forever
{
if( unsigned int clientSocket = accept(mySocket, 0, 0) == SOCKET_ERROR)
{
//MessageBox(NULL, "Accept failed.", "Error", NULL);
cout << "Accept failed";
quitFlag = true;
return;
}
else
{
cout << "Client connected\n";
WaitForSingleObject(mutexHandle, INFINITE);

FD_SET(clientSocket, &masterSet);

ReleaseMutex(mutexHandle);
}
}
}



Tera_Dragon

Share this post


Link to post
Share on other sites
Maybe select works different with Winsock ( mostly use linux ) but the first parameter is the highest fd plus one, whereas in your code it seems to be the number of sockets. This might explain why it doesn't think its a socket.

Edit - nevermind. Looks like its not even used at all.
select

Share this post


Link to post
Share on other sites
As it looks as though no one can help me I've decided to try different things untill I get something that works. Right now I've taken the acceptingProcedure function out of the class.
However with this line of code:
FD_SET getmasterSet(){return getmasterSet;}

I get this error:
error C2664: '__thiscall fd_set::fd_set(const struct fd_set &)' : cannot convert parameter 1 from 'struct fd_set (__thiscall NetworkServer::*)(void)' to 'const struct fd_set
&'


I am trying to do this so that I can do this line of code in my acceptingProcedure:
FD_SET(clientSocket, &server.getmasterSet());
or something of this affect.
How can I do this without having to make masterSet global? I don't want to make it global because I keep getting this error:
NetworkServer.obj : error LNK2005: "struct fd_set masterSet" (?masterSet@@3Ufd_set@@A) already defined in Main.obj
Debug/Main.exe : fatal error LNK1169: one or more multiply defined symbols found


I have the masterSet in a seperate file, include this file in my class header, and then include the class header in my main file.
Please help
Tera_Dragon

Share this post


Link to post
Share on other sites
As it seems that no one could (or was willing to...) help me, I decided to start again, but this time not put the code into a class straight away. I have finished this, however Iam getting the following errors when I try t compile:


Main.obj : error LNK2001: unresolved external symbol __imp__accept@12
Main.obj : error LNK2001: unresolved external symbol __imp__ntohl@4
Main.obj : error LNK2001: unresolved external symbol __imp__closesocket@4
Main.obj : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
Main.obj : error LNK2001: unresolved external symbol __imp__recv@16
Main.obj : error LNK2001: unresolved external symbol __imp__select@20
Main.obj : error LNK2001: unresolved external symbol __imp__listen@8
Main.obj : error LNK2001: unresolved external symbol __imp__bind@12
Main.obj : error LNK2001: unresolved external symbol __imp__htons@4
Main.obj : error LNK2001: unresolved external symbol __imp__socket@12
Main.obj : error LNK2001: unresolved external symbol __imp__WSAStartup@8
Main.obj : error LNK2001: unresolved external symbol __imp__WSACleanup@0


I am including the ws2_32.lib like this:
#pragma (lib, "ws2_32.lib")

Please help ;-)

Share this post


Link to post
Share on other sites
I believe this is the offending code...


if( unsigned int clientSocket = accept(mySocket, 0, 0) == SOCKET_ERROR)
{
//MessageBox(NULL, "Accept failed.", "Error", NULL);
cout << "Accept failed";
quitFlag = true;
return;
}
else
{
cout << "Client connected\n";
WaitForSingleObject(mutexHandle, INFINITE);
FD_SET(clientSocket, &masterSet);
ReleaseMutex(mutexHandle);
}





Try putting the "unsigned int clientSocket = accept(...);" outside of the if/else statement and compare clientSocket to SOCKET_ERROR in the if statement. Do you see why? Or you can just as easily add some more parenthesis if you're not worried about clientSocket's scope:


if ((unsigned int clientSocket = accept(...)) == SOCKET_ERROR)
{...}




Share this post


Link to post
Share on other sites

This topic is 4836 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.

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