Link to home
Start Free TrialLog in
Avatar of MGlobal
MGlobal

asked on

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
Avatar of bass20
bass20

Yes and no; using C in Unix/Linux the socket() system call returns a file descriptor for that socket. Therefore, the return value must be greather than or equal (>=) than 0 for the call to be successfull. In case of error, socket() will return -1.
You can test it's result like this:
   int mySock=0;
   if( (mySock=socket(...))<0 ) //there's an error
      perror("socket");
   //if it doesn't produce an error, mySock will have the value of the recently created file descriptor


Hope I helped :)
Avatar of MGlobal

ASKER

Hi  bass20,

I am using Windows. How about the return value?  All documenst I have read, they only check error when it is -1. But  by testing, I found that in Windows, it never returns 0 (if successful). But I am not sure socket() realy always returns > 0 (if successful). This is importance for me, because I have a program (written by other person), there will be a bug in that program, if socket() can return 0 in case successful.

Thanks,
MGlobal

ASKER CERTIFIED SOLUTION
Avatar of bass20
bass20

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MGlobal

ASKER

Thank  bass20.
MGlobal