Solved

Socket Descriptor return value

Posted on 2004-10-10
4
1,362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
If your business is like most, chances are you still need to maintain a fax infrastructure for your staff. It’s hard to believe that a communication technology that was thriving in the mid-80s could still be an essential part of your team’s modern I…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

751 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