SOCKET sock[32]
what is the value of for example sock[3]? or better said, how do I know that this socket is not taken? also what value it has when I close socket using closesocket(sock[3])
I need this, because I need to find a "empty" socket that is not connected to any client on my server when a client connects
You don't if you don't properly initialize everything.
1 2 3 4 5 6 7 8 9 10 11 12
SOCKET sock[32] = { INVALID_SOCKET }; // All elements are invalid
...
sock[3] = socket( ... ); // Create a socket (well, try to, at least)
...
if (sock[3] != INVALID_SOCKET)
{
// flush and closesocket() here
}
If you know that there is no data to flush, just call closesocket(). Nothing bad will happen if it gets an INVALID_SOCKET as argument.
> There is no way to avoid it except for the example on the first line of code I gave you.
> SOCKET sock[32] = { INVALID_SOCKET }; // All elements are invalid
This would initialize sock[0] with INVALID_SOCKET and the remaining elements of the array with zeroes.