I've got an application using sockets (which I did not write, so bear with me) and when I try to close a socket, closesocket() fails with error WSAEOPNOTSUPP and the socket sticks around...as in, it is not fully deleted.
The socket is created as follows:
bool Socket::CreateConnection()
{
int error;
struct addrinfo hints;
struct addrinfo *list = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
error = getaddrinfo(socketAddress.c_str(), socketPort.c_str(), &hints, &list);
if (error)
{
Log().RecordError("Unable to initialize connection to server", WSAGetLastError(), EventTypeError);
}
for (struct addrinfo *ptr = list; ptr != NULL; ptr = ptr->ai_next)
{
connection = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (connection == INVALID_SOCKET)
{
Log().RecordError("Unable to initialize socket connection", WSAGetLastError(), EventTypeError);
break;
}
error = connect(connection, ptr->ai_addr, (int)ptr->ai_addrlen);
if (error == SOCKET_ERROR)
{
closesocket(connection);
connection = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(list);
return connection != INVALID_SOCKET;
}
And the destructor for the class:
Socket::~Socket()
{
int error;
if (connection != INVALID_SOCKET)
{
continueReading = false;
error = shutdown(connection, SD_SEND);
if (error != SOCKET_ERROR)
{
/* Finish any necessary receives to politely close the socket */
int length;
do
{
char buffer[1024];
length = recv(connection, buffer, sizeof(buffer)/sizeof(*buffer), MSG_WAITALL);
} while (length > 0);
}
else
{
Log().RecordError("Error shutting down connection to server", WSAGetLastError(), EventTypeError);
}
closesocket(connection); //Fails HERE
int wsa_err = WSAGetLastError();
if(wsa_err)
Log().RecordError("closesocket() error", wsa_err, EventTypeError);
}
if (winsockInitialized)
{
WSACleanup();
}
}
Anyone have a clue why this would happen??