Advertisement

How To Validate a Winsock Socket?

Started by September 04, 2005 01:11 PM
1 comment, last by Telastyn 19 years, 5 months ago
Hi, If a function was passed a SOCKET then how would it be possible to check that this socket is actually valid. When Socket() returns teh socket on creation you can check against an error code. But there is nothing stopping the programmer creating a socket of value 2 or something. The only technique for validating the socket i could think of woulf be to attempt to send a byte of data with send() and check the return code of this method. Assuming ofcourse that send() would return an error for a borked socket being passed in. Any ideas? ace
If you try and use a socket number that isn't a socket, you will get an error of 10038 (WSAENOTSOCK).

I don't lose track of my sockets, so if a socket is closed, then I remove it from the list. I use select() to see if the sockets have any new status (recv'ing or disconnection). Maybe using select() on an invalid socket will also return WSAENOTSOCK.
Advertisement
And the standard BSD sockets error is ENOTSOCK, and should behave the same way, except it works on unixy OSes too.

This topic is closed to new replies.

Advertisement