Hi there, I'm a bit of a C++ newbie on the sly so be gentle. I'm delving into winsock apps, trying to sort out a non-blocking server but running into a few problems. Everything appears to work just fine, clients can connect, send data, get data back, fantastic. Downside is they can only do it one at a time :| Once the second client connects, my select() function is returning a 10038 error, or: WSAENOTSOCK: Socket operation on nonsocket. An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid. Now, I've tried my best to find out why my socket isn't being a socket and so far not having much luck, however the first socket that's opened has the id 3452816845, and the second merely 316! That set alarm bells ringing, so I can only assume it's on creation they're not having fun. Here's some code!
bool CClientSocket::Init(CListenSocket &listenSock)
{
int tempSize = sizeof(addr);
sock = accept(listenSock.GetSocket(), (struct sockaddr*)&addr, &tempSize);
if (sock == INVALID_SOCKET || sock == 0)
{
return false;
}
ipAddr = inet_ntoa(addr.sin_addr);
connected = true;
return true;
}
void CServer::RecvData()
{
fd_set inputSet;
fd_set errorSet;
FD_ZERO(&inputSet);
FD_ZERO(&errorSet);
for (int i = 0 ; i < clientVector.size() ; ++i)
{
FD_SET(clientVector->GetSocket(), &inputSet);
FD_SET(clientVector->GetSocket(), &errorSet);
}
// here when I open two clients to it I'm getting error 10038
// this means one of the sockets in the set "inputSet" is not a valid socket
// don't know why just yet
while (select(clientVector.size(), &inputSet, NULL, &errorSet, &timeout) > 0)
{
for (int i = 0 ; i < clientVector.size() ; ++i)
{
if (FD_ISSET(clientVector->GetSocket(), &errorSet))
{
std::cout << "Client sent an erroneous message" << std::endl;
}
if (FD_ISSET(clientVector->GetSocket(), &inputSet))
{
char buffer[1];
recv(clientVector->GetSocket(), buffer, 1, 0);
std::cout << buffer << std::endl;
}
}
}
}
void CServer::AcceptClient()
{
clientVector.push_back(new CClient);
if (!clientVector.front()->GetCSocket()->Init(listenSock))
{
std::cout << "Error: " << WSAGetLastError();
std::cout << " // Failed to accept client" << std::endl;
clientVector.pop_back();
}
else
{
std::cout << "Client connected on " << clientVector.front()->GetCSocket()->GetIP() << std::endl;
std::cout << clientVector.size() << " clients connected." << std::endl;
}
}
I can post as much of the rest of it as anyone would like, just thought these three snippets might be enough for now. As I said, I'm a nub, don't pull any punches, tell me where I've been stupid :) Thanks in advance :) [edit] Thought I might add a topic name! [/edit]
[Edited by - Craggeh on July 6, 2004 5:14:20 AM]