Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Socket Descriptor return value

Posted on 2004-10-10
4
Medium Priority
?
1,367 Views
Last Modified: 2008-03-04
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
0
Comment
Question by:MGlobal
  • 2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:bass20
ID: 12275912
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 :)
0
 
LVL 1

Author Comment

by:MGlobal
ID: 12294063
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

0
 
LVL 1

Accepted Solution

by:
bass20 earned 300 total points
ID: 12296483
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)"
0
 
LVL 1

Author Comment

by:MGlobal
ID: 12314536
Thank  bass20.
MGlobal
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Tech spooks aren't just for those who are tech savvy, it also happens to those of us running a business. Check out the top tech spooks for business owners.
In this article, the configuration steps in Zabbix to monitor devices via SNMP will be discussed with some real examples on Cisco Router/Switch, Catalyst Switch, NAS Synology device.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question