|
Question : Socket Descriptor return value
|
|
Hi All,
I have a question that, Socket function may return a value 0 when it creates socket successful?
socket function: SOCKET socket( int af, int type, int protocol );
Thanks in advandce.
Regards, MGlobal
|
Answer : Socket Descriptor return value
|
|
The socket() function (both in Windows and UNIX) returns a file descriptor, in an incremented fashion (for example, the first call returns 19, the second returns 20, the third returns 21, etc...); it'll never return 0 (to your app) because when the OS is running there are already three file descriptors assigned: "stdin", "stdout" and "stderr" (input, output, error output), and they take descriptors number 0, 1 and 2. Socket CAN RETURN 0 if there is no file descriptor with that number (for example, if you change stdin's descriptor), but it'll generally return > 2 because of those 3 file descriptors I mentioned. When Windows' socket() produces an error it returns an INVALID_SOCKET value; if you want to catch them or require detailed info about the API, read this: http://msdn.microsoft.com/library/en-us/winsock/winsock/socket_2.asp?frame=true
To see the error produced by the socket call upon create you can do something like: If myRecentlyCreatedSocket = INVALID_SOCKET Then Call ShowErrorMessage(Err.LastDllError)"
|
|
|
|