I have inherited some code that I am trying to understand and get working. I'm learning about sockets as I go. The method I am having problems with is one that calles the 'select' method. It fail and returns the error: WSAENOTSOCK. Could anyone possibly tell me what might be causing this. I have really struggled to anything that helps me with this.
The variable m_listeningSocket has been defined as an int and is the return value from socket:
int m_listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
although I don't understand why this return value has been set to an int as I thought it should be a SOCKET type - is this going to cause problems.
int Select( int timeout)
{
struct timeval timout;
int ret, maxfdp1;
if( timeout <= 0 )
return -1;
maxfdp1 = m_listeningSocket + 1;
FD_ZERO( &m_rset );
FD_SET ( m_listeningSocket, &m_rset );
FD_SET ( 0, &m_rset );
FD_ZERO( &m_wset );
FD_ZERO( &m_eset );
timout.tv_sec = timeout / 1000;
timout.tv_usec = timeout % 1000;
ret = ::select( maxfdp1, &m_rset, &m_wset, &m_eset, &timout );
return ret;
}
'getsockopt' returns zero, indicating that there is no problem, and the rest of the code that sets the socket up before I call the Select method is:
int CCCD_Socket::CreateListenSock()
{
int retVal = -1;
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(1, 1);
int err = WSAStartup(wVersionRequested, &wsaData);
if(err != 0)
{
printf("WSAStartup error %ld", WSAGetLastError());
WSACleanup();
return retVal;
}
struct sockaddr_in channel;
memset(&channel, 0, sizeof(channel));
channel.sin_family = AF_INET;
channel.sin_addr.s_addr = htonl(INADDR_ANY);
channel.sin_port = htons(SERVER_PORT);
m_listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
if(m_listeningSocket == -1)
{
printf("socket error %ld", WSAGetLastError());
WSACleanup();
return retVal;
}
int on = 1;
setsockopt(m_listeningSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
int b = bind(m_listeningSocket, (struct sockaddr*)&channel, sizeof(channel));
if(b < 0)
{
printf("bind error %ld", WSAGetLastError());
WSACleanup();
return retVal;
}
int l = listen(m_listeningSocket, 10);
if(l < 0)
{
printf("listen error %ld", WSAGetLastError());
WSACleanup();
return retVal;
}
else
{
retVal = m_listeningSocket;
}
return retVal;
}