Solved

Socket Descriptor return value

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Some time ago I was asked to set up a web portal PC to put at our entrance. When customers arrive, they could see a webpage 'promoting' our company. So I tried to set up a windows 7 PC as a kiosk PC.......... I will spare you all the annoyances I…
If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now