Advertisement

Winsock 10038 error woes

Started by July 05, 2004 02:05 PM
1 comment, last by Craggeh 20 years, 7 months ago
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]
char buffer[1];

get rid of the [1]. Unrelated to your problem (probably), but a problem nonetheless...

std::cout << "Client connected on " << clientVector.front()->...

didn't you jsut push the new one onto the back? Unrelated again (maybe).

clientVector.push_back(new CClient);

that may be causing memorey leakes when you pop things off... unless you're using shard_ptr's in the container... Unrelated.

select(clientVector.size(),

should be +1, but on windows I suppose it doesn't matter.

Okay, so.

GetScoket() just returns the sock variable, right?

hmmm...
Advertisement
This might sound a bit daft, but I'm in a suikoden speed run competition at the moment so my replies might not be the quickest for the next hour and a half :)

I was only passing a byte at a time to the buffer (afaik), hence that was 1 at the time. I deserve a slap for the front even though I pushed it to the back, heh. GetSocket() does indeed return the SOCKET.

[EDIT] Just though I'd add I'm using Winsock 2 and I've finished my Suikoden run for tonight, about 20 minutes behind the leader :( Ah well. [/EDIT]

[Edited by - Craggeh on July 5, 2004 3:21:35 PM]

This topic is closed to new replies.

Advertisement