Solved

setsockopt

Posted on 1997-05-14
5
677 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

829 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