Solved

Socket Descriptor return value

Posted on 2004-10-10
4
1,360 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 100 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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…

856 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