Solved

setsockopt

Posted on 1997-05-14
5
675 Views
Last Modified: 2008-03-17
I am currently trying to implement my first client server
application. I am using Perl 5.002 to do so. I noticed
that in the Camel Book pg.350 under the section about
sockets, they give an example of a multi-threaded server.
In that example the following statment is found:

        setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("1", 1))
                or die "setsockopt: $!";

But when I looked up the definition of setsockopt in the
aphabetical listing of all Perl functions (on pg.214) it says:

        setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, 1)
                or warn "Can't do setsockopt: $!\n";

My question is why would you use pack to set the value of OPTVAL
in the first example?

Any help would be greatly appreciated!
0
Comment
Question by:areeves
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 200 total points
ID: 1204006
pack("1", 1) should explicitly return the binary number 00000001.

Anyway, there's no difference between the two forms, as long as setsockopt() is concerned.
In fact, having a look at setsockopt(2), you may see that the 'option_value' parameter may be _any_ non-zero value for enabling a boolean option (zero for disabling it).

HTH, julio
0
 

Author Comment

by:areeves
ID: 1204007
OK, I guess I am still fuzzy on what all this means.
When you say OPTVAL can be any non-zero number to
enable boolean options, are you talking about OPTNAME?
The Pearl5 Camel Book talks about passing arguments,
and then states in the next sentence a common option
to set would be SO_REUSEADDR. Are they talking about
OPTNAME being the argument and/or the boolean option
you are talking about?

Then in the man pages it says that OPTVAL is the
address of a location in memory that contains the
option information to be passed to the calling
process. What are they talking about? And how does
it relate to this discussion?
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1204008
Hello, setsockopt(2) gives this synopsis (on DU), but note that this is a _C_function_:

int setsockopt (
  int socket,
  int level,
  int option_name,
  const void *option_value,
  size_t option_len );

The Perl setsockopt() is something similar (many perl functions resamble to C functions), but it does not need an 'option_len' parameter.

So:
'option_name' specifies the option to set;
'option_value' specifies if we want to enable or disable that option.

Now should be clear that, in the Perl5 Camel Book, SO_REUSEADDR is meant as the 'option_name' argument.

The problem with man pages is that you usually find the corrensponding C functions, but you should possibly care of the differences between C and Perl.

E.g., in the setsockopt() case, the 'option_value' parameter needed by the C function is a pointer to a location which holds the actual value, while the Perl function just needs the value itself.

In general, you may go this way (but a bit of experience will be of help): read a man page to understand the meaning and purpose of parameters, then go back to the book and (usually) you find that you just need to get rid of C pointers and directely pass values (variables) as parameters to the Perl function.

Hope this makes things a bit clearer, dispite of my English :)

Rgds, julio
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1204009
BTW, was this worth a D grade?
Ok, never mind, i guess you're new here...

Cheers, julio
0
 

Author Comment

by:areeves
ID: 1204010
Sorry, for the D grade, but I just felt like you could have
given me a little more explanation with your first answer.
Remember I said I was developing my first client server
application. Now if you would have given me the explanation you
gave me in the second response maybe a better grade would have
been in order. Also I don't really think your first response
directly answered my question. Were the authors of the Perl5
Camel Book trying to throw the reader a curve ball by doing
the same thing two different ways or was there really a reason
for what they did?

I hope you understand my position, and sorry if I insulted you
by giving you a low grade. None the less, I still appreciated
the help!

Thanks again for your quick response!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

19 Experts available now in Live!

Get 1:1 Help Now