setsockopt

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!
areevesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

julio011597Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
areevesAuthor Commented:
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
julio011597Commented:
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
julio011597Commented:
BTW, was this worth a D grade?
Ok, never mind, i guess you're new here...

Cheers, julio
0
areevesAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.